简体   繁体   中英

Query DynamoDB in Java

How do you query/scan a DynamoDB table using only the primary key? The only way I have been able to return results from the table is the code below, where I set the primary key Id and add a Condition using the sort key Date .

Is there any way to query the table without supplying a 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());

Figured it out. You do not have to include the 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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM