步驟 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' ]
    • 始時間結束時間都是可選的。它們是亞馬遜離子字面值,可以用反引號(`...`)表示。如需進一步了解,請參閱 在亞馬遜 QLDB 中使用 PartiQL 查詢離子

    • 最佳作法是使用日期範圍 (開始時間和結束時間) 和文件 ID (metadata.id) 來限定歷史查詢。QLDB 處理交易中的SELECT查詢,這些交易受到交易逾時限制

      QLDB 歷程記錄會依文件 ID 編製索引,而且您目前無法建立其他歷程記錄索引。包含開始時間和結束時間的歷史查詢可獲得日期範圍限定的好處。

  2. 編譯並運行該QueryHistory.java程序以使用 VIN 查詢VehicleRegistration文檔的修訂歷史記錄1N4AL11D75C109151

若要以密碼編譯方式驗證vehicle-registration分類帳中的文件版次,請繼續執行步驟 7:驗證分類帳中的文件