DynamoDB - Batch Write

DynamoDB - Batch Write

Batch writing works on multiple items by creating or deleting multiple items. These operations use BatchWriteItem , which is limited to 16MB of entries and 25 requests. Each element complies with the 400 KB limit. Batch write also cannot perform item updates.

What is batch recording?

Batch write can manipulate items in multiple tables. The call to the operation happens on a per-request basis, which means the operations do not affect each other and heterogeneous mixes are allowed; for example, one PutItem and three DeleteItem requests in a batch, with the failure of the PutItem request not affecting the others. Failed requests cause the operation to return information (keys and data) related to each failed request.

Note. If DynamoDB returns any items without processing them, retry them; however, use the fallback method to avoid another request failure due to overload.

DynamoDB rejects a batch write operation when one or more of the following assertions are true:

  • The request exceeds the provided bandwidth.

  • The request tries to use BatchWriteItems to update the item.

  • The query performs multiple operations on the same element.

  • Query tables do not exist.

  • The element attributes in the request do not match the target.

  • Requests exceed size limits.

The request exceeds the provided bandwidth.

The request tries to use BatchWriteItems to update the item.

The query performs multiple operations on the same element.

Query tables do not exist.

The element attributes in the request do not match the target.

Requests exceed size limits.

Batch writing requires specific RequestItem parameters −

  • Delete operations require the DeleteRequest key subelements , which are the name and value of the attribute.

  • PutRequest elements require an Item subelement that represents the map of the attribute and attribute values.

Delete operations require the DeleteRequest key subelements , which are the name and value of the attribute.

PutRequest elements require an Item subelement that represents the map of the attribute and attribute values.

Response - A successful operation results in an HTTP 200 response that indicates characteristics such as capacity units used, table processing metrics, and any outstanding items.

Batch Write with Java

Perform a batch write by creating an instance of the DynamoDB class, an instance of the TableWriteItems class describing all operations, and calling the batchWriteItem method to use the TableWriteItems object.

Note. You must create a TableWriteItems instance for each table in a multi-table batch write. Also, check your response request for any pending requests.

You can view the following batch write example −

DynamoDB dynamoDB = new DynamoDB ( new AmazonDynamoDBClient ( new ProfileCredentialsProvider ()));    
      

TableWriteItems forumTableWriteItems = new TableWriteItems ( "Forum" ) . withItemsToPut ( new Item () . withPrimaryKey ( "Title" , "XYZ CRM" ) . withNumber ( "Threads" , 0 ));   
    
     
     
      

TableWriteItems threadTableWriteItems = new TableWriteItems ( Thread ) . withItemsToPut ( new Item () . withPrimaryKey ( "ForumTitle" , "XYZ CRM" , "Topic" , "Updates" ) . withHashAndRangeKeysToDelete ( "ForumTitle" , "A partition key value" , ​​"Product Line 1" , "A sort key value" );   
    
     
    
    
    

BatchWriteItemOutcome outcome = dynamoDB . batchWriteItem ( 
   forumTableWriteItems , threadTableWriteItems );

The following program is another great example for a better understanding of how the package writes from Java.

Note. The following example can use a previously created data source. Before attempting to execute, acquire the supporting libraries and create the necessary data sources (tables with the required characteristics or other referenced sources).

This example also uses the Eclipse IDE, the AWS credential file, and the AWS toolkit in the Eclipse AWS Java project.