python-3.x - 了解使用 Gremlin-Python 添加边的不同方法

标签 python-3.x gremlin tinkerpop3 janusgraph

我试图了解方法的差异,以及在 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()”顶点时会发生什么,在这种情况下,也使用“__.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) 在添加边时使用 “合并”步骤 的正确用法是什么?一般来说,附加优势是什么?就像在 5 ^^ 的遍历中使用它一样? p>

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

我认为在操作上没有“最佳顺序”,但我个人认为立即查看 from()to() 会更好读遵循 addE()

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

您不能以那种方式使用中间遍历 V()。这样做基本上是在说“在图中找到顶点,其中的 T.id 是不存在的“a”。在你的例子中,“a”是一个局部于范围的步骤标签只有那个遍历。

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

有很大的不同。 property(k,v) 是一个变异步骤,它通过添加/更新具有指定值的属性键来修改流中的图形元素。 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 中使用,因为它没有第二个参数。当您想“获取或创建”或“更新插入”元素时,您通常会在添加顶点/边的上下文中使用 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() 是终止步骤。您通常调用它来生成遍历可能产生的任何副作用。一旦“迭代”,你真的不能再调用它,因为它不会有任何效果,因为迭代是一种单向操作,一旦遍历器对象用完,就没有什么可以再次 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() 的情况是您选择在 session 上下文或真正长时间运行的脚本上下文中自己管理事务。如果你正在构建一个新的应用程序,你应该避免这些选项中的任何一个,而只需使用 bytecode-based traversals .

关于python-3.x - 了解使用 Gremlin-Python 添加边的不同方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57441822/

相关文章:

python - TensorFlow 2 中的 tf.contrib.layers.complete_connected() ?

python-3.x - 如何使用 CLI 测试我自己的手写数字或 MNIST 数据集中的数据之一

python-3.x - 读取PE文件时如何将字节转换为手动输出

gremlin - 如何在小鬼查询中转义引号

azure-cosmosdb - 如何获得由满足特定条件的所有顶点组成的子图

java - 如何对 Amazon Alexa 进行 REST API 调用

graph - 泰坦 : Gremlin query returns inconsistent results on repeated execution

python-3.x - 亚马逊海王星 : How to create custom vertex id in Python using tinkerpop/gremlinpython package?

java - 我如何在 Java 中使用 Gremlin/Tinkerpop?

graph - 在 gremlin 查询中显示子级别