用Java查询DynamoDB

时间:2018-09-02 19:32:29

标签: java amazon-dynamodb

如何仅使用主键查询/扫描DynamoDB表?我能够从表中返回结果的唯一方法是下面的代码,其中设置主键Id并使用排序键Condition添加Date

是否可以在不提供RangeKeyCondition的情况下查询表?

DynamoDataObject data = new DynamoDataObject();
data.setId(userId);

Condition rangeKeyCondition = new Condition()
    .withComparisonOperator(ComparisonOperator.BEGINS_WITH)
    .withAttributeValueList(new AttributeValue().withS("2018"));

DynamoDBQueryExpression queryExpression = new DynamoDBQueryExpression()
    .withHashKeyValues(data)
    .withRangeKeyCondition("Date", rangeKeyCondition)
    .withConsistentRead(false);

PaginatedList<DynamoDataObject > result = AWSProvider.getInstance().getDynamoDBMapper().query(DynamoDataObject.class, queryExpression);
Log.d(TAG, "PaginatedList length: " + result.size());

1 个答案:

答案 0 :(得分:0)

弄清楚了。您不必包括withRangeKeyCondition

DynamoDataObject data = new DynamoDataObject();
data.setId(userId);

DynamoDBQueryExpression queryExpression = new DynamoDBQueryExpression()
    .withHashKeyValues(data)
    .withConsistentRead(false);

PaginatedList<DynamoDataObject > result = AWSProvider.getInstance().getDynamoDBMapper().query(DynamoDataObject.class, queryExpression);
Log.d(TAG, "PaginatedList length: " + result.size());