javaamazon-dynamodbdynamodb-queriesdynamo-localamazon-dynamodb-index

how to get the records using partition key from a composite primary key in dynamodb


I am using java to insert and retrieve records in DynamoDB.

Insertion Code

                    Item item_to_insert = new Item().withPrimaryKey("LatLong", key,"location_code",key)
                        .withJSON("location_address", jsonW.toString())
                        .withString("version","1");
                PutItemOutcome outcome = table.putItem(item_to_insert);
              

Retrieving

                GetItemSpec i_spec = new GetItemSpec()
                        .withPrimaryKey("LatLong", key,"location_code",key)
                        .withProjectionExpression("location_address")
                        .withConsistentRead(true);

Now I want to retrieve the records just using LatLong attribute which is partition key. Any Idea how to do this??


Solution

  • GetItem fetches a specific item, which requires you to specify the complete primary key. If you want to retrieve all items with a common partition key, I believe the method you're looking for is the Query method.

    See more here: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryingJavaDocumentAPI.html

    The code would look something like this:

    QuerySpec spec = new QuerySpec()
        .withKeyConditionExpression("Id = :v_id")
        .withValueMap(new ValueMap()
        .withString(":v_id", "Amazon DynamoDB#DynamoDB Thread 1"));