了解使用 Gremlin-Python 添加边的不同方法

Understanding different methods for adding edges with Gremlin-Python

我试图了解方法的差异,以及在 Gremlin 中添加边(在现有顶点之间)的最佳语法-Python。

在这里阅读了几篇关于 SO 的帖子后,我将发现的一些不同方法细分为几个问题。

非常感谢您的提前反馈!

1) 创建边缘时向边缘添加属性 的最佳顺序是什么:其中哪一个是更好的选择(如果有任何显着差异)完全)?

g.V().property("prop1", "prop1_val").as_("a")
 .V().property("prop2", "prop2_val").as_("b")
 .addE("some_relationship")
# rest of traversal option 1:
 .property("prop1_val_weight", 0.1d)
 .from_("a").to("b")
# rest of traversal option 2:
 .from_("a").to("b")
 .property("prop1_val_weight", 0.1d)

2) " __.V() "?

的目的和正确用法是什么
g.V().property("prop1", "prop1_val")
 .as_("a").V().property("prop2", "prop2_val")
 .as_("b").addE("some_relationship")
 .property("prop1_val_weight", 0.1d)
# AND THEN:
 .from_("a").to("b")
# VERSUS: 
 .from_(__.V("a")).to(__.V("b"))

3) 使用 "property" 与 "properties" 有什么区别:

g.V().property("prop1", "prop1_val").as_("a")
# VERSUS:
g.V().properties("prop1", "prop1_val").as_("a")
# REST OF THE TRAVERSAL:
 .V().property("prop2", "prop2_val").as_("b")
 .addE("some_relationship")
 .property("prop1_val_weight", 0.1d)
 .from_("a").to("b")

4) 当没有指定“.to()”vertex/vertices时会发生什么,在这种情况下,还使用“__.V( ) " :

g.V().property("prop1", "prop1_val").as_("a")
 .V().property("prop2", "prop2_val").as_("b")
 .addE("some_relationship").to(__.V()
 .has("prop2", "prop2_val"))

5) 在遍历末尾添加“.profile()”的原因是什么:

g.V('Alice').as_('v').V('Bob').coalesce(inE('spokeWith')
 .where(outV().as_('v')).addE('spokeWith')
 .property('date', 'xyz').from_('v'))
 .profile()

6) 在添加边时使用 "coalesce" 步骤 的正确用法是什么,通常还有额外的好处,就像在遍历中使用它一样在 5 ^^ ?

7) 以及一些一般性问题

  • 同时查找标签的优势是什么,例如" g.V().has("LABEL1", "prop1", "prop1_val").as_("a") [等]"
  • 在将遍历分配给变量后(例如,“ t = g.V() ...”)在几个步骤中,仅一次就足够了,最后,调用 "t.iterate()" 还是应该每次都这样做?
  • 应该在脚本中的哪个点调用 "tx.commit()":在几次遍历结束时只调用一次就足够了吗?

1) What is the best order of adding properties to the edge, while creating it

我认为在操作上没有 "best order",但我个人认为 from()to() 紧跟在 addE() 之后更好读。

2) What is the purpose, and correct usage, of " __.V() "?

您不能以那种方式使用中间遍历 V()。这样做基本上是说 "find me the vertex in the graph with the T.id of "a" 不存在。在您的情况下,"a" 是一个步骤标签,仅在该遍历范围内。

3) What are the differences between using "property" vs. "properties":

有很大的不同。 property(k,v) 是一个突变步骤,它通过 adding/updating 和 属性 键修改流中的图形元素和指定的值。 properties(k...) 获取由提供的键指定的属性列表(即读取操作)。

4) What happens when there is no ".to()" vertex/vertices specified, and in this case, also using " __.V() "

为什么不启动 Gremlin 控制台并查看:

gremlin> g.addV().addE('self')
==>e[17][16-self->16]
gremlin> g.addV().as('z').addE('self').property('x','y').from('z').to(V().has('person','name','nobody'))
The provided traverser does not map to a value: v[20]->[TinkerGraphStep(vertex,[~label.eq(person), name.eq(nobody)])]
Type ':help' or ':h' for help.
Display stack trace? [yN]

5) 在遍历末尾添加“.profile()”的原因是什么:

分析代码或解释 SQL 查询的原因相同 - 更详细地了解该代码的作用。也许您需要查看索引是否被正确使用或找出执行时间最长的步骤以查看是否可以更好地优化遍历。

6) What is the correct usage, and in general the added advantage, of using the "coalesce" step while adding edges, like it's being used in the traversal at 5 ^^ ?

除非我误读了它,否则我看不出有任何理由使用 coalesce() 因为它在 5 中使用,因为它没有第二个参数。当您想 "get or create" 或 "upsert" 一个元素时,您通常在添加 vertices/edges 的上下文中使用 coalesce() - 您可以阅读更多关于 here.

what is the advantage of also looking for the label, e.g. " g.V().has("LABEL1", "prop1", "prop1_val").as_("a") [etc.]"

最好在执行 has() 时包含顶点标签,因为这样您就可以明确命名要搜索的键。您在示例中使用了 "prop1" - 如果 "prop1" 不是全局唯一键并且您只想要 "LABEL1" 顶点但 "LABEL2" 顶点也具有 "prop1" 值,那么您可能得不到你想要的。我想如果你使用全局唯一的键名那么这不是问题但我认为如果你想最大化你的 Gremlin 的可移植性你可能想要实践因为我认为那里有一些图形系统需要标签的规格。

after assigning a traversal to a variable (eg. " t = g.V() ... " in several steps, is it sufficient to then only once, at the end, call "t.iterate()" or should this be done each time?

iterate() 是终止步骤。您通常调用它来生成遍历可能产生的任何副作用。一旦 "iterated",你真的不能再次调用它,因为它不会有任何效果,因为迭代是一种单向操作,一旦遍历器对象用完,就没有什么可以再次 iterate() 了。

at which point in a script should one call "tx.commit()": is calling it only once, at the end of several traversals, sufficient?

如果您遵循最佳做法,则根本不会调用 commit()。 Gremlin 服务器为您管理您的交易。一个请求(即遍历)基本上是一个事务。您唯一需要调用 commit() 的情况是您选择在会话或非常长的 运行 脚本的上下文中自己管理事务。如果您正在构建一个新的应用程序,您应该避免使用这些选项中的任何一个,而只需使用 bytecode-based traversals.