Mappatura dei dati arbitrari - Amazon DynamoDB

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à.

Mappatura dei dati arbitrari

Oltre ai tipi Java supportati (vedere Tipi di dati supportati per DynamoDB Mapper per Java), è possibile utilizzare i tipi dell'applicazione per i quali non vi è una mappatura diretta ai tipi di Amazon DynamoDB. Per mappare questi tipi, è necessario fornire un'implementazione che converta il tipo complesso in un tipo supportato DynamoDB e viceversa; è inoltre necessario annotare il metodo di accesso del tipo complesso utilizzando l'annotazione @DynamoDBTypeConverted. Il codice del convertitore trasforma i dati quando gli oggetti vengono salvati o caricati. Viene utilizzato anche per tutte le operazioni che utilizzano tipi complessi. Quando si confrontano i dati durante le operazioni di query e di scansione, i confronti vengono fatti sui dati archiviati in DynamoDB.

Ad esempio, considera la seguente classe CatalogItem, che definisce una proprietà, Dimension, che è DimensionType. Questa proprietà archivia le dimensioni dell'item, quali altezza, larghezza e spessore. Supponiamo che si decida di memorizzare queste dimensioni dell'elemento come una stringa (ad esempio 8,5x11x0,5) in DynamoDB. Nell'esempio seguente viene fornito il codice del convertitore, il quale converte l'oggetto DimensionType in una stringa e una stringa in DimensionType.

Nota

In questo esempio di codice si presuppone che siano già stati caricati dati in DynamoDB per l'account seguendo le istruzioni riportate nella sezione Creazione di tabelle e caricamento di dati per esempi di codice in DynamoDB.

Per step-by-step istruzioni su come eseguire l'esempio seguente, vedereEsempi di codice Java.

Esempio
public class DynamoDBMapperExample { static AmazonDynamoDB client; public static void main(String[] args) throws IOException { // Set the AWS region you want to access. Regions usWest2 = Regions.US_WEST_2; client = AmazonDynamoDBClientBuilder.standard().withRegion(usWest2).build(); DimensionType dimType = new DimensionType(); dimType.setHeight("8.00"); dimType.setLength("11.0"); dimType.setThickness("1.0"); Book book = new Book(); book.setId(502); book.setTitle("Book 502"); book.setISBN("555-5555555555"); book.setBookAuthors(new HashSet<String>(Arrays.asList("Author1", "Author2"))); book.setDimensions(dimType); DynamoDBMapper mapper = new DynamoDBMapper(client); mapper.save(book); Book bookRetrieved = mapper.load(Book.class, 502); System.out.println("Book info: " + "\n" + bookRetrieved); bookRetrieved.getDimensions().setHeight("9.0"); bookRetrieved.getDimensions().setLength("12.0"); bookRetrieved.getDimensions().setThickness("2.0"); mapper.save(bookRetrieved); bookRetrieved = mapper.load(Book.class, 502); System.out.println("Updated book info: " + "\n" + bookRetrieved); } @DynamoDBTable(tableName = "ProductCatalog") public static class Book { private int id; private String title; private String ISBN; private Set<String> bookAuthors; private DimensionType dimensionType; // Partition key @DynamoDBHashKey(attributeName = "Id") public int getId() { return id; } public void setId(int id) { this.id = id; } @DynamoDBAttribute(attributeName = "Title") public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } @DynamoDBAttribute(attributeName = "ISBN") public String getISBN() { return ISBN; } public void setISBN(String ISBN) { this.ISBN = ISBN; } @DynamoDBAttribute(attributeName = "Authors") public Set<String> getBookAuthors() { return bookAuthors; } public void setBookAuthors(Set<String> bookAuthors) { this.bookAuthors = bookAuthors; } @DynamoDBTypeConverted(converter = DimensionTypeConverter.class) @DynamoDBAttribute(attributeName = "Dimensions") public DimensionType getDimensions() { return dimensionType; } @DynamoDBAttribute(attributeName = "Dimensions") public void setDimensions(DimensionType dimensionType) { this.dimensionType = dimensionType; } @Override public String toString() { return "Book [ISBN=" + ISBN + ", bookAuthors=" + bookAuthors + ", dimensionType= " + dimensionType.getHeight() + " X " + dimensionType.getLength() + " X " + dimensionType.getThickness() + ", Id=" + id + ", Title=" + title + "]"; } } static public class DimensionType { private String length; private String height; private String thickness; public String getLength() { return length; } public void setLength(String length) { this.length = length; } public String getHeight() { return height; } public void setHeight(String height) { this.height = height; } public String getThickness() { return thickness; } public void setThickness(String thickness) { this.thickness = thickness; } } // Converts the complex type DimensionType to a string and vice-versa. static public class DimensionTypeConverter implements DynamoDBTypeConverter<String, DimensionType> { @Override public String convert(DimensionType object) { DimensionType itemDimensions = (DimensionType) object; String dimension = null; try { if (itemDimensions != null) { dimension = String.format("%s x %s x %s", itemDimensions.getLength(), itemDimensions.getHeight(), itemDimensions.getThickness()); } } catch (Exception e) { e.printStackTrace(); } return dimension; } @Override public DimensionType unconvert(String s) { DimensionType itemDimension = new DimensionType(); try { if (s != null && s.length() != 0) { String[] data = s.split("x"); itemDimension.setLength(data[0].trim()); itemDimension.setHeight(data[1].trim()); itemDimension.setThickness(data[2].trim()); } } catch (Exception e) { e.printStackTrace(); } return itemDimension; } } }