步骤 4:查询分类账中的表 - Amazon Quantum Ledger Database (Amazon QLDB)

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

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

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

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

在此步骤中,您将使用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:修改分类账中的文档