Gremlin 在 Neptune 中的交易 - Amazon Neptune
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

Gremlin 在 Neptune 中的交易

在多个上下文中执行 Gremlin 事务。在使用 Gremlin 时,重要的是要了解你所处的环境及其含义:

  • Script-based— 使用基于文本的 Gremlin 字符串发出请求,如下所示:

    • 使用 Java 驱动程序和Client.submit(string).

    • 使用 Gremlin 控制台和:remote connect.

    • 使用 HTTP API。

  • Bytecode-based— 使用 Gremlin 语言变体 (GLV) 中典型的序列化 Gremlin 字节码发出请求。

    例如,使用 Java 驱动程序g = traversal().withRemote(...)

对于上述任一上下文,都有额外的上下文,即请求以无会话形式发送或绑定到会话发送。

注意

必须始终提交 Gremlin 事务或回滚事务,这样才能释放服务器端资源。

无会话请求

无会话时,请求等同于单个事务。

对于脚本而言,这意味着在单个请求中发送的一条或多条 Gremlin 语句将作为单个事务提交或回滚。例如:

Cluster cluster = Cluster.open(); Client client = cluster.connect(); // sessionless // 3 vertex additions in one request/transaction: client.submit("g.addV();g.addV();g.addV()").all().get();

对于字节码,每次从以下来源生成和执行的遍历都会发出无会话请求g

GraphTraversalSource g = traversal().withRemote(...); // 3 vertex additions in three individual requests/transactions: g.addV().iterate(); g.addV().iterate(); g.addV().iterate(); // 3 vertex additions in one single request/transaction: g.addV().addV().addV().iterate();

绑定到会话的请求

绑定到会话时,可以在单个事务的上下文中应用多个请求。

对于脚本来说,这意味着没有必要将所有图形操作连接成一个单一的嵌入式字符串值:

Cluster cluster = Cluster.open(); Client client = cluster.connect(sessionName); // session try { // 3 vertex additions in one request/transaction: client.submit("g.addV();g.addV();g.addV()").all().get(); } finally { client.close(); } try { // 3 vertex additions in three requests, but one transaction: client.submit("g.addV()").all().get(); // starts a new transaction with the same sessionName client.submit("g.addV()").all().get(); client.submit("g.addV()").all().get(); } finally { client.close(); }

对于字节码,之后 TinkerPop 3.5.x,可以显式控制事务并透明地管理会话。Gremlin 语言变体 (GLV) 支持 Gremlin 的tx()语法commit()或交易rollback(),如下所示:

GraphTraversalSource g = traversal().withRemote(conn); Transaction tx = g.tx(); // Spawn a GraphTraversalSource from the Transaction. // Traversals spawned from gtx are executed within a single transaction. GraphTraversalSource gtx = tx.begin(); try { gtx.addV('person').iterate(); gtx.addV('software').iterate(); tx.commit(); } finally { if (tx.isOpen()) { tx.rollback(); } }

尽管上面的示例是用 Java 编写的,但你也可以在 Python、Javascript 和.NET 中使用这种tx()语法。

警告

无会话只读查询在 SNAPSHO T 隔离下执行,但在显式事务中运行的只读查询在 SERIALIZABLE 隔离下执行。与SERIALIZABLE隔离下运行的只读查询不同,在隔离状态下执行的只读查询会产生更高的开销,并且可能会阻塞或被并行写入阻塞。SNAPSHOT