6
votes

I'm trying to query a dynamodb table using the partition key and a sort key. The sort key is a unix date, so I want to request x partition key between these 2 dates on the sort. I am currently able to achieve this with a table scan, but I have to move this to a query for the speed benefit. I am unable to find any decent examples online of people using a partition key and sort key to query their table.

I have carefully read through this https://docs.aws.amazon.com/sdk-for-go/api/service/dynamodb/#DynamoDB.Query and understand that my params must go within the KeyConditionExpression.

I have read through https://github.com/aws/aws-sdk-go/blob/master/service/dynamodb/expression/examples_test.go and understand it on the whole. But I just can't find the syntax for KeyConditionExpression

I'd have thought it was something like this:

keyCond := expression.Key("accountId").
    Equal(expression.Value(accountId)).
    And(expression.Key("sortKey").
    Between(expression.Value(fromDateDec), expression.Value(toDateDec)))

But this throws:

ValidationException: Invalid KeyConditionExpression: Incorrect operand type for operator or function; operator or function: BETWEEN, operand type: NULL

1
Could it be that the error message is accurate? I.e., that fromDateDec or toDateDec has a NULL value?Nadav Har'El
Yes it was. toDate was supposed to be in64 rather than decimal.... i had read the error message incorrectly as thoguht the error was with me using the between functionrlou

1 Answers

0
votes

First you need KeyAnd to combine Hash Key and sort key condition.

// keyCondition represents the key condition where the partition key
// "TeamName" is equal to value "Wildcats" and sort key "Number" is equal
// to value 1
keyCondition := expression.KeyAnd(expression.Key("TeamName").Equal(expression.Value("Wildcats")), expression.Key("Number").Equal(expression.Value(1)))

Now instead equal condition you can replace with your between condition as follows

// keyCondition represents the boolean key condition of whether the value
// of the key "foo" is between values 5 and 10
keyCondition := expression.KeyBetween(expression.Key("foo"), expression.Value(5), expression.Value(10))