Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query DynamoDB using ONLY Partition Key [Java]?

I am new to DynamoDB and wanted to know how can we query on a table in DynamoDB by using ONLY partition key in JAVA

I have table called "ervive-pdi-data-invalid-qa" and it's Schema is :

  1. partition key is "SubmissionId"
  2. Sort key is "Id".
  3. City (Attribute)
  4. Errors (Attribute)

The table looks like this: Table

I want to retrieve the sort key value and remaining attributes data by using Partition key using (software.amazon.awssdk) new version of AWS SDK DynamoDB classes.

is it possible to get it? If so, can any one post the answers?

Have tried this:

       DynamoDbClient ddb = 
           DynamoDbClient.builder().region(Region.US_EAST_1).build();
             DynamoDbEnhancedClient enhancedClient = 
                DynamoDbEnhancedClient.builder()
                .dynamoDbClient(ddb)
                .build();

        //Define table
        DynamoDbTable<ErvivePdiDataInvalidQa> table = 
           enhancedClient.table("ervive-pdi-data-invalid-qa",
                  TableSchema.fromBean(ErvivePdiDataInvalidQa.class));

        Key key = Key.builder().partitionValue(2023).build();
        ErvivePdiDataInvalidQa result = table.getItem(r->r.key(key));
        System.out.println("The record id is "+result.getId());

ErvivePdiDataInvalidQa table class is in below comment*

and it is returning "The provided key element does not match the schema (Service: DynamoDb, Status Code: 400, Request ID: PE1MKPMQ9MLT51OLJQVDCURQGBVV4KQNSO5AEMVJF66Q9ASUAAJG, Extended Request ID: null)"

like image 444
PADMAVATHI DESETTI Avatar asked Nov 01 '25 07:11

PADMAVATHI DESETTI


1 Answers

Query you need is documented in one of the examples of AWS Dynamodb Query API for Java.

AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
.withRegion(Regions.US_WEST_2).build();
DynamoDB dynamoDB = new DynamoDB(client);

Table table = dynamoDB.getTable("ervive-pdi-data-invalid-qa");

QuerySpec spec = new QuerySpec()
    .withKeyConditionExpression("SubmissionId = :v_id")
    .withValueMap(new ValueMap()
        .withInt(":v_id", 2146));

ItemCollection<QueryOutcome> items = table.query(spec);

Iterator<Item> iterator = items.iterator();
Item item = null;
while (iterator.hasNext()) {
    item = iterator.next();
    System.out.println(item.toJSONPretty());
}

A single Query operation can retrieve a maximum of 1 MB of data, see documentation

like image 164
A.Khan Avatar answered Nov 03 '25 10:11

A.Khan