建置增強型文件 - AWS SDK for Java 2.x

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

建置增強型文件

EnhancedDocument表具有複雜結構且具有巢狀屬性的文件類型物件。EnhancedDocument需要符合為指定的主索引鍵屬性的頂層屬性DocumentTableSchema。其餘的內容是任意的,可以由頂層屬性和深層巢狀屬性組成。

您可以使用提供數種方式來加入元素的建置器來建立EnhancedDocument執行個體。

從 JSON 字符串構建

使用 JSON 字符串,您可以構建一EnhancedDocument個方法調用。下面的代碼片段EnhancedDocumentjsonPerson()幫助器方法返回的 JSON 字符串創建一個。此方jsonPerson()法會傳回之前顯示之人物件的 JSON 字串版本。

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

從個別元素建置

或者,您可以使用構建器的類型安全方法從單個組件構建EnhancedDocument實例。

下列範例會建立person類似於先前範例中以 JSON 字串建立的增強文件類似的增強文件。

/* 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")); }