Crea documenti avanzati - AWS SDK for Java 2.x

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

Crea documenti avanzati

An EnhancedDocument rappresenta un oggetto di tipo documento con una struttura complessa con attributi annidati. An EnhancedDocument richiede attributi di primo livello che corrispondano agli attributi della chiave primaria specificati per. DocumentTableSchema Il contenuto rimanente è arbitrario e può essere costituito da attributi di primo livello e anche da attributi profondamente annidati.

Si crea un'EnhancedDocumentistanza utilizzando un generatore che offre diversi modi per aggiungere elementi.

Crea da una stringa JSON

Con una stringa JSON, puoi creare una chiamata EnhancedDocument in un unico metodo. Il seguente frammento crea un EnhancedDocument da una stringa JSON restituita dal metodo helper. jsonPerson() Il jsonPerson() metodo restituisce la versione in stringa JSON dell'oggetto persona mostrato in precedenza.

EnhancedDocument document = EnhancedDocument.builder() .json( jsonPerson() ) .build());

Costruisci a partire da singoli elementi

In alternativa, puoi creare un'EnhancedDocumentistanza a partire da singoli componenti utilizzando i metodi type-safe del builder.

L'esempio seguente crea un documento person avanzato simile al documento avanzato creato a partire dalla stringa JSON dell'esempio precedente.

/* Define the shape of an address map whose JSON representation looks like the following. Use 'addressMapEnhancedType' in the following EnhancedDocument.builder() to simplify the code. "home": { "zipCode": "00000", "city": "Any Town", "state": "FL", "street": "123 Any Street" }*/ EnhancedType<Map<String, String>> addressMapEnhancedType = EnhancedType.mapOf(EnhancedType.of(String.class), EnhancedType.of(String.class)); // Use the builder's typesafe methods to add elements to the enhanced document. EnhancedDocument personDocument = EnhancedDocument.builder() .putNumber("id", 50) .putString("firstName", "Shirley") .putString("lastName", "Rodriguez") .putNumber("age", 53) .putNull("nullAttribute") .putJson("phoneNumbers", phoneNumbersJSONString()) /* Add the map of addresses whose JSON representation looks like the following. { "home": { "zipCode": "00000", "city": "Any Town", "state": "FL", "street": "123 Any Street" } } */ .putMap("addresses", getAddresses(), EnhancedType.of(String.class), addressMapEnhancedType) .putList("hobbies", List.of("Theater", "Golf"), EnhancedType.of(String.class)) .build();
private static String phoneNumbersJSONString() { return " [" + " {" + " \"type\": \"Home\"," + " \"number\": \"555-0140\"" + " }," + " {" + " \"type\": \"Work\"," + " \"number\": \"555-0155\"" + " }" + " ]"; } private static Map<String, Map<String, String>> getAddresses() { return Map.of( "home", Map.of( "zipCode", "00002", "city", "Any Town", "state", "ME", "street", "123 Any Street")); }