生成增强型文档 - Amazon SDK for Java 2.x
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

生成增强型文档

EnhancedDocument 表示具有复杂结构和嵌套属性的文档类型对象。EnhancedDocument 需要与为 DocumentTableSchema 指定的主键属性相匹配的顶级属性。其余内容是任意的,可以由顶级属性以及深度嵌套的属性组成。

您可以使用提供多种元素添加方法的生成器来创建 EnhancedDocument 实例。

使用 JSON 字符串生成

使用 JSON 字符串,您可以在一个方法调用中生成 EnhancedDocument。以下代码段从 jsonPerson() 帮助程序方法返回的 JSON 字符串创建一个 EnhancedDocumentjsonPerson() 方法返回前面显示的 person 对象的 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")); }