使用 Gremlin 查询的 Union 方法及其对 Neptune 交易的影响
Using Union Method of Gremlin Query and its affect on Neptune Transactions
假设我在 AWS Neptune 上创建了一个图形,并且我想获取有关某个顶点的多个信息。
我使用联合方法通过以下查询获取多个信息,
this.graphTraversalSource.V().hasLabel("label1").hasId("id1")
.union(__.valueMap(true) ,__.outE().groupCount().by(T.label).unfold(),
__.inE("created_post").outV().valueMap(true))
简单地说,查询试图获得以下内容,
1.顶点本身的属性。
2.出边及其计数
3.创建了id1的post的用户的属性上面的查询returns a
上述查询在 Gremlin-Server 上的示例结果如下
=>{label=label1, id=id1, text=[My sample text]}
==>has_comment=1
==>has_like=1
==>{label=user, id=user1}
现在,假设我要在 Java 中编写这些,因为您知道查询必须由 next() 或 iterate() 结束。如果我输入 next(),那么将返回上述结果中的第一项。 {label=label1, id=id1, text=[My sample text]}
。但是,如果我尝试使用 next(4),那么我将得到完整的结果。或者,我知道我可以使用 GraphTraversal 的 next() and hasNext()
来根据需要获取结果,而无需使用 next(4)
我担心提交给 Neptune 的交易,因为据他们说
Transactions Neptune opens a new transaction at the beginning of each
Gremlin traversal and closes the transaction upon the successful
completion of the traversal. The transaction is rolled back when there
is an error.
Multiple statements separated by a semicolon (;) or a newline
character (\n) are included in a single transaction. Every statement
other than the last must end with a next() step to be executed. Only
the final traversal data is returned.
Manual transaction logic using tx.commit() and tx.rollback() is not
supported.
根据以上内容,我了解到每个 .next() 语句都会导致在 Neptune 发生 tx。
说了这么多,我的问题是,
- 我对海王星交易技术的理解是真的吗?
- 如果是,是否意味着我每次写
.next()
都会发生一笔交易?
.next(int)
的行为如何,例如使用 .next(4)
是否意味着将发生 4 笔交易?
- 我该如何优化它?使用一个数据库访问和一个事务获取所有需要的数据?即有更好的方法来满足我的需求吗?
- 如何使用一行获取上述查询的所有结果?即不使用 hasNext() 和 next() 方法?有没有办法知道结果集的大小,从而使用 next(result-set-size) 方法?
谢谢
哈迪
您可以通过 next()
和 iterate()
以外的多种方式终止遍历 - 例如 toList()
获取所有结果并将它们打包到 List
对象中.我会说这是人们终止遍历的最常见方式(并且最容易使用),假设您不介意在客户端的内存中实现 List
。
由于 Neptune 使用 TinkerPop 驱动程序,我可以说调用 next()
(或 toList() or other terminators) on bytecode-based traversals will not trigger additional requests to Neptune and therefore not start new transactions. The call to
next(),
toList()` 和其他终止步骤对流回的数据进行操作给客户。
我相信您指出的这部分文档:
Multiple statements separated by a semicolon (;) or a newline character (\n) are included in a single transaction. Every statement other than the last must end with a next() step to be executed. Only the final traversal data is returned.
与基于脚本的执行相关,可能不适用,因为您似乎正在使用基于字节码的遍历。
假设我在 AWS Neptune 上创建了一个图形,并且我想获取有关某个顶点的多个信息。
我使用联合方法通过以下查询获取多个信息,
this.graphTraversalSource.V().hasLabel("label1").hasId("id1")
.union(__.valueMap(true) ,__.outE().groupCount().by(T.label).unfold(),
__.inE("created_post").outV().valueMap(true))
简单地说,查询试图获得以下内容, 1.顶点本身的属性。 2.出边及其计数 3.创建了id1的post的用户的属性上面的查询returns a
上述查询在 Gremlin-Server 上的示例结果如下
=>{label=label1, id=id1, text=[My sample text]}
==>has_comment=1
==>has_like=1
==>{label=user, id=user1}
现在,假设我要在 Java 中编写这些,因为您知道查询必须由 next() 或 iterate() 结束。如果我输入 next(),那么将返回上述结果中的第一项。 {label=label1, id=id1, text=[My sample text]}
。但是,如果我尝试使用 next(4),那么我将得到完整的结果。或者,我知道我可以使用 GraphTraversal 的 next() and hasNext()
来根据需要获取结果,而无需使用 next(4)
我担心提交给 Neptune 的交易,因为据他们说
Transactions Neptune opens a new transaction at the beginning of each Gremlin traversal and closes the transaction upon the successful completion of the traversal. The transaction is rolled back when there is an error.
Multiple statements separated by a semicolon (;) or a newline character (\n) are included in a single transaction. Every statement other than the last must end with a next() step to be executed. Only the final traversal data is returned.
Manual transaction logic using tx.commit() and tx.rollback() is not supported.
根据以上内容,我了解到每个 .next() 语句都会导致在 Neptune 发生 tx。
说了这么多,我的问题是,
- 我对海王星交易技术的理解是真的吗?
- 如果是,是否意味着我每次写
.next()
都会发生一笔交易? .next(int)
的行为如何,例如使用.next(4)
是否意味着将发生 4 笔交易?- 我该如何优化它?使用一个数据库访问和一个事务获取所有需要的数据?即有更好的方法来满足我的需求吗?
- 如何使用一行获取上述查询的所有结果?即不使用 hasNext() 和 next() 方法?有没有办法知道结果集的大小,从而使用 next(result-set-size) 方法?
谢谢 哈迪
您可以通过 next()
和 iterate()
以外的多种方式终止遍历 - 例如 toList()
获取所有结果并将它们打包到 List
对象中.我会说这是人们终止遍历的最常见方式(并且最容易使用),假设您不介意在客户端的内存中实现 List
。
由于 Neptune 使用 TinkerPop 驱动程序,我可以说调用 next()
(或 toList() or other terminators) on bytecode-based traversals will not trigger additional requests to Neptune and therefore not start new transactions. The call to
next(),
toList()` 和其他终止步骤对流回的数据进行操作给客户。
我相信您指出的这部分文档:
Multiple statements separated by a semicolon (;) or a newline character (\n) are included in a single transaction. Every statement other than the last must end with a next() step to be executed. Only the final traversal data is returned.
与基于脚本的执行相关,可能不适用,因为您似乎正在使用基于字节码的遍历。