Roy
Roy

Reputation: 741

DynamoDB Java table.query exception

I am executing this code from my Elastic beanstalk instance. In Dynamodb, I have a table with hash and range key. I need to find all rows that matches my hash key. I don't want to specify the range. Similar query on that table works fine on AWS Console. I followed this help.

DynamoDB db = new DynamoDB(new AmazonDynamoDBClient(new ProfileCredentialsProvider()));
KeyAttribute key = new KeyAttribute("ID", new AttributeValue().withS("123"));
QuerySpec querySpec = new QuerySpec().withHashKey(key);
Table table = db.getTable("USER_TABLE");
ItemCollection<QueryOutcome> items = null;
try
{
            items = table.query(querySpec);
}
catch (Exception e)
{
            log.severe(String.format("table.query exception " + e.getMessage()));
}

The table.query call is throwing an Exception: "value type: class com.amazonaws.services.dynamodbv2.model.AttributeValue"

I don't see any reason for the exception and I am stuck. I posted the same question on AWS forum but no answer in there yet - any help is much appreciated.

Upvotes: 0

Views: 512

Answers (1)

Chen Harel
Chen Harel

Reputation: 10052

The problem is with this line:

KeyAttribute key = new KeyAttribute("ID", new AttributeValue().withS("123"));

KeyAttribute doesn't require AttributeValue but rather an Object that represents the value itself so in your case you should create the KeyAttribute like this:

KeyAttribute key = new KeyAttribute("ID", "123");

Upvotes: 1

Related Questions