Neptune 中的 Gremlin 交易 - Amazon Neptune

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

Neptune 中的 Gremlin 交易

有數個內容,Gremlin 交易會在其中執行。使用 Gremlin 時,務必了解您正在其中運作的內容以及其含義:

  • Script-based – 請求是使用文字型 Gremlin 字串提出的,如下所示:

    • 使用 Java 驅動程式和 Client.submit(string)

    • 使用 Gremlin 主控台和 :remote connect

    • 使用 HTTP API。

  • Bytecode-based – 請求是使用 Girmlin 語言變體 (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) 支援 commit()rollback() 交易的 Gremlin tx() 語法,如下所示:

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() 語法。

警告

無工作階段唯讀查詢是在 SNAPSHOT 隔離下執行,但是在明確交易內執行的唯讀查詢是在 SERIALIZABLE 隔離下執行。在 SERIALIZABLE 隔離下執行的唯讀查詢會產生更高的負荷,並且可能會封鎖並行寫入或遭其封鎖,這與在 SNAPSHOT 隔離下執行的唯讀查詢不同。