本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
步驟 2:測試與分類帳的連線
重要
支援結束通知:現有客戶將能夠使用 Amazon,QLDB直到 2025 年 7 月 31 日終止支援為止。有關更多詳細信息,請參閱將 Amazon QLDB 分類帳遷移到 Amazon Aurora 郵政. SQL
在此步驟中,您確認可以QLDB使用交易資料API端點連接到 Amazon 中的vehicle-registration
分類帳。
若要測試與分類帳的連線
-
複查下列程式 (
ConnectToLedger.java
),此程式會建立與vehicle-registration
分類帳的資料階段作業連線。- 2.x
-
/* * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: MIT-0 * * Permission is hereby granted, free of charge, to any person obtaining a copy of this * software and associated documentation files (the "Software"), to deal in the Software * without restriction, including without limitation the rights to use, copy, modify, * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package software.amazon.qldb.tutorial; import java.net.URI; import java.net.URISyntaxException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; import software.amazon.awssdk.services.qldbsession.QldbSessionClient; import software.amazon.awssdk.services.qldbsession.QldbSessionClientBuilder; import software.amazon.qldb.QldbDriver; import software.amazon.qldb.RetryPolicy; /** * Connect to a session for a given ledger using default settings. * <p> * This code expects that you have AWS credentials setup per: * http://docs.aws.amazon.com/java-sdk/latest/developer-guide/setup-credentials.html */ public final class ConnectToLedger { public static final Logger log = LoggerFactory.getLogger(ConnectToLedger.class); public static AwsCredentialsProvider credentialsProvider; public static String endpoint = null; public static String ledgerName = Constants.LEDGER_NAME; public static String region = null; public static QldbDriver driver; private ConnectToLedger() { } /** * Create a pooled driver for creating sessions. * * @param retryAttempts How many times the transaction will be retried in * case of a retryable issue happens like Optimistic Concurrency Control exception, * server side failures or network issues. * @return The pooled driver for creating sessions. */ public static QldbDriver createQldbDriver(int retryAttempts) { QldbSessionClientBuilder builder = getAmazonQldbSessionClientBuilder(); return QldbDriver.builder() .ledger(ledgerName) .transactionRetryPolicy(RetryPolicy .builder() .maxRetries(retryAttempts) .build()) .sessionClientBuilder(builder) .build(); } /** * Create a pooled driver for creating sessions. * * @return The pooled driver for creating sessions. */ public static QldbDriver createQldbDriver() { QldbSessionClientBuilder builder = getAmazonQldbSessionClientBuilder(); return QldbDriver.builder() .ledger(ledgerName) .transactionRetryPolicy(RetryPolicy.builder() .maxRetries(Constants.RETRY_LIMIT).build()) .sessionClientBuilder(builder) .build(); } /** * Creates a QldbSession builder that is passed to the QldbDriver to connect to the Ledger. * * @return An instance of the AmazonQLDBSessionClientBuilder */ public static QldbSessionClientBuilder getAmazonQldbSessionClientBuilder() { QldbSessionClientBuilder builder = QldbSessionClient.builder(); if (null != endpoint && null != region) { try { builder.endpointOverride(new URI(endpoint)); } catch (URISyntaxException e) { throw new IllegalArgumentException(e); } } if (null != credentialsProvider) { builder.credentialsProvider(credentialsProvider); } return builder; } /** * Create a pooled driver for creating sessions. * * @return The pooled driver for creating sessions. */ public static QldbDriver getDriver() { if (driver == null) { driver = createQldbDriver(); } return driver; } public static void main(final String... args) { Iterable<String> tables = ConnectToLedger.getDriver().getTableNames(); log.info("Existing tables in the ledger:"); for (String table : tables) { log.info("- {} ", table); } } }
注意
-
若要在分類帳上執行資料作業,您必須建立
QldbDriver
類別的執行環境,以連線至特定分類帳。這與您在上一個步驟中用來建立分類帳的用AmazonQLDB
戶端不同的用戶端物件。先前的用戶端僅用於執行中列出的管理API作業Amazon QLDB API 參考。 -
首先,創建一個
QldbDriver
對象。建立此動因時,您必須指定分類帳名稱。然後,您可以使用此驅動程式的
execute
方法來執行 PartiQL 陳述式。 -
您可以選擇性地指定異動例外的重試次數上限。該
execute
方法會自動重試樂觀並發控制(OCC)衝突和其他常見的瞬態異常,直到這個可配置的限制。預設值為4
。如果達到限制後交易仍然失敗,則驅動程序拋出異常。如需進一步了解,請參閱了解 Amazon 中的驅動程序的重試政策 QLDB。
-
- 1.x
-
/* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: MIT-0 * * Permission is hereby granted, free of charge, to any person obtaining a copy of this * software and associated documentation files (the "Software"), to deal in the Software * without restriction, including without limitation the rights to use, copy, modify, * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package software.amazon.qldb.tutorial; import com.amazonaws.auth.AWSCredentialsProvider; import com.amazonaws.client.builder.AwsClientBuilder; import com.amazonaws.services.qldbsession.AmazonQLDBSessionClientBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.qldb.PooledQldbDriver; import software.amazon.qldb.QldbDriver; import software.amazon.qldb.QldbSession; import software.amazon.qldb.exceptions.QldbClientException; /** * Connect to a session for a given ledger using default settings. * <p> * This code expects that you have AWS credentials setup per: * http://docs.aws.amazon.com/java-sdk/latest/developer-guide/setup-credentials.html */ public final class ConnectToLedger { public static final Logger log = LoggerFactory.getLogger(ConnectToLedger.class); public static AWSCredentialsProvider credentialsProvider; public static String endpoint = null; public static String ledgerName = Constants.LEDGER_NAME; public static String region = null; private static PooledQldbDriver driver; private ConnectToLedger() { } /** * Create a pooled driver for creating sessions. * * @return The pooled driver for creating sessions. */ public static PooledQldbDriver createQldbDriver() { AmazonQLDBSessionClientBuilder builder = AmazonQLDBSessionClientBuilder.standard(); if (null != endpoint && null != region) { builder.setEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, region)); } if (null != credentialsProvider) { builder.setCredentials(credentialsProvider); } return PooledQldbDriver.builder() .withLedger(ledgerName) .withRetryLimit(Constants.RETRY_LIMIT) .withSessionClientBuilder(builder) .build(); } /** * Create a pooled driver for creating sessions. * * @return The pooled driver for creating sessions. */ public static PooledQldbDriver getDriver() { if (driver == null) { driver = createQldbDriver(); } return driver; } /** * Connect to a ledger through a {@link QldbDriver}. * * @return {@link QldbSession}. */ public static QldbSession createQldbSession() { return getDriver().getSession(); } public static void main(final String... args) { try (QldbSession qldbSession = createQldbSession()) { log.info("Listing table names "); for (String tableName : qldbSession.getTableNames()) { log.info(tableName); } } catch (QldbClientException e) { log.error("Unable to create session.", e); } } }
注意
-
若要在分類帳上執行資料作業,您必須建立
PooledQldbDriver
或QldbDriver
類別的執行環境,以連線至特定分類帳。這與您在上一個步驟中用來建立分類帳的用AmazonQLDB
戶端不同的用戶端物件。先前的用戶端僅用於執行中列出的管理API作業Amazon QLDB API 參考。我們建議您使用,
PooledQldbDriver
除非您需要使用QldbDriver
. 的預設集區大小PooledQldbDriver
是工作階段用戶端允許的開啟HTTP連線數目上限。 -
首先,創建一個
PooledQldbDriver
對象。建立此動因時,您必須指定分類帳名稱。然後,您可以使用此驅動程式的
execute
方法來執行 PartiQL 陳述式。或者,您可以從此池驅動程序對象手動創建會話,並使用會話的execute
方法。會話表示與分類帳的單個連接。 -
您可以選擇性地指定異動例外的重試次數上限。該
execute
方法會自動重試樂觀並發控制(OCC)衝突和其他常見的瞬態異常,直到這個可配置的限制。預設值為4
。如果達到限制後交易仍然失敗,則驅動程序拋出異常。如需進一步了解,請參閱了解 Amazon 中的驅動程序的重試政策 QLDB。
-
-
編譯並執行
ConnectToLedger.java
程式,以測試資料階段作業與vehicle-registration
分類帳的連線。
覆寫 AWS 區域
範例應用程式會連線到QLDB預設值 AWS 區域,您可以按照先決條件步驟中的說明進行設定設定您的預設 AWS 憑證和區域。您還可以通過修改QLDB會話客戶端構建器屬性來更改區域。
- 2.x
-
下列程式碼範例會實體化新
QldbSessionClientBuilder
物件。import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.qldbsession.QldbSessionClientBuilder; // This client builder will default to US East (Ohio) QldbSessionClientBuilder builder = QldbSessionClient.builder() .region(Region.US_EAST_2);
您可以使用該
region
方法QLDB在任何可用的區域中運行代碼。如需完整清 AmazonQLDB,請參閱 AWS 一般參考. - 1.x
-
下列程式碼範例會實體化新
AmazonQLDBSessionClientBuilder
物件。import com.amazonaws.regions.Regions; import com.amazonaws.services.qldbsession.AmazonQLDBSessionClientBuilder; // This client builder will default to US East (Ohio) AmazonQLDBSessionClientBuilder builder = AmazonQLDBSessionClientBuilder.standard() .withRegion(Regions.US_EAST_2);
您可以使用該
withRegion
方法QLDB在任何可用的區域中運行代碼。如需完整清 AmazonQLDB,請參閱 AWS 一般參考.
若要在vehicle-registration
分類帳中建立表格,請繼續執行步驟 3:建立資料表、索引和範例資料。