通过gremlin查询计算titangraph中的子节点总数

时间:2017-03-24 05:24:24

标签: gremlin

我在Java中创建了层次树的titan图。 如何使用gremlin从指定节点查找总子节点层次结构。 建议我使用gremlin查询计数,它应该更快。

1 个答案:

答案 0 :(得分:3)

遍历树的基本模式位于repeat()步。例如,我使用TinkerPop文档的Tree Recipes部分中描述的图表:

gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV(id, 'A').as('a').
......1>            addV(id, 'B').as('b').
......2>            addV(id, 'C').as('c').
......3>            addV(id, 'D').as('d').
......4>            addV(id, 'E').as('e').
......5>            addV(id, 'F').as('f').
......6>            addV(id, 'G').as('g').
......7>            addE('hasParent').from('a').to('b').
......8>            addE('hasParent').from('b').to('c').
......9>            addE('hasParent').from('d').to('c').
.....10>            addE('hasParent').from('c').to('e').
.....11>            addE('hasParent').from('e').to('f').
.....12>            addE('hasParent').from('g').to('f').iterate()
gremlin> g.V('F').repeat(__.in('hasParent')).emit().count()
==>6
gremlin> g.V('C').repeat(__.in('hasParent')).emit().count()
==>3
gremlin> g.V('A').repeat(__.in('hasParent')).emit().count()
==>0

获得计数的关键在于emit()的使用,它允许计算repeat()中遇到的所有遍历者。

为了比较你可以通过TinkerGraph(内存中)获得什么样的速度,我生成了一个400,000顶点深度树:

gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> lastV = g.addV().next()
==>v[0]
gremlin> (0..<400000).each{lastV=g.V(lastV).as('f').addV().as('t').addE('next').from('f').to('t').select('t').next()}
==>0
==>1
==>2
==>3
...
gremlin> graph
==>tinkergraph[vertices:400001 edges:400000]
gremlin> clockWithResult{ g.V(0L).repeat(__.out('next')).emit().count().next() }
==>171.44102253
==>400000

完成171ms。 TinkerGraph显然更快,因为它将数据完全保留在内存中。 Titan / JanusGraph和其他图表必须从磁盘读取。