2

Officially, This is the example code using java:

BatchGetResultPageIterable batchResults = enhancedClient.batchGetItem(r -> r.addReadBatch(ReadBatch.builder(Customer.class)
                                                                            .mappedTableResource(customerTable)
                                                                            .addGetItem(key1)
                                                                            .addGetItem(key2)
                                                                            .addGetItem(key3)
                                                                            .build()));

What is the way to achieve that but sending a list of keys ? The library that I have to use is: https://github.com/aws/aws-sdk-java-v2/tree/master/services-custom/dynamodb-enhanced

sogu
  • 2,738
  • 5
  • 31
  • 90
EdwinCab
  • 361
  • 1
  • 3
  • 17
  • You just want the syntactic sugar of passing a list instead of calling addGetItem() repeatedly? You can do that with your own bit of wrapper code. – hunterhacker Apr 04 '22 at 18:37
  • thanks, the idea is receive a list of keys to query the table, can be 1, 2, 3, keys, then we cannot have the construction of that way, because is dynamic – EdwinCab Apr 04 '22 at 18:43

2 Answers2

1
DynamoDbTable<myRecord> table = enhancedClient.table("MyTableName", TABLE_SCHEMA);

List<Integer> keyIds = List.of(0,1,2,3,4);   // Example IDs
ReadBatch.Builder<myRecord> readBatchBuilder = ReadBatch.builder(myRecord.class);
keyIds.forEach(id -> readBatchBuilder.addGetItem(Key.builder().partitionValue(id).build()));
readBatchBuilder.mappedTableResource(table);
ReadBatch readBatch = readBatchBuilder.build();
BatchGetResultPageIterable iterable = enhancedClient.batchGetItem(r -> r.addReadBatch(readBatch));
Hephaestus
  • 4,337
  • 5
  • 35
  • 48
0

Create your batch get builder outside a stream. Create a stream of your keys and for each item in the stream, add it using the builder.

ReadBatch.Builder readBatchBuilder = ReadBatch.builder(QueryResultObject.class)
keys.forEach(
    readBatchBuilder.addGetItem(
        Key.builder().partitionValue(it.toString()).build()
    )
)
Hephaestus
  • 4,337
  • 5
  • 35
  • 48
Paul Ryan
  • 1
  • 1