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

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

Neptune OpenPher 中的交易

Amazon Neptune 中的 OpenPher 实施使用Neptune 定义的交易语义但是,Bolt 驱动程序提供的隔离级别对 Bolt 事务语义有一些具体的影响,如以下各节所述。

只读 Bolt 交易查询

处理只读查询的方法有多种,具有不同的事务模型和隔离级别,如下所示:

隐式只读交易查询

以下是只读隐式事务的示例:

public void executeReadImplicitTransaction() { // end point final String END_POINT = "(End Point URL)"; // read query final String READ_QUERY = "MATCH (n) RETURN n limit 10"; // create the driver final Driver driver = GraphDatabase.driver(END_POINT, AuthTokens.none(), Config.builder().withEncryption() .withTrustStrategy(TrustStrategy.trustSystemCertificates()) .build()); // create the session config SessionConfig sessionConfig = SessionConfig.builder() .withFetchSize(1000) .withDefaultAccessMode(AccessMode.READ) .build(); // run the query as access mode read driver.session(sessionConfig).readTransaction(new TransactionWork<String>() { final StringBuilder resultCollector = new StringBuilder(); @Override public String execute(final Transaction tx) { // execute the query Result queryResult = tx.run(READ_QUERY); // Read the result for (Record record : queryResult.list()) { for (String key : record.keys()) { resultCollector.append(key) .append(":") .append(record.get(key).asNode().toString()); } } return resultCollector.toString(); } } ); // close the driver. driver.close(); }

由于只读副本只接受只读查询,因此无论会话配置中设置的访问模式如何,针对只读副本的所有查询都将作为只读隐式事务执行。Neptune 将读隐式交易评估为只读查询SNAPSHOT隔离语义。

在失败的情况下,默认情况下会重试读隐式事务。

自动提交只读事务查询

以下是只读自动提交事务的示例:

public void executeAutoCommitTransaction() { // end point final String END_POINT = "(End Point URL)"; // read query final String READ_QUERY = "MATCH (n) RETURN n limit 10"; // Create the session config. final SessionConfig sessionConfig = SessionConfig .builder() .withFetchSize(1000) .withDefaultAccessMode(AccessMode.READ) .build(); // create the driver final Driver driver = GraphDatabase.driver(END_POINT, AuthTokens.none(), Config.builder() .withEncryption() .withTrustStrategy(TrustStrategy.trustSystemCertificates()) .build()); // result collector final StringBuilder resultCollector = new StringBuilder(); // create a session final Session session = driver.session(sessionConfig); // run the query final Result queryResult = session.run(READ_QUERY); for (final Record record : queryResult.list()) { for (String key : record.keys()) { resultCollector.append(key) .append(":") .append(record.get(key).asNode().toString()); } } // close the session session.close(); // close the driver driver.close(); }

如果访问模式设置为READ在会话配置中,Neptune 将自动提交事务查询评估为只读查询SNAPSHOT隔离语义。请注意,只读副本只接受只读查询。

如果不传入会话配置,则默认情况下,自动提交查询将使用变异查询隔离进行处理,因此必须传入将访问模式明确设置为的会话配置READ.

在失败的情况下,不会重新尝试只读自动提交查询。

显式只读交易查询

以下是显式只读事务的示例:

public void executeReadExplicitTransaction() { // end point final String END_POINT = "(End Point URL)"; // read query final String READ_QUERY = "MATCH (n) RETURN n limit 10"; // Create the session config. final SessionConfig sessionConfig = SessionConfig .builder() .withFetchSize(1000) .withDefaultAccessMode(AccessMode.READ) .build(); // create the driver final Driver driver = GraphDatabase.driver(END_POINT, AuthTokens.none(), Config.builder() .withEncryption() .withTrustStrategy(TrustStrategy.trustSystemCertificates()) .build()); // result collector final StringBuilder resultCollector = new StringBuilder(); // create a session final Session session = driver.session(sessionConfig); // begin transaction final Transaction tx = session.beginTransaction(); // run the query on transaction final List<Record> list = tx.run(READ_QUERY).list(); // read the result for (final Record record : list) { for (String key : record.keys()) { resultCollector .append(key) .append(":") .append(record.get(key).asNode().toString()); } } // commit the transaction and for rollback we can use beginTransaction.rollback(); tx.commit(); // close the driver driver.close(); }

如果访问模式设置为READ在会话配置中,Neptune 将显式只读事务评估为只读查询SNAPSHOT隔离语义。请注意,只读副本只接受只读查询。

如果不传入会话配置,则默认情况下会使用变异查询隔离来处理显式只读事务,因此必须传入将访问模式明确设置为的会话配置READ.

在失败的情况下,默认情况下将重试只读显式查询。

突变 Bolt 交易查询

与只读查询一样,可以通过多种方式处理变异查询,具有不同的事务模型和隔离级别,如下所示:

隐式变异交易查询

下面是隐式突变交易示例:

public void executeWriteImplicitTransaction() { // end point final String END_POINT = "(End Point URL)"; // create node with label as label and properties. final String WRITE_QUERY = "CREATE (n:label {name : 'foo'})"; // Read the vertex created with label as label. final String READ_QUERY = "MATCH (n:label) RETURN n"; // create the driver final Driver driver = GraphDatabase.driver(END_POINT, AuthTokens.none(), Config.builder() .withEncryption() .withTrustStrategy(TrustStrategy.trustSystemCertificates()) .build()); // create the session config SessionConfig sessionConfig = SessionConfig .builder() .withFetchSize(1000) .withDefaultAccessMode(AccessMode.WRITE) .build(); final StringBuilder resultCollector = new StringBuilder(); // run the query as access mode write driver.session(sessionConfig).writeTransaction(new TransactionWork<String>() { @Override public String execute(final Transaction tx) { // execute the write query and consume the result. tx.run(WRITE_QUERY).consume(); // read the vertex written in the same transaction final List<Record> list = tx.run(READ_QUERY).list(); // read the result for (final Record record : list) { for (String key : record.keys()) { resultCollector .append(key) .append(":") .append(record.get(key).asNode().toString()); } } return resultCollector.toString(); } }); // at the end, the transaction is automatically committed. // close the driver. driver.close(); }

在变异查询中进行的读取将在下执行READ COMMITTED与通常的保证隔离Neptune 突变交易.

无论您是否具体传入会话配置,事务始终被视为写入事务。

有关冲突,请参阅使用锁定等待超时解决冲突.

自动提交变异事务查询

变异自动提交查询继承与变异隐式事务相同的行为。

如果没有传入会话配置,默认情况下,该事务将被视为写入事务。

在失败的情况下,不会自动重试变异自动提交查询。

显式变异交易查询

下面是一个显式突变交易示例:

public void executeWriteExplicitTransaction() { // end point final String END_POINT = "(End Point URL)"; // create node with label as label and properties. final String WRITE_QUERY = "CREATE (n:label {name : 'foo'})"; // Read the vertex created with label as label. final String READ_QUERY = "MATCH (n:label) RETURN n"; // create the driver final Driver driver = GraphDatabase.driver(END_POINT, AuthTokens.none(), Config.builder() .withEncryption() .withTrustStrategy(TrustStrategy.trustSystemCertificates()) .build()); // create the session config SessionConfig sessionConfig = SessionConfig .builder() .withFetchSize(1000) .withDefaultAccessMode(AccessMode.WRITE) .build(); final StringBuilder resultCollector = new StringBuilder(); final Session session = driver.session(sessionConfig); // run the query as access mode write final Transaction tx = driver.session(sessionConfig).beginTransaction(); // execute the write query and consume the result. tx.run(WRITE_QUERY).consume(); // read the result from the previous write query in a same transaction. final List<Record> list = tx.run(READ_QUERY).list(); // read the result for (final Record record : list) { for (String key : record.keys()) { resultCollector .append(key) .append(":") .append(record.get(key).asNode().toString()); } } // commit the transaction and for rollback we can use tx.rollback(); tx.commit(); // close the session session.close(); // close the driver. driver.close(); }

显式变异查询继承与隐式变异事务相同的行为。

如果没有传入会话配置,默认情况下,该事务将被视为写入事务。

有关冲突,请参阅使用锁定等待超时解决冲突.