amazon-dynamodbdynamo-local

Does sortkey field is mandatory as part of search criteria while retrieving data from DynamoDB?


I'm trying to run DynamoDB queries on a table containing HashKey and SortKey fields.

While doing get-item operation, when I provide key with both hashkey, sortkey fields I'm seeing the results. But when I tried with only hashkey field getting the following Exception:

An error occurred (ValidationException) when calling the GetItem operation: One of the required keys was not given a value

Can't we get DynamoDB data based on hashkey only?


Solution

  • When we use both partition key and sort key for a table, the primary key generated based on both. Which means there could be multiple items with the same partition key (hash key).

    For an example,

    partition key | sort key  
    1 | A  
    1 | B  
    2 | A  
    2 | B 
    

    In order to perform get operation, you need to specify the primary key of the item. In your table, the hash key is not the primary key. You need to specify both partition key and sort key to get an item from the table.

    Further, if you are using query operation then specifying only partition key will work.