Mikki
Mikki

Reputation: 138

Dynamodb - Having mulitple rangekey conditions for the same hash key

Could i know if there is a provision of having multiple range key conditions for the same hash key. For example,if the hash key is 'locality' and rangekey is 'shopNo'. Then could we have the below keyconditionset:

>HashKeyCondition: locality EQ  'NewYork'  
>RangeKeyCondition1: shopNo BETWEEN  1 to 10  
>RangeKeyCondition2: shopNo BETWEEN  20 to 30

When the above was tried, it was considering only one rangeKeyCondition . Is there any way that we can achieve giving two rangekeyconditions for the same hashkey. I am supposing that it could be possible since for a given hashkey, all the rangekeys under this partition are indexed via a 'sortedindex'.

I can see this can be achieved using a 'QueryFilter' with 'OR' ConditionalOperator but the filter is applied after the key-based retrieval and before the results are returned to you. This may not be efficient, as getting all shopNo's for a locallity and then performing a filter could be slower than getting the only items which satisfy the provided multiple rangekey conditions

Could anyone advice on how can this be achieved?

Thanks.

Upvotes: 1

Views: 1262

Answers (1)

Ben Schwartz
Ben Schwartz

Reputation: 1756

I think the best approach here is to make multiple Query API calls to DynamoDB in parallel with a subset of the results in each request.

For example:

Request 1: HashKeyCondition: locality EQ 'NewYork' AND RangeKeyCondition1: shopNo BETWEEN 1 to 10

Request 2: HashKeyCondition: locality EQ 'NewYork' AND RangeKeyCondition2: shopNo BETWEEN 20 to 30

In the Java SDK there is a AmazonDynamoDBAsyncClient that provides the API call queryAsync: https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/AmazonDynamoDBAsyncClient.html#queryAsync%28com.amazonaws.services.dynamodbv2.model.QueryRequest%29

Upvotes: 1

Related Questions