我正在试验TinkerPop3 文档中的遍历示例。在Gremlin shell 中使用,加载了经典图形g = TinkerFactory.createClassic():
gremlin> marko = g.v(1)
==>v[1]
gremlin> marko
==>v[1]
Run Code Online (Sandbox Code Playgroud)
然而:
gremlin> marko = g.V().has('name', 'marko')
==>v[1]
gremlin> marko
gremlin>
Run Code Online (Sandbox Code Playgroud)
为什么第二种形式没有捕获v[1]?
鉴于第二种形式,尝试使用该变量会导致错误:
gremlin> marko.out('knows')
The traversal strategies are complete and the traversal can no longer have steps added to it
Display stack trace? [yN]
Run Code Online (Sandbox Code Playgroud) 我有一个像这样的递归数据结构:
@Canonical
static class Person {
String name
Set<Person> knows
}
Run Code Online (Sandbox Code Playgroud)
我有一个代表这种结构的Tinkerpop图:
(Jon) -- knows --> (Billy) -- knows --> (Jane) -- ... -->
\- knows --> (Suzy) -- ... -->
Run Code Online (Sandbox Code Playgroud)
将任意深度的Tinkerpop图映射到数据结构的最有效方法是什么?
我可以想象使用PathStep它,但似乎应该有一个更好的方法,而且我不是很好地看待TP3.
def 'build recursive person object'() {
when:
def g = TinkerGraph.open()
def jon = g.addVertex(T.label, 'person', 'name', 'jon')
def bill = g.addVertex(T.label, 'person', 'name', 'bill')
def jane = g.addVertex(T.label, 'person', 'name', 'jane')
jon.addEdge('knows', bill)
bill.addEdge('knows', jane)
Multimap<String, Person> relationships = HashMultimap.create()
g.V().has('name', 'jon')
.as('a')
.jump('b', …Run Code Online (Sandbox Code Playgroud) 我的场景是在单个查询中在顶点之间添加多个边:
假设以下节点:这些是我拥有的标签和ID
用户数:
4100
歌曲:
4200
4355
4676
我必须在这些顶点之间建立边缘
4100 --> 4200,
4100 --> 4355,
4100 --> 4676.
Run Code Online (Sandbox Code Playgroud)
通常可以通过在node之间创建单个边来完成此操作。如果我们想一次在50个以上的顶点之间创建边,则这不是一种有效的方法。我正在使用Tinkerpop 3.0.1。
我使用cassandra 2.1.7支持的Titan 1.0.0作为后端存储.在尝试在gremlin控制台和java程序上执行相同的查询时,我得到了两种不同格式的输出.我正在使用titan-1.0.0-hadoop1提供的gremlin控制台,对于java我正在使用TinkerPop Gremlin 3.0.1-incubating.
Gremlin控制台:
gremlin> g.V().has('msid',within(-2128958273, 2147477890)).as('in').local(outE('hie_child').has('hostid_e',within(153,83)).order().by('hrank',incr).limit(5)).group().by(outV().id()).by(inV().id().fold())
Run Code Online (Sandbox Code Playgroud)
==> [77467688:[1531850904,4742561976,1009049792,1010020408,1053356264],73363640:[2060075072,3698942184,6776295608,7030726848,35401920]]
我得到了预期的输出类型,即 Map<VertexId, List<VertexId>>
但是在java程序中执行相同的查询时,我得到了Map<VertexId, BulkSet>.的BulkSet包括计数器指示的时间的特定条目在结果集增加的号码.有人可以告诉我是否有办法在java中获得与gremlin控制台类似的结果.
Java的:
List<Map<Object, Object>> list = g.V().has("msid", P.within(-2128958273,2147477890)).as("in").local(__.outE("hie_child").has("hostid_e", P.within(153,83)).order().by("hrank", Order.incr).limit(5)).group().by(__.outV().id()).by(__.inV().id().fold()).fold().next();
System.out.println(list);
Run Code Online (Sandbox Code Playgroud)
[{77467688 = {1531850904 = 1,4742561976 = 1,1009049792 = 1,1010000408 = 1,1053356264 = 1},73363640 = {2060075072 = 1,3698942184 = 1,6776295608 = 1,7030726848 = 1,35401920 = 1}} ]
我在应用程序中使用 gremlin REST 服务器,并且想在单个查询中创建到顶点的多条边。我有一个顶点 ID 列表,从那里创建边到单个顶点。
例如 - gV(12,13,14,15).addEdge('uses', gV(100))
我尝试了很多遍历步骤,但无法使其工作。
图表如下:
gremlin> a = graph.addVertex("name", "alice")
gremlin> b = graph.addVertex("name", "bobby")
gremlin> c = graph.addVertex("name", "cindy")
gremlin> d = graph.addVertex("name", "david")
gremlin> e = graph.addVertex("name", "eliza")
gremlin> a.addEdge("rates",b,"tag","ruby","value",9)
gremlin> b.addEdge("rates",c,"tag","ruby","value",8)
gremlin> c.addEdge("rates",d,"tag","ruby","value",7)
gremlin> d.addEdge("rates",e,"tag","ruby","value",6)
gremlin> e.addEdge("rates",a,"tag","java","value",10)
Run Code Online (Sandbox Code Playgroud)
我有3个脚本:
脚本#1
gremlin> g.V().has('name','alice').
repeat(out()).
until(has('name','alice')).
cyclicPath().
path().by('name')`
==>[alice,bobby,cindy,david,eliza,alice]
Run Code Online (Sandbox Code Playgroud)
脚本#2
gremlin> g.V().has('name','alice').
repeat(outE().inV()).
until(has('name','alice')).
cyclicPath().
group().
by('name').
by(path().unfold().has('value').values('value').fold()).
next()
==>alice=[9, 8, 7, 6, 10]
Run Code Online (Sandbox Code Playgroud)
脚本#3
gremlin> g.V().has('name','alice').
repeat(outE().inV()).
until(has('name','alice')).
cyclicPath().
group().
by('name').
by(path().unfold().has('value').values('value').fold()).
next().collect { k, v ->
k + '=' + v.withIndex().collect { …Run Code Online (Sandbox Code Playgroud) 例如,给定此图:
gremlin> graph = TinkerFactory.createModern() (1)
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal(standard()) (2)
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().has('name','marko').out('knows').values('name') (3)
Run Code Online (Sandbox Code Playgroud)
我想从控制台打印出该图形的PNG文件.文档没有说明如何做到这一点.提前致谢.
我正在使用Janus Graph文档,并且已按提及将其提取。
./gremlin.sh
Run Code Online (Sandbox Code Playgroud)
工作正常,并启动Gremlin提示符。
此代码也可以正常工作
graph = JanusGraphFactory.open('inmemory')
g = graph.traversal()
Run Code Online (Sandbox Code Playgroud)
问题
当我这样做时,我得到了巨大的堆栈跟踪
graph = JanusGraphFactory.open('conf/janusgraph-berkeleyje-es.properties')
gremlin> graph = JanusGraphFactory.open('conf/janusgraph-berkeleyje-es.properties')
12:15:49 WARN org.janusgraph.diskstorage.es.rest.RestElasticSearchClient - Unable to determine Elasticsearch server version. Default to FIVE.
java.net.ConnectException: Connection refused
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
at org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor.processEvent(DefaultConnectingIOReactor.java:171)
at org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor.processEvents(DefaultConnectingIOReactor.java:145)
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.execute(AbstractMultiworkerIOReactor.java:348)
at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.execute(PoolingNHttpClientConnectionManager.java:192)
at org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase$1.run(CloseableHttpAsyncClientBase.java:64)
at java.lang.Thread.run(Thread.java:748)
Could not instantiate implementation: org.janusgraph.diskstorage.es.ElasticSearchIndex
Type ':help' or ':h' for help.
Display stack trace? [yN]
Run Code Online (Sandbox Code Playgroud) 在发现你必须使用from_而不是from使用gremlin javascript 之前,我挣扎了好几个小时.
在深入挖掘源代码后,我终于发现代码正在使用from_而不是代码from(参见代码).因为我是新手,所以这很奇怪,因为它的对应物to仍然是to(而不是to_,请参阅此处的代码)
我用谷歌搜索到了,但无法找到这种方式的原因,并且对使用下划线版本感到不安,因为大多数时候下划线表示用户不应该真正信任的私有方法.
另外,是否有一个我错过的gremlin javascript官方文档页面?我担心将来我可能会遇到这些问题,而且JavaScript版本并没有真正的官方文档,我可能需要经历同样的困难.我喜欢gremlin,但如果JavaScript版本不稳定且不应该使用,我不妨考虑除Tinkerpop套件之外的其他选择.
我用打TinkerGraph和gremlin-scala,我看到它是能够坚持复杂的对象:
case class InnerObj(a: Int, b: String)
case class ComplexObj(a: Int, b: InnerObj)
case class SuperComplexObj(a : String, b: ComplexObj)
class GremlinQueriesSpec extends FlatSpec
with ScalaFutures with MustMatchers {
behavior of "Gremlin queries"
it must "be able to persist complex objects containing collections" taggedAs Integration in {
val g = TinkerGraph.open()
implicit val graph = g.asScala
val user = RandomData.randomUserDataAggregate
graph + user
graph.V().toCC[UserDataAggregate].toList() must be eq List(user)
}
}
Run Code Online (Sandbox Code Playgroud)