aws-sdk-java-v2 icon indicating copy to clipboard operation
aws-sdk-java-v2 copied to clipboard

DynamoDB Enhanced Client: batch writes

Open chrylis opened this issue 2 years ago • 6 comments

Describe the feature

Requested feature: batchPutItem(Collection<T>) and batchUpdateItem(Collection<T>) methods on DynamoDbTable.

Use Case

The low-level client supports batch transactional writes, and the Enhanced client works by mapping POJOs onto low-level operations. I would like to write a series of records transactionally from POJOs, but the Enhanced client only supports single-record operations. Instead, I want to prepare the records to be saved and save them as a batch.

Proposed Solution

PutItemOperation (with the other TableOperations) generates a PutItemRequest, and the low-level client already supports the batchWriteItem operation that takes multiple PutItemRequests and batches them. All of the existing logic for the Enhanced client could remain in place with a scatter-gather implementation that collects the generated requests and executes them via batchWriteItem.

Other Information

No response

Acknowledgements

  • [x] I may be able to implement this feature request
  • [ ] This feature might incur a breaking change

AWS Java SDK version used

2.17.260

JDK version used

OpenJDK 64-Bit Server VM AdoptOpenJDK-11.0.11+9 (build 11.0.11+9, mixed mode)

Operating System and version

Gentoo Linux

chrylis avatar Aug 30 '22 19:08 chrylis

We do have batch operations in DynamoDbEnhancedClient, is this what you're looking for?

https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/enhanced/dynamodb/DynamoDbEnhancedClient.html

They're not DynamoDbTable operations because you can specify writes to different tables in the same request.

debora-ito avatar Aug 30 '22 22:08 debora-ito

So... it would be this?

enhancedClient.transactWriteItems(twi ->
  twi.addPutItem(table, record1).addPutItem(table, record2)
)

Not the worst API in the SDK, but still requires wrangling multiple resources unnecessarily, and it would be convenient to be able to access the batch/transact operations directly from the DynamoDbTable object.

chrylis avatar Aug 31 '22 20:08 chrylis

The records need to be provided anyway, so the only "unnecessary resource" you mean is the table?

Did you envision something much different in your feature request if the operations were from the DynamoDbTable directly?

debora-ito avatar Sep 01 '22 21:09 debora-ito

The unnecessary resource is the DynamoDB client; in my code, I'm generally not exposing that as it's owned by a service object, and the business code uses only table.

chrylis avatar Sep 07 '22 20:09 chrylis

Along the same lines, a batchGet on the DynamoDbTable would be nice.

nickcaballero avatar Sep 08 '22 17:09 nickcaballero

Regarding the batchPut, it can be written as a TableOperation that takes a BatchWriteItemEnhancedRequest<T> and returns a BatchWriteResult<T>, where the request/result are more strictly typed versions of the already available types. That would also pave the road for supporting a batchDelete.

nickcaballero avatar Sep 08 '22 17:09 nickcaballero

Quick update: this was added to the backlog.

Anyone who'd like to see this supported in the Java SDK make sure to add a 👍 to the original description to help us with prioritization.

debora-ito avatar Oct 11 '22 21:10 debora-ito