Passaggio 4: interrogare le tabelle in un libro mastro - Database Amazon Quantum Ledger (Amazon) QLDB

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Passaggio 4: interrogare le tabelle in un libro mastro

Importante

Avviso di fine del supporto: i clienti esistenti potranno utilizzare Amazon QLDB fino alla fine del supporto il 31/07/2025. Per ulteriori dettagli, consulta Migrare un Amazon QLDB Ledger ad Amazon Aurora Postgre. SQL

Dopo aver creato tabelle in un QLDB registro Amazon e averle caricate con i dati, puoi eseguire query per rivedere i dati di immatricolazione dei veicoli che hai appena inserito. QLDButilizza PartiQL come linguaggio di interrogazione e Amazon Ion come modello di dati orientato ai documenti.

PartiQL è un linguaggio di query open source e SQL compatibile che è stato esteso per funzionare con Ion. Con PartiQL, puoi inserire, interrogare e gestire i tuoi dati con operatori familiariSQL. Amazon Ion è un superset di. JSON Ion è un formato di dati open source basato su documenti che offre la flessibilità di archiviazione ed elaborazione di dati strutturati, semistrutturati e annidati.

In questo passaggio, si utilizzano SELECT le istruzioni per leggere i dati dalle tabelle del registro. vehicle-registration

avvertimento

Quando si esegue una query QLDB senza una ricerca indicizzata, viene richiamata una scansione completa della tabella. PartiQL supporta tali interrogazioni perché è compatibile. SQL Tuttavia, non eseguite scansioni di tabelle per casi d'uso di produzione in. QLDB Le scansioni delle tabelle possono causare problemi di prestazioni su tabelle di grandi dimensioni, inclusi conflitti di concorrenza e timeout delle transazioni.

Per evitare le scansioni delle tabelle, è necessario eseguire istruzioni con una clausola di WHERE predicato utilizzando un operatore di uguaglianza su un campo indicizzato o un ID di documento; ad esempio, o. WHERE indexedField = 123 WHERE indexedField IN (456, 789) Per ulteriori informazioni, consulta Ottimizzazione delle prestazioni delle query.

Per interrogare le tabelle
  • Compila ed esegui il seguente programma (FindVehicles.java) per interrogare tutti i veicoli registrati con una persona nel tuo registro.

    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...")); } }
    Nota

    Innanzitutto, questo programma interroga la Person tabella del documento per ottenere il relativo campo GovId LEWISR261LL di metadati. id

    Quindi, utilizza questo documento id come chiave esterna per interrogare la VehicleRegistration tabella. PrimaryOwner.PersonId Inoltre si unisce VehicleRegistration alla Vehicle tabella sul VIN campo.

Per ulteriori informazioni sulla modifica dei documenti nelle tabelle del vehicle-registration registro, vedere. Fase 5: Modificare i documenti in un libro mastro