1

I have the following code:-

    Map<String, AttributeValue> eav = new HashMap<String, AttributeValue>();
    eav.put(":val1", new AttributeValue().withS("12"));

    ScrapCategoryDO myObject = new ScrapCategoryDO();
    myObject.setCategoryName("Iron");
    DynamoDBQueryExpression<ScrapCategoryDO> queryExpression = new DynamoDBQueryExpression<ScrapCategoryDO>()
            //.withHashKeyValues(myObject)
            .withFilterExpression("UnitPrice < :val1")
            .withExpressionAttributeValues(eav);

    List<ScrapCategoryDO> latestReplies = mapper.query(ScrapCategoryDO.class, queryExpression);
    Log.d(TAG, "Number of categories = "+latestReplies.size());
    for (ScrapCategoryDO reply : latestReplies) {
        Log.d(TAG,"category name = "+reply.getCategoryName()+", unit price = "+ reply.getUnitPrice());
    }

And I am getting the following error on with .withHashKeyValues(myObject) as commented. I want to know how can I query the entire table without any filter criteria/condition or so? I tried :-

     DynamoDBQueryExpression<ScrapCategoryDO> queryExpression = new DynamoDBQueryExpression<ScrapCategoryDO>();

But still I was getting the same error.

W/System.err﹕ java.lang.IllegalArgumentException: Illegal query expression: No hash key condition is found in the query
W/System.err﹕ at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.processKeyConditions(DynamoDBMapper.java:2424)
W/System.err﹕ at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.createQueryRequestFromExpression(DynamoDBMapper.java:2386)
W/System.err﹕ at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.query(DynamoDBMapper.java:2169)
W/System.err﹕ at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.query(DynamoDBMapper.java:2130)
W/System.err﹕ at com.example.rohitsingla.scrapman.MyActivity.FindAllScrapCategories(MyActivity.java:207)
W/System.err﹕ at com.example.rohitsingla.scrapman.MyActivity.access$000(MyActivity.java:26)
W/System.err﹕ at com.example.rohitsingla.scrapman.MyActivity$3.run(MyActivity.java:164)

1 Answer 1

1

Please use scan API to get all the records from DynamoDB table. Here is the sample code.

Query API can't be used without the key attributes.

Sample code:-

ScanResultPage<yourModelClass> yourModelClassResultPage = null;
do {
    DynamoDBMapper dynamoDBMapper = new DynamoDBMapper(dynamoDBClient);
    DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
    if (yourModelClassResultPage != null) {
        scanExpression.setExclusiveStartKey(yourModelClassResultPage.getLastEvaluatedKey());
    }
    yourModelClassResultPage = dynamoDBMapper.scanPage(yourModelClass.class, scanExpression);   

} while (yourModelClassResultPage.getLastEvaluatedKey() != null);

Scan API documentation

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the answer. It's working. But I want to set my "CategoryName" field as a key. I am not able to update my table by going to console. need help in that.
If you have defined CategoryName as key, it can't be updated once inserted. Would appreciate if you can accept the answer and open new question if there is a new problem for everyone's benefit and brevity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.