Step 4: Query the tables in a ledger - Amazon Quantum Ledger Database (Amazon QLDB)

Step 4: Query the tables in a ledger

Important

End of support notice: Existing customers will be able to use Amazon QLDB until end of support on 07/31/2025. For more details, see Migrate an Amazon QLDB Ledger to Amazon Aurora PostgreSQL.

After creating tables in an Amazon QLDB ledger and loading them with data, you can run queries to review the vehicle registration data that you just inserted. QLDB uses PartiQL as its query language and Amazon Ion as its document-oriented data model.

PartiQL is an open-source, SQL-compatible query language that has been extended to work with Ion. With PartiQL, you can insert, query, and manage your data with familiar SQL operators. Amazon Ion is a superset of JSON. Ion is an open-source, document-based data format that gives you the flexibility of storing and processing structured, semistructured, and nested data.

In this step, you use SELECT statements to read data from the tables in the vehicle-registration ledger.

Warning

When you run a query in QLDB without an indexed lookup, it invokes a full table scan. PartiQL supports such queries because it's SQL compatible. However, don't run table scans for production use cases in QLDB. Table scans can cause performance problems on large tables, including concurrency conflicts and transaction timeouts.

To avoid table scans, you must run statements with a WHERE predicate clause using an equality operator on an indexed field or a document ID; for example, WHERE indexedField = 123 or WHERE indexedField IN (456, 789). For more information, see Optimizing query performance.

To query the tables
  • Compile and run the following program (FindVehicles.java) to query all vehicles registered under a person in your ledger.

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

    First, this program queries the Person table for the document with GovId LEWISR261LL to get its id metadata field.

    Then, it uses this document id as a foreign key to query the VehicleRegistration table by PrimaryOwner.PersonId. It also joins VehicleRegistration with the Vehicle table on the VIN field.

To learn about modifying documents in the tables in the vehicle-registration ledger, see Step 5: Modify documents in a ledger.