本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
步骤 4:查询分类账中的表
重要
终止支持通知:现有客户将能够使用 Amazon QLDB,直到 2025 年 7 月 31 日终止支持。有关更多详细信息,请参阅将亚马逊 QLDB 账本迁移到亚马逊 Aurora PostgreSQL
在 Amazon QLDB 分类账中创建表格和加载数据后,您可运行查询以查看刚刚插入的车辆登记数据。QLDB 使用 PartiQL作为其查询语言,使用 Amazon Ion作为其面向文档的数据模型。
PartiQL 是开源、与 SQL 兼容的查询语言,现已扩展为可与 Ion 配合使用。使用 PartiQL,您可使用熟悉的 SQL 运算符插入、查询和管理数据。Amazon Ion 是 JSON 的超集。Ion 是基于文档的开源数据格式,可让您灵活地存储和处理结构化、半结构化和嵌套数据。
在此步骤中,您将使用 SELECT
语句从 vehicle-registration
分类账中的表中读取数据。
警告
当您在没有索引查找的情况下运行查询时,它会调用全表扫描。PartiQL 之所以支持此类查询,是因为其与 SQL 兼容。但是,切勿在 QLDB 中对生产用例运行表扫描。表扫描可能会导致大型表出现性能问题,包括并发冲突与事务超时。
为避免表扫描,必须在索引字段或文档 ID 上使用相等运算符(WHERE indexedField = 123
或WHERE indexedField IN (456, 789)
)运行带有WHERE
谓词子句的语句。有关更多信息,请参阅 优化查询性能。
查询表格
-
使用以下程序(
FindVehicles.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. */ import { QldbDriver, Result, TransactionExecutor } from "amazon-qldb-driver-nodejs"; import { dom } from "ion-js"; import { getQldbDriver } from "./ConnectToLedger"; import { PERSON } from "./model/SampleData"; import { PERSON_TABLE_NAME } from "./qldb/Constants"; import { error, log } from "./qldb/LogUtil"; import { getDocumentId } from "./qldb/Util"; import { prettyPrintResultList } from "./ScanTable"; /** * Query 'Vehicle' and 'VehicleRegistration' tables using a unique document ID in one transaction. * @param txn The {@linkcode TransactionExecutor} for lambda execute. * @param govId The owner's government ID. * @returns Promise which fulfills with void. */ async function findVehiclesForOwner(txn: TransactionExecutor, govId: string): Promise<void> { const documentId: string = await getDocumentId(txn, PERSON_TABLE_NAME, "GovId", govId); const query: string = "SELECT Vehicle FROM Vehicle INNER JOIN VehicleRegistration AS r " + "ON Vehicle.VIN = r.VIN WHERE r.Owners.PrimaryOwner.PersonId = ?"; await txn.execute(query, documentId).then((result: Result) => { const resultList: dom.Value[] = result.getResultList(); log(`List of vehicles for owner with GovId: ${govId}`); prettyPrintResultList(resultList); }); } /** * Find all vehicles registered under a person. * @returns Promise which fulfills with void. */ const main = async function(): Promise<void> { try { const qldbDriver: QldbDriver = getQldbDriver(); await qldbDriver.executeLambda(async (txn: TransactionExecutor) => { await findVehiclesForOwner(txn, PERSON[0].GovId); }); } catch (e) { error(`Error getting vehicles for owner: ${e}`); } } if (require.main === module) { main(); }
注意
首先,该程序在
Person
表格中查询文档GovId LEWISR261LL
,以获取其id
元数据字段。然后,它使用文档
id
,通过PrimaryOwner.PersonId
作为外键查询VehicleRegistration
表。它还结合VIN
字段上的Vehicle
表的VehicleRegistration
。 -
要运行编译后的程序,请输入以下命令。
node dist/FindVehicles.js
要了解如何修改 vehicle-registration
分类账表格中的文档,请参阅 第 5 步:修改分类账中的文档。