本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
第 2 步:测试与分类账的连接
重要
终止支持通知:现有客户将能够使用亚马逊,QLDB直到 2025 年 7 月 31 日终止支持。有关更多详细信息,请参阅将亚马逊QLDB账本迁移到亚马逊 Aurora Postgr SQL
在此步骤中,您将验证是否可以使用交易数据API终端节点连接到 Amazon QLDB 中的vehicle-registration
账本。
测试与分类账的连接
-
使用以下程序(
ConnectToLedger.ts
),该程序创建了与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. */ import { QldbDriver, RetryConfig } from "amazon-qldb-driver-nodejs"; import { ClientConfiguration } from "aws-sdk/clients/qldbsession"; import { LEDGER_NAME } from "./qldb/Constants"; import { error, log } from "./qldb/LogUtil"; const qldbDriver: QldbDriver = createQldbDriver(); /** * Create a driver for creating sessions. * @param ledgerName The name of the ledger to create the driver on. * @param serviceConfigurationOptions The configurations for the AWS SDK client that the driver uses. * @returns The driver for creating sessions. */ export function createQldbDriver( ledgerName: string = LEDGER_NAME, serviceConfigurationOptions: ClientConfiguration = {} ): QldbDriver { const retryLimit = 4; const maxConcurrentTransactions = 10; //Use driver's default backoff function (and hence, no second parameter provided to RetryConfig) const retryConfig: RetryConfig = new RetryConfig(retryLimit); const qldbDriver: QldbDriver = new QldbDriver(ledgerName, serviceConfigurationOptions, maxConcurrentTransactions, retryConfig); return qldbDriver; } export function getQldbDriver(): QldbDriver { return qldbDriver; } /** * Connect to a session for a given ledger using default settings. * @returns Promise which fulfills with void. */ const main = async function(): Promise<void> { try { log("Listing table names..."); const tableNames: string[] = await qldbDriver.getTableNames(); tableNames.forEach((tableName: string): void => { log(tableName); }); } catch (e) { error(`Unable to create session: ${e}`); } } if (require.main === module) { main(); }
- 1.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. */ import { QldbDriver } from "amazon-qldb-driver-nodejs"; import { ClientConfiguration } from "aws-sdk/clients/qldbsession"; import { LEDGER_NAME } from "./qldb/Constants"; import { error, log } from "./qldb/LogUtil"; const qldbDriver: QldbDriver = createQldbDriver(); /** * Create a driver for creating sessions. * @param ledgerName The name of the ledger to create the driver on. * @param serviceConfigurationOptions The configurations for the AWS SDK client that the driver uses. * @returns The driver for creating sessions. */ export function createQldbDriver( ledgerName: string = LEDGER_NAME, serviceConfigurationOptions: ClientConfiguration = {} ): QldbDriver { const qldbDriver: QldbDriver = new QldbDriver(ledgerName, serviceConfigurationOptions); return qldbDriver; } export function getQldbDriver(): QldbDriver { return qldbDriver; } /** * Connect to a session for a given ledger using default settings. * @returns Promise which fulfills with void. */ var main = async function(): Promise<void> { try { log("Listing table names..."); const tableNames: string[] = await qldbDriver.getTableNames(); tableNames.forEach((tableName: string): void => { log(tableName); }); } catch (e) { error(`Unable to create session: ${e}`); } } if (require.main === module) { main(); }
注意
要在账本上运行数据交易,您必须创建一个QLDB驱动程序对象以连接到指定的账本。这与您在 上一步 中创建分类账时使用的
qldbClient
客户端对象不同。之前的客户机仅用于运行中列出的管理API操作亚马逊QLDBAPI参考资料。 -
要运行编译后的程序,请输入以下命令。
node dist/ConnectToLedger.js
要在 vehicle-registration
分类账中创建表,请继续 步骤 3:创建表、索引与示例数据。
第 1 步:创建新的分类账
步骤 3:创建表、索引与示例数据