Document API differences between version 1 and version 2 of the Amazon SDK for Java - Amazon SDK for Java 2.x
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Document API differences between version 1 and version 2 of the Amazon SDK for Java

The Document API supports working with JSON-style documents as single items in a DynamoDB table. The V1 Document API has a corresponding API in V2, but instead of using a separate client for the document API as in V1, V2 incorporates document API features in the DynamoDB enhanced client.

In V1, the Item class represents an unstructured record from a DynamoDB table. In V2, an unstructured record is represented by an instance of the EnhancedDocument class. Note that primary keys are defined in the table schema for V2, and on the item itself in V1.

The table below compares the differences between the Document APIs in V1 and V2.

Use case V1 V2
Create a document client
AmazonDynamoDB client = ... //Create a client. DynamoDB documentClient = new DynamoDB(client);
// The V2 Document API uses the same DynamoDbEnhancedClient // that is used for mapping POJOs. DynamoDbClient standardClient = ... //Create a standard client. DynamoDbEnhancedClient enhancedClient = ... // Create an enhanced client.
Reference a table
Table documentTable = docClient.documentClient("Person");
DynamoDbTable<EnhancedDocument> documentTable = enhancedClient.table("Person", TableSchema.documentSchemaBuilder() .addIndexPartitionKey(TableMetadata.primaryIndexName(),"id", AttributeValueType.S) .attributeConverterProviders(AttributeConverterProvider.defaultProvider()) .build());
Work with semi-structured data
Put item
Item item = new Item() .withPrimaryKey("id", 50) .withString("firstName", "Shirley"); PutItemOutcome outcome = documentTable.putItem(item);
EnhancedDocument personDocument = EnhancedDocument.builder() .putNumber("id", 50) .putString("firstName", "Shirley") .build(); documentTable.putItem(personDocument);
Get item
GetItemOutcome outcome = documentTable.getItemOutcome( "id", 50); Item personDocFromDb = outcome.getItem(); String firstName = personDocFromDb.getString("firstName");
EnhancedDocument personDocFromDb = documentTable .getItem(Key.builder() .partitionValue(50) .build()); String firstName = personDocFromDb.getString("firstName");
Work with JSON items
Convert a JSON structure to use it with the Document API
// The 'jsonPerson' identifier is a JSON string. Item item = new Item().fromJSON(jsonPerson);
// The 'jsonPerson' identifier is a JSON string. EnhancedDocument document = EnhancedDocument.builder() .json(jsonPerson).build());
Put JSON
documentTable.putItem(item)
documentTable.putItem(document);
Read JSON
GetItemOutcome outcome = //Get item. String jsonPerson = outcome.getItem().toJSON();
String jsonPerson = documentTable.getItem(Key.builder() .partitionValue(50).build()) .fromJson();

API reference and guides for document APIs

V1 V2
API reference API reference API reference
Documentation guide Amazon DynamoDB Developer Guide Enhanced Document API (this guide)