步骤 4:查询分类账中的表 - 亚马逊 Quantum Ledger 数据库(亚马逊QLDB)

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

步骤 4:查询分类账中的表

重要

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

在 Amazon QLDB 账本中创建表格并加载数据后,您可以运行查询来查看刚刚插入的车辆登记数据。QLDB使用 P artiQL 作为其查询语言,使用 A mazon Ion 作为其面向文档的数据模型。

PartiQL 是一种开源、SQL兼容的查询语言,现已扩展为可与 Ion 配合使用。使用 PartiQL,您可以使用熟悉SQL的运算符插入、查询和管理数据。Amazon Ion 是... 的超集。JSONIon 是基于文档的开源数据格式,可让您灵活地存储和处理结构化、半结构化和嵌套数据。

在此步骤中,您将使用 SELECT 语句从 vehicle-registration分类账中的表中读取数据。

警告

当您在没有索引查询QLDB的情况下在中运行查询时,它会调用全表扫描。PartiQL 之所以支持此类查询,是因为它是兼容的。SQL但是,不要在中对生产用例运行表扫描QLDB。表扫描可能会导致大型表出现性能问题,包括并发冲突与事务超时。

为避免表扫描,必须在索引字段或文档 ID 上使用相等运算符(WHERE indexedField = 123WHERE indexedField IN (456, 789))运行带有WHERE谓词子句的语句。有关更多信息,请参阅 优化查询性能

查询表格
  • 编译并运行以下程序(FindVehicles.java),以查询分类账中某人名下注册的所有车辆。

    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. */ package software.amazon.qldb.tutorial; import java.io.IOException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.amazon.ion.IonValue; import software.amazon.qldb.Result; import software.amazon.qldb.TransactionExecutor; import software.amazon.qldb.tutorial.model.Person; import software.amazon.qldb.tutorial.model.SampleData; /** * Find all vehicles registered under a person. * * This code expects that you have AWS credentials setup per: * http://docs.aws.amazon.com/java-sdk/latest/developer-guide/setup-credentials.html */ public final class FindVehicles { public static final Logger log = LoggerFactory.getLogger(FindVehicles.class); private FindVehicles() { } /** * Find vehicles registered under a driver using their government ID. * * @param txn * The {@link TransactionExecutor} for lambda execute. * @param govId * The government ID of the owner. * @throws IllegalStateException if failed to convert parameters into {@link IonValue}. */ public static void findVehiclesForOwner(final TransactionExecutor txn, final String govId) { try { final String documentId = Person.getDocumentIdByGovId(txn, govId); final String query = "SELECT v FROM Vehicle AS v INNER JOIN VehicleRegistration AS r " + "ON v.VIN = r.VIN WHERE r.Owners.PrimaryOwner.PersonId = ?"; final Result result = txn.execute(query, Constants.MAPPER.writeValueAsIonValue(documentId)); log.info("List of Vehicles for owner with GovId: {}...", govId); ScanTable.printDocuments(result); } catch (IOException ioe) { throw new IllegalStateException(ioe); } } public static void main(final String... args) { final Person person = SampleData.PEOPLE.get(0); ConnectToLedger.getDriver().execute(txn -> { findVehiclesForOwner(txn, person.getGovId()); }); } }
    1.x
    /* * Copyright 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. */ package software.amazon.qldb.tutorial; import java.io.IOException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.amazon.ion.IonValue; import software.amazon.qldb.Result; import software.amazon.qldb.TransactionExecutor; import software.amazon.qldb.tutorial.model.Person; import software.amazon.qldb.tutorial.model.SampleData; /** * Find all vehicles registered under a person. * * This code expects that you have AWS credentials setup per: * http://docs.aws.amazon.com/java-sdk/latest/developer-guide/setup-credentials.html */ public final class FindVehicles { public static final Logger log = LoggerFactory.getLogger(FindVehicles.class); private FindVehicles() { } /** * Find vehicles registered under a driver using their government ID. * * @param txn * The {@link TransactionExecutor} for lambda execute. * @param govId * The government ID of the owner. * @throws IllegalStateException if failed to convert parameters into {@link IonValue}. */ public static void findVehiclesForOwner(final TransactionExecutor txn, final String govId) { try { final String documentId = Person.getDocumentIdByGovId(txn, govId); final String query = "SELECT v FROM Vehicle AS v INNER JOIN VehicleRegistration AS r " + "ON v.VIN = r.VIN WHERE r.Owners.PrimaryOwner.PersonId = ?"; final Result result = txn.execute(query, Constants.MAPPER.writeValueAsIonValue(documentId)); log.info("List of Vehicles for owner with GovId: {}...", govId); ScanTable.printDocuments(result); } catch (IOException ioe) { throw new IllegalStateException(ioe); } } public static void main(final String... args) { final Person person = SampleData.PEOPLE.get(0); ConnectToLedger.getDriver().execute(txn -> { findVehiclesForOwner(txn, person.getGovId()); }, (retryAttempt) -> log.info("Retrying due to OCC conflict...")); } }
    注意

    首先,该程序在 Person 表格中查询文档GovId LEWISR261LL,以获取其id元数据字段。

    然后,它使用文档id,通过PrimaryOwner.PersonId作为外键查询VehicleRegistration表。它还结合VIN字段上的VehicleVehicleRegistration

要了解如何修改 vehicle-registration 分类账表格中的文档,请参阅 第 5 步:修改分类账中的文档