繁体   English   中英

通过 Gremlin 在大图中的节点/边数?

[英]Number of nodes/edges in a large graph via Gremlin?

通过 Gremlin 计算大型图中节点/边数的最简单和最有效的方法是什么? 我发现的最好的方法是使用 V 迭代器:

gremlin> g.V.gather{it.size()}

但是,根据V 的文档,这对于大图来说不是一个可行的选择:

图的顶点迭代器。 利用它来遍历图中的所有顶点。 除非与键索引查找结合使用,否则请谨慎使用大型图。

我认为计算所有顶点的首选方法是:

gremlin> g = TinkerGraphFactory.createTinkerGraph()
==>tinkergraph[vertices:6 edges:6]
gremlin> g.V.count()
==>6
gremlin> g.E.count()
==>6

不过,我认为在一个非常大的图表上,无论你做什么, gV/E崩溃。 在非常大的图形上,进行计数的最佳选择是使用 Faunus( http://thinkaurelius.github.io/faunus/ )之类的工具,以便您可以利用 Hadoop 的强大功能并行进行计数。

更新:上面的原始答案是针对 TinkerPop 2.x。 对于 TinkerPop 3.x,答案大致相同,并暗示使用Gremlin Spark或某些特定于提供程序的工具(如 DataStax Graph 的DSE GraphFrames ),这些工具经过优化以执行这些类型的大规模遍历。

我尝试了上述方法,它对我不起作用。 对于你们中的一些人来说,这可能有效:

gremlin> g.V.count()
{"detailedMessage":"Query parsing failed at line 1, character position at 3, error message : no viable alternative at input 'g.V.'","code":"MalformedQueryException","requestId":"99f749db-c240-9834-aa12-e17bb21e598e"}
Type ':help' or ':h' for help.
Display stack trace? [yN]
gremlin> g.V().count()
==>37
gremlin> g.E().count()
==>45
gremlin> 

使用 gV( gV().count代替gVcount() (对于那些其他命令出错的地方)。

通过 python:

from gremlin_python.structure.graph import Graph
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection

graph = Graph()
graph_db_uri = 'ws://localhost/gremlin'

      
g = graph.traversal().withRemote(DriverRemoteConnection(graph_db_uri,'g'))
count=g.V().hasLabel('node_label').count().next()
print("vertex count: ",count)

count=g.E().hasLabel('edge_label').count().next()
print("edge count: ",count)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM