Étape 6 : Afficher l'historique des révisions d'un document - Base de données Amazon Quantum Ledger (AmazonQLDB)

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

Étape 6 : Afficher l'historique des révisions d'un document

Important

Avis de fin de support : les clients existants pourront utiliser Amazon QLDB jusqu'à la fin du support le 31 juillet 2025. Pour plus de détails, consultez Migrer un Amazon QLDB Ledger vers Amazon Aurora SQL Postgre.

Après avoir modifié les données d'immatriculation d'un véhicule à l'étape précédente, vous pouvez consulter l'historique de tous ses propriétaires enregistrés et tout autre champ mis à jour. Au cours de cette étape, vous recherchez l'historique des révisions d'un document dans le VehicleRegistration tableau de votre vehicle-registration grand livre.

Pour consulter l'historique des révisions
  1. Passez en revue le programme suivant (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."); } }
    Note
    • Vous pouvez consulter l'historique des révisions d'un document en interrogeant la syntaxe intégrée Fonction d'historique ci-dessous.

      SELECT * FROM history( table_name [, `start-time` [, `end-time` ] ] ) AS h [ WHERE h.metadata.id = 'id' ]
    • L'heure de début et l'heure de fin sont toutes deux facultatives. Ce sont des valeurs littérales d'Amazon Ion qui peuvent être indiquées par des backticks (). `...` Pour en savoir plus, veuillez consulter la section Interroger Ion avec PartiQL sur Amazon QLDB.

    • Il est recommandé de qualifier une requête d'historique à la fois par une plage de dates (heure de début et heure de fin) et par un identifiant de document (metadata.id). QLDBtraite les SELECT requêtes dans les transactions, qui sont soumises à une limite de délai d'expiration des transactions.

      QLDBl'historique est indexé par numéro de document, et vous ne pouvez pas créer d'index d'historique supplémentaire pour le moment. Les requêtes d'historique qui incluent une heure de début et une heure de fin bénéficient de la qualification par plage de dates.

  2. Compilez et exécutez le QueryHistory.java programme pour interroger l'historique des révisions du VehicleRegistration document avec VIN1N4AL11D75C109151.

Pour vérifier cryptographiquement une révision de document dans le vehicle-registration registre, passez à. Étape 7 : Vérifier un document dans un registre