步骤 6:查看文档修订历史记录 - Amazon Quantum Ledger Database (Amazon QLDB)

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

步骤 6:查看文档修订历史记录

在上一步中修改车辆注册数据后,您可查询其所有注册车主的历史记录以及任何其他更新的字段。在此步骤中,您将在vehicle-registration分类账的 VehicleRegistration表格中查询文档的修订历史记录。

若要查看修订历史记录
  1. 查看以下程序 (QueryHistory.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 java.time.Instant; import java.time.temporal.ChronoUnit; 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.SampleData; import software.amazon.qldb.tutorial.model.VehicleRegistration; /** * Query a table's history for a particular set of documents. * * 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 QueryHistory { public static final Logger log = LoggerFactory.getLogger(QueryHistory.class); private static final int THREE_MONTHS = 90; private QueryHistory() { } /** * In this example, query the 'VehicleRegistration' history table to find all previous primary owners for a VIN. * * @param txn * The {@link TransactionExecutor} for lambda execute. * @param vin * VIN to find previous primary owners for. * @param query * The query to find previous primary owners. * @throws IllegalStateException if failed to convert document ID to an {@link IonValue}. */ public static void previousPrimaryOwners(final TransactionExecutor txn, final String vin, final String query) { try { final String docId = VehicleRegistration.getDocumentIdByVin(txn, vin); log.info("Querying the 'VehicleRegistration' table's history using VIN: {}...", vin); final Result result = txn.execute(query, Constants.MAPPER.writeValueAsIonValue(docId)); ScanTable.printDocuments(result); } catch (IOException ioe) { throw new IllegalStateException(ioe); } } public static void main(final String... args) { final String threeMonthsAgo = Instant.now().minus(THREE_MONTHS, ChronoUnit.DAYS).toString(); final String query = String.format("SELECT data.Owners.PrimaryOwner, metadata.version " + "FROM history(VehicleRegistration, `%s`) " + "AS h WHERE h.metadata.id = ?", threeMonthsAgo); ConnectToLedger.getDriver().execute(txn -> { final String vin = SampleData.VEHICLES.get(0).getVin(); previousPrimaryOwners(txn, vin, query); }); log.info("Successfully queried history."); } }
    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 com.amazon.ion.IonValue; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.qldb.QldbSession; import software.amazon.qldb.Result; import software.amazon.qldb.TransactionExecutor; import software.amazon.qldb.tutorial.model.SampleData; import software.amazon.qldb.tutorial.model.VehicleRegistration; import java.io.IOException; import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.Collections; import java.util.List; /** * Query a table's history for a particular set of documents. * * 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 QueryHistory { public static final Logger log = LoggerFactory.getLogger(QueryHistory.class); private static final int THREE_MONTHS = 90; private QueryHistory() { } /** * In this example, query the 'VehicleRegistration' history table to find all previous primary owners for a VIN. * * @param txn * The {@link TransactionExecutor} for lambda execute. * @param vin * VIN to find previous primary owners for. * @param query * The query to find previous primary owners. * @throws IllegalStateException if failed to convert document ID to an {@link IonValue}. */ public static void previousPrimaryOwners(final TransactionExecutor txn, final String vin, final String query) { try { final String docId = VehicleRegistration.getDocumentIdByVin(txn, vin); final List<IonValue> parameters = Collections.singletonList(Constants.MAPPER.writeValueAsIonValue(docId)); log.info("Querying the 'VehicleRegistration' table's history using VIN: {}...", vin); final Result result = txn.execute(query, parameters); ScanTable.printDocuments(result); } catch (IOException ioe) { throw new IllegalStateException(ioe); } } public static void main(final String... args) { final String threeMonthsAgo = Instant.now().minus(THREE_MONTHS, ChronoUnit.DAYS).toString(); final String query = String.format("SELECT data.Owners.PrimaryOwner, metadata.version " + "FROM history(VehicleRegistration, `%s`) " + "AS h WHERE h.metadata.id = ?", threeMonthsAgo); ConnectToLedger.getDriver().execute(txn -> { final String vin = SampleData.VEHICLES.get(0).getVin(); previousPrimaryOwners(txn, vin, query); }, (retryAttempt) -> log.info("Retrying due to OCC conflict...")); log.info("Successfully queried history."); } }
    注意
    • 您可以通过以下语法查询内置版本,以查看文档历史记录函数的修订历史记录。

      SELECT * FROM history( table_name [, `start-time` [, `end-time` ] ] ) AS h [ WHERE h.metadata.id = 'id' ]
    • 开始时间结束时间均为可选。它们是 Amazon Ion 的字面值,可以用反引号 (`...`) 表示。要了解更多信息,请参阅 在 Amazon QLDB 中使用 PartiQL 查询 Ion

    • 最佳做法是,使用日期范围(开始时间结束时间)和文档 ID (metadata.id) 来限定历史查询。QLDB 处理事务中的SELECT查询,这些查询受事务超时限制的约束。

      QLDB 历史记录按文档 ID 编制索引,目前无法创建其他历史索引。包含开始时间和结束时间的历史记录查询将从日期范围限定中获得便利。

  2. 编译并运行 QueryHistory.java 程序,以使用 VIN 1N4AL11D75C109151查询VehicleRegistration文档的修订历史记录。

要以加密方式验证 vehicle-registration 分类账中的文档修订版,请继续第 7 步:验证分类账中的文档