9
votes

I started using Amazon DynamoDB and have an issue with query.

I have a table Dev_Testgame1_Mail with id as primary hash key and following three global secondary indexes,

  1. fromPlayerId (Hash Key)
  2. toPlayerId (Hash Key) + isRead (Range Key)
  3. toPlayerId (Hash Key) + endDate(Range Key)

I have above code to do the query,

**DynamoDBMail hashKObject = new DynamoDBMail();
            hashKObject.setToPlayerId(playerId);
Condition endDateRangeKeyCondition = new Condition();
            //endDateRangeKeyCondition.withComparisonOperator(ComparisonOperator.NULL).withAttributeValueList(new AttributeValue().withB(Utils.convertDateToByteBuffer(DateUtil.getUtcDateTime())));
            endDateRangeKeyCondition.withComparisonOperator(ComparisonOperator.NULL);

            DynamoDBQueryExpression<DynamoDBMail> queryExpression = new DynamoDBQueryExpression<DynamoDBMail>();
            queryExpression.withHashKeyValues(hashKObject).withRangeKeyCondition("endDate", endDateRangeKeyCondition);
            queryExpression.withIndexName("gsi_tp_enddt").withLimit(pageSize).withScanIndexForward(false);
            return dynamodbMapper.queryPage(DynamoDBMail.class, queryExpression, new DynamoDBMapperConfig(TableNameOverride.withTableNamePrefix(Utils.getDynamoDBTableNamePrefix(gameId, env))));**

And I get the following error,

com.amazonaws.AmazonServiceException: Status Code: 400, AWS Service: AmazonDynamoDBv2, AWS Request ID: GUUBV24K2O40T276R9NNN0EKB7VV4KQNSO5AEMVJF66Q9ASUAAJG, AWS Error Code: ValidationException, AWS Error Message: Consistent reads are not supported on global secondary indexes

Your help is much appreciated on this issue.

Thanks Arun

1

1 Answers

19
votes

Set the consistent read property of the queryExpression to false. Put this line:

queryExpression.withConsistentRead(false);

or

queryExpression.setConsistentRead(false);

before you call

return dynamodbMapper.queryPage(DynamoDBMail.class, queryExpression, new DynamoDBMapperConfig(TableNameOverride.withTableNamePrefix(Utils.getDynamoDBTableNamePrefix(gameId, env))));

As an aside, I don't think your NULL RangeKeyCondition is doing anything for you.