I have a DynamoDB table where each Item has a key of the name 'DataType'. Also there is a GSI on this table with this 'DataType' as the HashKey and 'timestamp' as rangeKey.
Around 10 per cent of the table items have the 'DataType' value as 'A'.
I want to scan all the items of this GSI with HashKey fixed as 'A'. Is there any way to perform this using scan/parallel scan? Or do i need to use query on GSI itself to perform this operation?
As per the documentation, https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/document/spec/ScanSpec.html
i could not find any way to specify a GSI on which i can scan with HashKey fixed.
Given that you want to only look at items with the Hash key "A", you'll need to use the Query API rather than the Scan API, provide the index name, and query for items in the index that have that partition key.
Here's some sample code using the Node AWS SDK V3 that creates three items in a table with a Global Secondary Index (called
GSI1). Two of the items have aGSI1PKvalue of"orange", while the other has aGSI1PKvalue of"gold". The query returns the two matches:This produces the output of:
One thing worth noting from this example is that GSIs don't allow consistent reads. So if your use case requires immediate consistency, you'll need to find another solution.