步驟 4:查詢分類帳中的表格 - Amazon Quantum Ledger 資料庫 (Amazon QLDB)

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

步驟 4:查詢分類帳中的表格

重要

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

在 Amazon QLDB 分類帳中建立表格並載入資料後,您可以執行查詢以檢閱剛插入的車輛註冊資料。QLDB使用 PartiQL 作為其查詢語言,Amazon 離子作為其文檔導向的數據模型。

PartiQL 是一種開放原始碼、SQL兼容的查詢語言,已擴展為與 Ion 一起使用。使用 PartiQL,您可以使用熟悉的SQL運算子插入、查詢和管理資料。Amazon 離子是一個超集. JSON Ion 是一種開放原始碼、以文件為基礎的資料格式,可讓您靈活地儲存和處理結構化、半結構化和巢狀資料。

在此步驟中,您可以使用SELECT陳述式從vehicle-registration分類帳中的表格讀取資料。

警告

當您在中執行查詢時QLDB沒有索引查閱,它會叫用完整資料表掃描。PartiQL 支持這樣的查詢,因為它是SQL兼容的。但是,請勿針對中的生產使用案例執行表格掃描QLDB。資料表掃描可能會造成大型資料表的效能問題,包括並行衝突和交易逾時。

若要避免資料表掃描,您必須在索引欄位或文件 ID 上使用相等運算子來執行具有述WHERE詞子句的陳述式;例如,WHERE indexedField = 123WHERE indexedField IN (456, 789)。如需詳細資訊,請參閱 最佳化查詢效能

若要查詢表格
  1. 使用以下程序(find_vehicles.py)查詢在分類帳中以人員註冊的所有車輛。

    3.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. # # 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 pyqldbsamples.model.sample_data import get_document_ids, print_result, SampleData from pyqldbsamples.constants import Constants from pyqldbsamples.connect_to_ledger import create_qldb_driver logger = getLogger(__name__) basicConfig(level=INFO) def find_vehicles_for_owner(driver, gov_id): """ Find vehicles registered under a driver using their government ID. :type driver: :py:class:`pyqldb.driver.qldb_driver.QldbDriver` :param driver: An instance of the QldbDriver class. :type gov_id: str :param gov_id: The owner's government ID. """ document_ids = driver.execute_lambda(lambda executor: get_document_ids(executor, Constants.PERSON_TABLE_NAME, 'GovId', gov_id)) query = "SELECT Vehicle FROM Vehicle INNER JOIN VehicleRegistration AS r " \ "ON Vehicle.VIN = r.VIN WHERE r.Owners.PrimaryOwner.PersonId = ?" for ids in document_ids: cursor = driver.execute_lambda(lambda executor: executor.execute_statement(query, ids)) logger.info('List of Vehicles for owner with GovId: {}...'.format(gov_id)) print_result(cursor) def main(ledger_name=Constants.LEDGER_NAME): """ Find all vehicles registered under a person. """ try: with create_qldb_driver(ledger_name) as driver: # Find all vehicles registered under a person. gov_id = SampleData.PERSON[0]['GovId'] find_vehicles_for_owner(driver, gov_id) except Exception as e: logger.exception('Error getting vehicles for owner.') raise e if __name__ == '__main__': main()
    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. # # 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 pyqldbsamples.model.sample_data import get_document_ids, print_result, SampleData from pyqldbsamples.constants import Constants from pyqldbsamples.connect_to_ledger import create_qldb_session logger = getLogger(__name__) basicConfig(level=INFO) def find_vehicles_for_owner(transaction_executor, gov_id): """ Find vehicles registered under a driver using their government ID. :type transaction_executor: :py:class:`pyqldb.execution.executor.Executor` :param transaction_executor: An Executor object allowing for execution of statements within a transaction. :type gov_id: str :param gov_id: The owner's government ID. """ document_ids = get_document_ids(transaction_executor, Constants.PERSON_TABLE_NAME, 'GovId', gov_id) query = "SELECT Vehicle FROM Vehicle INNER JOIN VehicleRegistration AS r " \ "ON Vehicle.VIN = r.VIN WHERE r.Owners.PrimaryOwner.PersonId = ?" for ids in document_ids: cursor = transaction_executor.execute_statement(query, ids) logger.info('List of Vehicles for owner with GovId: {}...'.format(gov_id)) print_result(cursor) if __name__ == '__main__': """ Find all vehicles registered under a person. """ try: with create_qldb_session() as session: # Find all vehicles registered under a person. gov_id = SampleData.PERSON[0]['GovId'] session.execute_lambda(lambda executor: find_vehicles_for_owner(executor, gov_id), lambda retry_attempt: logger.info('Retrying due to OCC conflict...')) except Exception: logger.exception('Error getting vehicles for owner.')
    注意

    首先,這個程Person序查詢與GovId LEWISR261LL獲取其id元數據字段的文檔表。

    然後,它使用此文檔id作為外鍵來查詢VehicleRegistrationPrimaryOwner.PersonId。它還VehicleRegistrationVIN字段上的Vehicle表格連接。

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

    python find_vehicles.py

若要瞭解如何在vehicle-registration分類帳中修改表格中的文件,請參閱步驟 5:修改分類帳中的文件