I am new to dynamodb and I need to process 5M records. Each record has an id and a status. I need to query for each record based on its status, process it, and finally update the status.
I am using DynamoDbEnhancedClient but I could not find example on how to query based only on the range and not the hash, while avoiding scan.
I tried to create a query with a condition and a limit of 1 but it did not work.
Here is what I have:
My Customer model:
@DynamoDbPartitionKey
private String id;
@DynamoDbSecondarySortKey(indexNames = "status")
private String status;
private String name;
configs:
@Bean
public DynamoDbEnhancedClient dynamoDbEnhancedClient(){
return DynamoDbEnhancedClient.builder()
.dynamoDbClient(dynamoDbClient())
.extensions(AutoGeneratedTimestampRecordExtension.create())
.build();
}
My query:
static final TableSchema<Customer> CUSTOMER_TABLE = TableSchema.fromClass(Customer.class);
public Customer findByStatus() {
DynamoDbTable<Customer> customerTable = dynamoDbEnhancedClient.table("customer", CUSTOMER_TABLE);
QueryConditional queryConditionalPerPartition = new EqualToConditional(Key.builder().
partitionValue("status").
build());
QueryEnhancedRequest request = QueryEnhancedRequest.builder()
.limit(1)
.queryConditional(queryConditionalPerPartition)
.build();
PageIterable<Customer> pageIterable = customerTable.query(request);
Customer customer = pageIterable.stream().findFirst().get().items().get(0);
return customer;
}
However that does not work. How can query by status and only get a single result? I have no restriction on the table structure and I can change it however I require.