Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.
Schritt 3: Erstellen Sie Tabellen, Indizes und Beispieldaten
Wichtig
Hinweis zum Ende des Supports: Bestandskunden können Amazon QLDB bis zum Ende des Supports am 31.07.2025 nutzen. Weitere Informationen finden Sie unter Migrieren eines Amazon QLDB Ledgers zu Amazon Aurora SQL Postgre
Wenn Ihr QLDB Amazon-Ledger aktiv ist und Verbindungen akzeptiert, können Sie damit beginnen, Tabellen mit Daten über Fahrzeuge, deren Besitzer und deren Zulassungsinformationen zu erstellen. Nach dem Erstellen der Tabellen und Indizes können Sie Daten in diese laden.
In diesem Schritt erstellen Sie vier Tabellen im vehicle-registration
-Ledger:
-
VehicleRegistration
-
Vehicle
-
Person
-
DriversLicense
Außerdem erzeugen Sie die folgenden Indizes.
Tabellenname | Feld |
---|---|
VehicleRegistration |
VIN |
VehicleRegistration |
LicensePlateNumber |
Vehicle |
VIN |
Person |
GovId |
DriversLicense |
LicenseNumber |
DriversLicense |
PersonId |
Beim Einfügen von Beispieldaten fügen Sie zunächst Dokumente in die Person
-Tabelle ein. Anschließend verwenden Sie die vom System zugewiesene id
aus den Person
-Dokumenten, um die entsprechenden Felder in den relevanten VehicleRegistration
- und DriversLicense
-Dokumenten auszufüllen.
Tipp
Es hat sich bewährt, das einem Dokument vom System zugewiesene Fremdschlüssel id
zu verwenden. Sie können zwar Felder definieren, die als eindeutige Identifikatoren dienen sollen (z. B. die eines FahrzeugsVIN), aber die wahre eindeutige Kennung eines Dokuments ist die eigene. id
Dieses Feld ist in den Metadaten des Dokuments enthalten, die Sie in der festgeschriebenen Ansicht (d. h. in der vom System definierten Ansicht der Tabelle) abrufen können.
Weitere Informationen zu Ansichten in finden Sie QLDB unterSchlüsselkonzepte. Weitere Informationen zu Metadaten finden Sie unter Metadaten von Dokumenten werden abgefragt.
So richten Sie den Beispiel-Stack ein
-
Überprüfen Sie die folgenden
.java
-Dateien: Diese Modellklassen stellen Dokumente dar, die Sie in denvehicle-registration
-Tabellen speichern. Sie können in das und vom Amazon Ion-Format serialisiert werden.Anmerkung
QLDBAmazon-Dokumentewerden im Ion-Format gespeichert, das eine Obermenge von ist. JSON Sie können also die Faster XML Jackson-Bibliothek verwenden, um die Daten zu modellieren. JSON
-
DriversLicense.java
/* * 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.model; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import software.amazon.qldb.tutorial.model.streams.RevisionData; import java.time.LocalDate; /** * Represents a driver's license, serializable to (and from) Ion. */ public final class DriversLicense implements RevisionData { private final String personId; private final String licenseNumber; private final String licenseType; @JsonSerialize(using = IonLocalDateSerializer.class) @JsonDeserialize(using = IonLocalDateDeserializer.class) private final LocalDate validFromDate; @JsonSerialize(using = IonLocalDateSerializer.class) @JsonDeserialize(using = IonLocalDateDeserializer.class) private final LocalDate validToDate; @JsonCreator public DriversLicense(@JsonProperty("PersonId") final String personId, @JsonProperty("LicenseNumber") final String licenseNumber, @JsonProperty("LicenseType") final String licenseType, @JsonProperty("ValidFromDate") final LocalDate validFromDate, @JsonProperty("ValidToDate") final LocalDate validToDate) { this.personId = personId; this.licenseNumber = licenseNumber; this.licenseType = licenseType; this.validFromDate = validFromDate; this.validToDate = validToDate; } @JsonProperty("PersonId") public String getPersonId() { return personId; } @JsonProperty("LicenseNumber") public String getLicenseNumber() { return licenseNumber; } @JsonProperty("LicenseType") public String getLicenseType() { return licenseType; } @JsonProperty("ValidFromDate") public LocalDate getValidFromDate() { return validFromDate; } @JsonProperty("ValidToDate") public LocalDate getValidToDate() { return validToDate; } @Override public String toString() { return "DriversLicense{" + "personId='" + personId + '\'' + ", licenseNumber='" + licenseNumber + '\'' + ", licenseType='" + licenseType + '\'' + ", validFromDate=" + validFromDate + ", validToDate=" + validToDate + '}'; } }
-
Person.java
/* * 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.model; import java.time.LocalDate; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import software.amazon.qldb.TransactionExecutor; import software.amazon.qldb.tutorial.Constants; import software.amazon.qldb.tutorial.model.streams.RevisionData; /** * Represents a person, serializable to (and from) Ion. */ public final class Person implements RevisionData { private final String firstName; private final String lastName; @JsonSerialize(using = IonLocalDateSerializer.class) @JsonDeserialize(using = IonLocalDateDeserializer.class) private final LocalDate dob; private final String govId; private final String govIdType; private final String address; @JsonCreator public Person(@JsonProperty("FirstName") final String firstName, @JsonProperty("LastName") final String lastName, @JsonProperty("DOB") final LocalDate dob, @JsonProperty("GovId") final String govId, @JsonProperty("GovIdType") final String govIdType, @JsonProperty("Address") final String address) { this.firstName = firstName; this.lastName = lastName; this.dob = dob; this.govId = govId; this.govIdType = govIdType; this.address = address; } @JsonProperty("Address") public String getAddress() { return address; } @JsonProperty("DOB") public LocalDate getDob() { return dob; } @JsonProperty("FirstName") public String getFirstName() { return firstName; } @JsonProperty("LastName") public String getLastName() { return lastName; } @JsonProperty("GovId") public String getGovId() { return govId; } @JsonProperty("GovIdType") public String getGovIdType() { return govIdType; } /** * This returns the unique document ID given a specific government ID. * * @param txn * A transaction executor object. * @param govId * The government ID of a driver. * @return the unique document ID. */ public static String getDocumentIdByGovId(final TransactionExecutor txn, final String govId) { return SampleData.getDocumentId(txn, Constants.PERSON_TABLE_NAME, "GovId", govId); } @Override public String toString() { return "Person{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", dob=" + dob + ", govId='" + govId + '\'' + ", govIdType='" + govIdType + '\'' + ", address='" + address + '\'' + '}'; } }
-
VehicleRegistration.java
/* * 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.model; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import software.amazon.qldb.TransactionExecutor; import software.amazon.qldb.tutorial.Constants; import software.amazon.qldb.tutorial.model.streams.RevisionData; import java.math.BigDecimal; import java.time.LocalDate; /** * Represents a vehicle registration, serializable to (and from) Ion. */ public final class VehicleRegistration implements RevisionData { private final String vin; private final String licensePlateNumber; private final String state; private final String city; private final BigDecimal pendingPenaltyTicketAmount; private final LocalDate validFromDate; private final LocalDate validToDate; private final Owners owners; @JsonCreator public VehicleRegistration(@JsonProperty("VIN") final String vin, @JsonProperty("LicensePlateNumber") final String licensePlateNumber, @JsonProperty("State") final String state, @JsonProperty("City") final String city, @JsonProperty("PendingPenaltyTicketAmount") final BigDecimal pendingPenaltyTicketAmount, @JsonProperty("ValidFromDate") final LocalDate validFromDate, @JsonProperty("ValidToDate") final LocalDate validToDate, @JsonProperty("Owners") final Owners owners) { this.vin = vin; this.licensePlateNumber = licensePlateNumber; this.state = state; this.city = city; this.pendingPenaltyTicketAmount = pendingPenaltyTicketAmount; this.validFromDate = validFromDate; this.validToDate = validToDate; this.owners = owners; } @JsonProperty("City") public String getCity() { return city; } @JsonProperty("LicensePlateNumber") public String getLicensePlateNumber() { return licensePlateNumber; } @JsonProperty("Owners") public Owners getOwners() { return owners; } @JsonProperty("PendingPenaltyTicketAmount") public BigDecimal getPendingPenaltyTicketAmount() { return pendingPenaltyTicketAmount; } @JsonProperty("State") public String getState() { return state; } @JsonProperty("ValidFromDate") @JsonSerialize(using = IonLocalDateSerializer.class) @JsonDeserialize(using = IonLocalDateDeserializer.class) public LocalDate getValidFromDate() { return validFromDate; } @JsonProperty("ValidToDate") @JsonSerialize(using = IonLocalDateSerializer.class) @JsonDeserialize(using = IonLocalDateDeserializer.class) public LocalDate getValidToDate() { return validToDate; } @JsonProperty("VIN") public String getVin() { return vin; } /** * Returns the unique document ID of a vehicle given a specific VIN. * * @param txn * A transaction executor object. * @param vin * The VIN of a vehicle. * @return the unique document ID of the specified vehicle. */ public static String getDocumentIdByVin(final TransactionExecutor txn, final String vin) { return SampleData.getDocumentId(txn, Constants.VEHICLE_REGISTRATION_TABLE_NAME, "VIN", vin); } @Override public String toString() { return "VehicleRegistration{" + "vin='" + vin + '\'' + ", licensePlateNumber='" + licensePlateNumber + '\'' + ", state='" + state + '\'' + ", city='" + city + '\'' + ", pendingPenaltyTicketAmount=" + pendingPenaltyTicketAmount + ", validFromDate=" + validFromDate + ", validToDate=" + validToDate + ", owners=" + owners + '}'; } }
-
Vehicle.java
/* * 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.model; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import software.amazon.qldb.tutorial.model.streams.RevisionData; /** * Represents a vehicle, serializable to (and from) Ion. */ public final class Vehicle implements RevisionData { private final String vin; private final String type; private final int year; private final String make; private final String model; private final String color; @JsonCreator public Vehicle(@JsonProperty("VIN") final String vin, @JsonProperty("Type") final String type, @JsonProperty("Year") final int year, @JsonProperty("Make") final String make, @JsonProperty("Model") final String model, @JsonProperty("Color") final String color) { this.vin = vin; this.type = type; this.year = year; this.make = make; this.model = model; this.color = color; } @JsonProperty("Color") public String getColor() { return color; } @JsonProperty("Make") public String getMake() { return make; } @JsonProperty("Model") public String getModel() { return model; } @JsonProperty("Type") public String getType() { return type; } @JsonProperty("VIN") public String getVin() { return vin; } @JsonProperty("Year") public int getYear() { return year; } @Override public String toString() { return "Vehicle{" + "vin='" + vin + '\'' + ", type='" + type + '\'' + ", year=" + year + ", make='" + make + '\'' + ", model='" + model + '\'' + ", color='" + color + '\'' + '}'; } }
-
Owner.java
/* * 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.model; import com.fasterxml.jackson.annotation.JsonProperty; /** * Represents a vehicle owner, serializable to (and from) Ion. */ public final class Owner { private final String personId; public Owner(@JsonProperty("PersonId") final String personId) { this.personId = personId; } @JsonProperty("PersonId") public String getPersonId() { return personId; } @Override public String toString() { return "Owner{" + "personId='" + personId + '\'' + '}'; } }
-
Owners.java
/* * 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.model; import com.fasterxml.jackson.annotation.JsonProperty; import java.util.List; /** * Represents a set of owners for a given vehicle, serializable to (and from) Ion. */ public final class Owners { private final Owner primaryOwner; private final List<Owner> secondaryOwners; public Owners(@JsonProperty("PrimaryOwner") final Owner primaryOwner, @JsonProperty("SecondaryOwners") final List<Owner> secondaryOwners) { this.primaryOwner = primaryOwner; this.secondaryOwners = secondaryOwners; } @JsonProperty("PrimaryOwner") public Owner getPrimaryOwner() { return primaryOwner; } @JsonProperty("SecondaryOwners") public List<Owner> getSecondaryOwners() { return secondaryOwners; } @Override public String toString() { return "Owners{" + "primaryOwner=" + primaryOwner + ", secondaryOwners=" + secondaryOwners + '}'; } }
-
DmlResultDocument.java
/* * 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.qldb; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; /** * Contains information about an individual document inserted or modified * as a result of DML. */ public class DmlResultDocument { private String documentId; @JsonCreator public DmlResultDocument(@JsonProperty("documentId") final String documentId) { this.documentId = documentId; } public String getDocumentId() { return documentId; } @Override public String toString() { return "DmlResultDocument{" + "documentId='" + documentId + '\'' + '}'; } }
-
RevisionData.java
/* * 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.model.streams; /** * Allows modeling the content of all revisions as a generic revision data. Used * in the {@link Revision} and extended by domain models in {@link * software.amazon.qldb.tutorial.model} to make it easier to write the {@link * Revision.RevisionDataDeserializer} that must deserialize the {@link * Revision#data} from different domain models. */ public interface RevisionData { }
-
RevisionMetadata.java
/* * 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.qldb; import com.amazon.ion.IonInt; import com.amazon.ion.IonString; import com.amazon.ion.IonStruct; import com.amazon.ion.IonTimestamp; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.dataformat.ion.IonTimestampSerializers; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Date; import java.util.Objects; /** * Represents the metadata field of a QLDB Document */ public class RevisionMetadata { private static final Logger log = LoggerFactory.getLogger(RevisionMetadata.class); private final String id; private final long version; @JsonSerialize(using = IonTimestampSerializers.IonTimestampJavaDateSerializer.class) private final Date txTime; private final String txId; @JsonCreator public RevisionMetadata(@JsonProperty("id") final String id, @JsonProperty("version") final long version, @JsonProperty("txTime") final Date txTime, @JsonProperty("txId") final String txId) { this.id = id; this.version = version; this.txTime = txTime; this.txId = txId; } /** * Gets the unique ID of a QLDB document. * * @return the document ID. */ public String getId() { return id; } /** * Gets the version number of the document in the document's modification history. * @return the version number. */ public long getVersion() { return version; } /** * Gets the time during which the document was modified. * * @return the transaction time. */ public Date getTxTime() { return txTime; } /** * Gets the transaction ID associated with this document. * * @return the transaction ID. */ public String getTxId() { return txId; } public static RevisionMetadata fromIon(final IonStruct ionStruct) { if (ionStruct == null) { throw new IllegalArgumentException("Metadata cannot be null"); } try { IonString id = (IonString) ionStruct.get("id"); IonInt version = (IonInt) ionStruct.get("version"); IonTimestamp txTime = (IonTimestamp) ionStruct.get("txTime"); IonString txId = (IonString) ionStruct.get("txId"); if (id == null || version == null || txTime == null || txId == null) { throw new IllegalArgumentException("Document is missing required fields"); } return new RevisionMetadata(id.stringValue(), version.longValue(), new Date(txTime.getMillis()), txId.stringValue()); } catch (ClassCastException e) { log.error("Failed to parse ion document"); throw new IllegalArgumentException("Document members are not of the correct type", e); } } /** * Converts a {@link RevisionMetadata} object to a string. * * @return the string representation of the {@link QldbRevision} object. */ @Override public String toString() { return "Metadata{" + "id='" + id + '\'' + ", version=" + version + ", txTime=" + txTime + ", txId='" + txId + '\'' + '}'; } /** * Check whether two {@link RevisionMetadata} objects are equivalent. * * @return {@code true} if the two objects are equal, {@code false} otherwise. */ @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } RevisionMetadata metadata = (RevisionMetadata) o; return version == metadata.version && id.equals(metadata.id) && txTime.equals(metadata.txTime) && txId.equals(metadata.txId); } /** * Generate a hash code for the {@link RevisionMetadata} object. * * @return the hash code. */ @Override public int hashCode() { // CHECKSTYLE:OFF - Disabling as we are generating a hashCode of multiple properties. return Objects.hash(id, version, txTime, txId); // CHECKSTYLE:ON } }
-
QldbRevision.java
/* * 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.qldb; import com.amazon.ion.IonBlob; import com.amazon.ion.IonStruct; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.qldb.tutorial.Constants; import software.amazon.qldb.tutorial.Verifier; import java.io.IOException; import java.util.Arrays; import java.util.Objects; /** * Represents a QldbRevision including both user data and metadata. */ public final class QldbRevision { private static final Logger log = LoggerFactory.getLogger(QldbRevision.class); private final BlockAddress blockAddress; private final RevisionMetadata metadata; private final byte[] hash; private final byte[] dataHash; private final IonStruct data; @JsonCreator public QldbRevision(@JsonProperty("blockAddress") final BlockAddress blockAddress, @JsonProperty("metadata") final RevisionMetadata metadata, @JsonProperty("hash") final byte[] hash, @JsonProperty("dataHash") final byte[] dataHash, @JsonProperty("data") final IonStruct data) { this.blockAddress = blockAddress; this.metadata = metadata; this.hash = hash; this.dataHash = dataHash; this.data = data; } /** * Gets the unique ID of a QLDB document. * * @return the {@link BlockAddress} object. */ public BlockAddress getBlockAddress() { return blockAddress; } /** * Gets the metadata of the revision. * * @return the {@link RevisionMetadata} object. */ public RevisionMetadata getMetadata() { return metadata; } /** * Gets the SHA-256 hash value of the revision. * This is equivalent to the hash of the revision metadata and data. * * @return the byte array representing the hash. */ public byte[] getHash() { return hash; } /** * Gets the SHA-256 hash value of the data portion of the revision. * This is only present if the revision is redacted. * * @return the byte array representing the hash. */ public byte[] getDataHash() { return dataHash; } /** * Gets the revision data. * * @return the revision data. */ public IonStruct getData() { return data; } /** * Returns true if the revision has been redacted. * @return a boolean value representing the redaction status * of this revision. */ public Boolean isRedacted() { return dataHash != null; } /** * Constructs a new {@link QldbRevision} from an {@link IonStruct}. * * The specified {@link IonStruct} must include the following fields * * - blockAddress -- a {@link BlockAddress}, * - metadata -- a {@link RevisionMetadata}, * - hash -- the revision's hash calculated by QLDB, * - dataHash -- the user data's hash calculated by QLDB (only present if revision is redacted), * - data -- an {@link IonStruct} containing user data in the document. * * If any of these fields are missing or are malformed, then throws {@link IllegalArgumentException}. * * If the document hash calculated from the members of the specified {@link IonStruct} does not match * the hash member of the {@link IonStruct} then throws {@link IllegalArgumentException}. * * @param ionStruct * The {@link IonStruct} that contains a {@link QldbRevision} object. * @return the converted {@link QldbRevision} object. * @throws IOException if failed to parse parameter {@link IonStruct}. */ public static QldbRevision fromIon(final IonStruct ionStruct) throws IOException { try { BlockAddress blockAddress = Constants.MAPPER.readValue(ionStruct.get("blockAddress"), BlockAddress.class); IonBlob revisionHash = (IonBlob) ionStruct.get("hash"); IonStruct metadataStruct = (IonStruct) ionStruct.get("metadata"); IonStruct data = ionStruct.get("data") == null || ionStruct.get("data").isNullValue() ? null : (IonStruct) ionStruct.get("data"); IonBlob dataHash = ionStruct.get("dataHash") == null || ionStruct.get("dataHash").isNullValue() ? null : (IonBlob) ionStruct.get("dataHash"); if (revisionHash == null || metadataStruct == null) { throw new IllegalArgumentException("Document is missing required fields"); } byte[] dataHashBytes = dataHash != null ? dataHash.getBytes() : QldbIonUtils.hashIonValue(data); verifyRevisionHash(metadataStruct, dataHashBytes, revisionHash.getBytes()); RevisionMetadata metadata = RevisionMetadata.fromIon(metadataStruct); return new QldbRevision( blockAddress, metadata, revisionHash.getBytes(), dataHash != null ? dataHash.getBytes() : null, data ); } catch (ClassCastException e) { log.error("Failed to parse ion document"); throw new IllegalArgumentException("Document members are not of the correct type", e); } } /** * Converts a {@link QldbRevision} object to string. * * @return the string representation of the {@link QldbRevision} object. */ @Override public String toString() { return "QldbRevision{" + "blockAddress=" + blockAddress + ", metadata=" + metadata + ", hash=" + Arrays.toString(hash) + ", dataHash=" + Arrays.toString(dataHash) + ", data=" + data + '}'; } /** * Check whether two {@link QldbRevision} objects are equivalent. * * @return {@code true} if the two objects are equal, {@code false} otherwise. */ @Override public boolean equals(final Object o) { if (this == o) { return true; } if (!(o instanceof QldbRevision)) { return false; } final QldbRevision that = (QldbRevision) o; return Objects.equals(getBlockAddress(), that.getBlockAddress()) && Objects.equals(getMetadata(), that.getMetadata()) && Arrays.equals(getHash(), that.getHash()) && Arrays.equals(getDataHash(), that.getDataHash()) && Objects.equals(getData(), that.getData()); } /** * Create a hash code for the {@link QldbRevision} object. * * @return the hash code. */ @Override public int hashCode() { // CHECKSTYLE:OFF - Disabling as we are generating a hashCode of multiple properties. int result = Objects.hash(blockAddress, metadata, data); // CHECKSTYLE:ON result = 31 * result + Arrays.hashCode(hash); return result; } /** * Throws an IllegalArgumentException if the hash of the revision data and metadata * does not match the hash provided by QLDB with the revision. */ public void verifyRevisionHash() { // Certain internal-only system revisions only contain a hash which cannot be // further computed. However, these system hashes still participate to validate // the journal block. User revisions will always contain values for all fields // and can therefore have their hash computed. if (blockAddress == null && metadata == null && data == null && dataHash == null) { return; } try { IonStruct metadataIon = (IonStruct) Constants.MAPPER.writeValueAsIonValue(metadata); byte[] dataHashBytes = isRedacted() ? dataHash : QldbIonUtils.hashIonValue(data); verifyRevisionHash(metadataIon, dataHashBytes, hash); } catch (IOException e) { throw new IllegalArgumentException("Could not encode revision metadata to ion.", e); } } private static void verifyRevisionHash(IonStruct metadata, byte[] dataHash, byte[] expectedHash) { byte[] metadataHash = QldbIonUtils.hashIonValue(metadata); byte[] candidateHash = Verifier.dot(metadataHash, dataHash); if (!Arrays.equals(candidateHash, expectedHash)) { throw new IllegalArgumentException("Hash entry of QLDB revision and computed hash " + "of QLDB revision do not match"); } } }
-
IonLocalDateDeserializer.java
/* * 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.model; import com.amazon.ion.Timestamp; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; import java.io.IOException; import java.time.LocalDate; /** * Deserializes [java.time.LocalDate] from Ion. */ public class IonLocalDateDeserializer extends JsonDeserializer<LocalDate> { @Override public LocalDate deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { return timestampToLocalDate((Timestamp) jp.getEmbeddedObject()); } private LocalDate timestampToLocalDate(Timestamp timestamp) { return LocalDate.of(timestamp.getYear(), timestamp.getMonth(), timestamp.getDay()); } }
-
IonLocalDateSerializer.java
/* * 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.model; import com.amazon.ion.Timestamp; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer; import com.fasterxml.jackson.dataformat.ion.IonGenerator; import java.io.IOException; import java.time.LocalDate; /** * Serializes [java.time.LocalDate] to Ion. */ public class IonLocalDateSerializer extends StdScalarSerializer<LocalDate> { public IonLocalDateSerializer() { super(LocalDate.class); } @Override public void serialize(LocalDate date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { Timestamp timestamp = Timestamp.forDay(date.getYear(), date.getMonthValue(), date.getDayOfMonth()); ((IonGenerator) jsonGenerator).writeValue(timestamp); } }
-
-
Überprüfen Sie die folgende Datei (
SampleData.java
), die Daten darstellt, die Sie in dievehicle-registration
-Tabellen einfügen.Anmerkung
-
Diese Klasse verwendet Ion-Bibliotheken, um Hilfsmethoden bereitzustellen, die Ihre Daten in das und aus dem Ion-Format konvertieren.
-
Die
getDocumentId
-Methode führt eine Abfrage für eine Tabelle mit dem Präfix_ql_committed_
aus. Dies ist ein reserviertes Präfix, das angibt, dass Sie die bestätigte Ansicht einer Tabelle abfragen möchten. In dieser Ansicht sind Ihre Daten imdata
-Feld verschachtelt und Metadaten sind immetadata
-Feld verschachtelt.
-
-
Kompilieren Sie das folgende Programm (
CreateTable.java
) und führen Sie es aus, um die zuvor genannten Tabellen zu erstellen.Anmerkung
Dieses Programm verdeutlicht, wie ein
TransactionExecutor
-Lambda an dieexecute
-Methode übergeben wird. In diesem Beispiel führen Sie mehrereCREATE TABLE
PartiQL-Anweisungen in einer einzigen Transaktion aus, indem Sie einen Lambda-Ausdruck verwenden.Die
execute
Methode startet implizit eine Transaktion, führt alle Anweisungen im Lambda aus und schreibt dann die Transaktion automatisch fest. -
Kompilieren Sie das folgende Programm (
CreateIndex.java
) und führen Sie es aus, um wie zuvor beschrieben Indizes für die Tabellen zu erstellen. -
Kompilieren Sie das folgende Programm (
InsertDocument.java
) und führen Sie es aus, um die Beispieldaten in Ihre Tabellen einzufügen.Anmerkung
-
Dieses Programm veranschaulicht, wie die
execute
-Methode mit parametrisierten Werten aufgerufen wird. Sie können außerdem eine Liste mit Datenparametern des TypsIonValue
an die auszuführende PartiQL-Anweisung übergeben. Verwenden Sie ein Fragezeichen (?
) als Variablenplatzhalter in Ihrer Anweisungszeichenfolge. -
Wenn eine
INSERT
-Anweisung erfolgreich ist, wird dieid
jedes eingefügten Dokuments zurückgegeben.
-
Als Nächstes können Sie mit SELECT
-Anweisungen Daten aus den Tabellen im vehicle-registration
-Ledger lesen. Fahren Sie mit Schritt 4: Fragen Sie die Tabellen in einem Hauptbuch ab fort.