第 1 步:创建新的分类账 - 亚马逊 Quantum Ledger 数据库(亚马逊QLDB)

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

第 1 步:创建新的分类账

重要

终止支持通知:现有客户将能够使用亚马逊,QLDB直到 2025 年 7 月 31 日终止支持。有关更多详细信息,请参阅将亚马逊QLDB账本迁移到亚马逊 Aurora Postgr SQL e。

在此步骤中,您将创建一个名为的新 Amazon QLDB 账本vehicle-registration

创建新分类账
  1. 查看以下文件(Constants.ts),其包含本教程中所有其他程序使用的常量值。

    /* * 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. */ /** * Constant values used throughout this tutorial. */ export const LEDGER_NAME = "vehicle-registration"; export const LEDGER_NAME_WITH_TAGS = "tags"; export const DRIVERS_LICENSE_TABLE_NAME = "DriversLicense"; export const PERSON_TABLE_NAME = "Person"; export const VEHICLE_REGISTRATION_TABLE_NAME = "VehicleRegistration"; export const VEHICLE_TABLE_NAME = "Vehicle"; export const GOV_ID_INDEX_NAME = "GovId"; export const LICENSE_NUMBER_INDEX_NAME = "LicenseNumber"; export const LICENSE_PLATE_NUMBER_INDEX_NAME = "LicensePlateNumber"; export const PERSON_ID_INDEX_NAME = "PersonId"; export const VIN_INDEX_NAME = "VIN"; export const RETRY_LIMIT = 4; export const JOURNAL_EXPORT_S3_BUCKET_NAME_PREFIX = "qldb-tutorial-journal-export"; export const USER_TABLES = "information_schema.user_tables";
  2. 使用以下程序(CreateLedger.ts)创建名为 vehicle-registration 的分类账。

    /* * 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 { QLDB } from "aws-sdk"; import { CreateLedgerRequest, CreateLedgerResponse, DescribeLedgerRequest, DescribeLedgerResponse } from "aws-sdk/clients/qldb"; import { LEDGER_NAME } from "./qldb/Constants"; import { error, log } from "./qldb/LogUtil"; import { sleep } from "./qldb/Util"; const LEDGER_CREATION_POLL_PERIOD_MS = 10000; const ACTIVE_STATE = "ACTIVE"; /** * Create a new ledger with the specified name. * @param ledgerName Name of the ledger to be created. * @param qldbClient The QLDB control plane client to use. * @returns Promise which fulfills with a CreateLedgerResponse. */ export async function createLedger(ledgerName: string, qldbClient: QLDB): Promise<CreateLedgerResponse> { log(`Creating a ledger named: ${ledgerName}...`); const request: CreateLedgerRequest = { Name: ledgerName, PermissionsMode: "ALLOW_ALL" } const result: CreateLedgerResponse = await qldbClient.createLedger(request).promise(); log(`Success. Ledger state: ${result.State}.`); return result; } /** * Wait for the newly created ledger to become active. * @param ledgerName Name of the ledger to be checked on. * @param qldbClient The QLDB control plane client to use. * @returns Promise which fulfills with a DescribeLedgerResponse. */ export async function waitForActive(ledgerName: string, qldbClient: QLDB): Promise<DescribeLedgerResponse> { log(`Waiting for ledger ${ledgerName} to become active...`); const request: DescribeLedgerRequest = { Name: ledgerName } while (true) { const result: DescribeLedgerResponse = await qldbClient.describeLedger(request).promise(); if (result.State === ACTIVE_STATE) { log("Success. Ledger is active and ready to be used."); return result; } log("The ledger is still creating. Please wait..."); await sleep(LEDGER_CREATION_POLL_PERIOD_MS); } } /** * Create a ledger and wait for it to be active. * @returns Promise which fulfills with void. */ const main = async function(): Promise<void> { try { const qldbClient: QLDB = new QLDB(); await createLedger(LEDGER_NAME, qldbClient); await waitForActive(LEDGER_NAME, qldbClient); } catch (e) { error(`Unable to create the ledger: ${e}`); } } if (require.main === module) { main(); }
    注意
    • createLedger 调用中,您必须指定分类账名称和权限模式。我们强烈建议使用 STANDARD 权限模式来最大限度地提高分类账数据的安全性。

    • 创建分类账时,将默认启用 删除保护。这是QLDB一项防止任何用户删除账本的功能。您可以选择使用QLDBAPI或 AWS Command Line Interface (AWS CLI) 在创建账本时禁用删除保护。

    • 您还可选择指定要附加到分类账的标签。

  3. 要运行编译后的程序,请输入以下命令。

    node dist/CreateLedger.js

要验证您与新分类账的连接,请继续 第 2 步:测试与分类账的连接