步驟 1:建立新分類帳 - Amazon Quantum 賬本數據庫(AmazonQLDB)

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

步驟 1:建立新分類帳

重要

支援結束通知:現有客戶將能夠使用 Amazon,QLDB直到 2025 年 7 月 31 日終止支援為止。有關更多詳細信息,請參閱將 Amazon QLDB 分類帳遷移到 Amazon Aurora 郵政. SQL

在此步驟中,您會建立名為的新 Amazon QLDB 分類帳vehicle-registration

若要建立新的分類帳
  1. 檢閱下列 file (constants.py),其中包含本自學課程中所有其他程式所使用的常數值。

    # 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. class Constants: """ Constant values used throughout this tutorial. """ LEDGER_NAME = "vehicle-registration" VEHICLE_REGISTRATION_TABLE_NAME = "VehicleRegistration" VEHICLE_TABLE_NAME = "Vehicle" PERSON_TABLE_NAME = "Person" DRIVERS_LICENSE_TABLE_NAME = "DriversLicense" LICENSE_NUMBER_INDEX_NAME = "LicenseNumber" GOV_ID_INDEX_NAME = "GovId" VEHICLE_VIN_INDEX_NAME = "VIN" LICENSE_PLATE_NUMBER_INDEX_NAME = "LicensePlateNumber" PERSON_ID_INDEX_NAME = "PersonId" JOURNAL_EXPORT_S3_BUCKET_NAME_PREFIX = "qldb-tutorial-journal-export" USER_TABLES = "information_schema.user_tables" S3_BUCKET_ARN_TEMPLATE = "arn:aws:s3:::" LEDGER_NAME_WITH_TAGS = "tags" RETRY_LIMIT = 4
  2. 使用下列程式 (create_ledger.py) 建立名為的分類帳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. # # This code expects that you have AWS credentials setup per: # https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html from logging import basicConfig, getLogger, INFO from time import sleep from boto3 import client from pyqldbsamples.constants import Constants logger = getLogger(__name__) basicConfig(level=INFO) qldb_client = client('qldb') LEDGER_CREATION_POLL_PERIOD_SEC = 10 ACTIVE_STATE = "ACTIVE" def create_ledger(name): """ Create a new ledger with the specified name. :type name: str :param name: Name for the ledger to be created. :rtype: dict :return: Result from the request. """ logger.info("Let's create the ledger named: {}...".format(name)) result = qldb_client.create_ledger(Name=name, PermissionsMode='ALLOW_ALL') logger.info('Success. Ledger state: {}.'.format(result.get('State'))) return result def wait_for_active(name): """ Wait for the newly created ledger to become active. :type name: str :param name: The ledger to check on. :rtype: dict :return: Result from the request. """ logger.info('Waiting for ledger to become active...') while True: result = qldb_client.describe_ledger(Name=name) if result.get('State') == ACTIVE_STATE: logger.info('Success. Ledger is active and ready to use.') return result logger.info('The ledger is still creating. Please wait...') sleep(LEDGER_CREATION_POLL_PERIOD_SEC) def main(ledger_name=Constants.LEDGER_NAME): """ Create a ledger and wait for it to be active. """ try: create_ledger(ledger_name) wait_for_active(ledger_name) except Exception as e: logger.exception('Unable to create the ledger!') raise e if __name__ == '__main__': main()
    注意
    • create_ledger通話中,您必須指定分類帳名稱和權限模式。我們建議您使用STANDARD權限模式來最大化分類帳資料的安全性。

    • 建立分類帳時,依預設會啟用刪除保護。這是中的一項功能QLDB,可防止任何使用者刪除分類帳。您可以選擇使用QLDBAPI或 AWS Command Line Interface (AWS CLI) 停用分類帳建立時的刪除保護。

    • 您也可以選擇指定要附加至分類帳的盤點單。

  3. 若要執行程式,請輸入下列命令。

    python create_ledger.py

若要驗證您與新分類帳的連線,請繼續執行步驟 2:測試與分類帳的連線