[英]tinkerpop3: use gremlin to count number of edges beetween all pairs of nodes
[英]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.