4

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)"

1
  • It is possible, just try to create a query includes both Partition key and Range key, then remove Range key condition part.
    – hoangdv
    Commented May 3, 2021 at 15:07

1 Answer 1

2

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

3
  • Is it possible to query the table and get back a single item, instead of a collection of items, when you know that the value you give for the partition key will return exactly one item?
    – Gillfish
    Commented Jan 24, 2022 at 23:33
  • 1
    Yes, if the Query API finds only single item then it will still be returned in an array Items: [{..}] as opposed to a single object you get in GetItem API response.
    – A.Khan
    Commented Jan 25, 2022 at 0:38
  • 1
    I had a similar question, but an extra doubt to this answer provided. Do we need to use QuerySpec, or we can directly call table.query("SubmissionId", 2146); to get the same results? Because all the examples I see online use QuerySpec Commented Feb 10, 2023 at 2:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.