Reputation: 4388
I am fetching data from AWS DynamoDB Table. With the below code, I can fetch single item from table with this code.
Condition hashKeyCondition = new Condition()
.withComparisonOperator(ComparisonOperator.GE.toString())
.withAttributeValueList(new AttributeValue().withN("1"));
Map<String, Condition> keyConditions = new HashMap<String, Condition>();
keyConditions.put("ID", hashKeyCondition);
Map<String, AttributeValue> lastEvaluatedKey = null;
do
{
QueryRequest queryRequest = new QueryRequest()
.withTableName("TABLE_NAME")
.withKeyConditions(keyConditions)
.withExclusiveStartKey(lastEvaluatedKey);
QueryResult queryResult = dynamoDBClient.query(queryRequest);
for (Map<String, AttributeValue> item : queryResult.getItems())
{
String value = item.get("column_name").getS();
Log.i("MainActivity", value);
}
lastEvaluatedKey = queryResult.getLastEvaluatedKey();
} while (lastEvaluatedKey != null);
But I am trying to fetch all the items that are greater than ID = "1". So I changed the ComparisonOperator.GE
to ComparisonOperator.GT
. Below is the following code.
Condition hashKeyCondition = new Condition()
.withComparisonOperator(ComparisonOperator.GT.toString())
.withAttributeValueList(new AttributeValue().withN("1"));
Map<String, Condition> keyConditions = new HashMap<String, Condition>();
keyConditions.put("ID", hashKeyCondition);
Map<String, AttributeValue> lastEvaluatedKey = null;
do
{
QueryRequest queryRequest = new QueryRequest()
.withTableName("TABLE_NAME")
.withKeyConditions(keyConditions)
.withConsistentRead(true)
.withExclusiveStartKey(lastEvaluatedKey);
QueryResult queryResult = dynamoDBClient.query(queryRequest);
for (Map<String, AttributeValue> item : queryResult.getItems())
{
String value = item.get("column_name").getS();
Log.i("MainActivity", value);
}
lastEvaluatedKey = queryResult.getLastEvaluatedKey();
} while (lastEvaluatedKey != null);
But I am getting AmazonServiceException
com.amazonaws.AmazonServiceException: Query key condition not supported (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: XXXXXXXXXXXXXXXXXXXXXXXXXXX
.
Please Help.
Upvotes: 0
Views: 3043
Reputation: 289
My guess is that this would be an error in the data schema of the table. If you don't have a range key established for Id you may not be able to sort on it. I believe the answer to your problem is located in this document.
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryingJavaDocumentAPI.html
Specifically the statement below stands out:
Specifying Optional Parameters
The query method supports several optional parameters. For example, you can optionally narrow the results from the preceding query to return replies in the past two weeks by specifying a condition. The condition is called a sort key condition, because DynamoDB evaluates the query condition that you specify against the sort key of the primary key. You can specify other optional parameters to retrieve only a specific list of attributes from items in the query result.
Maybe try adding a range key or secondary index and see if that would support your id query?
Upvotes: 0
Reputation: 1657
I'm a bit surprised the first piece of code works, but it might be a product of the SDK in use. You can only use equals (EQ) when querying the primary key (hash). You can use the other operators when the query includes a condition that checks against the sort key (range).
"The selection criteria for the query. For a query on a table, you can have conditions only on the table primary key attributes. You must provide the partition key name and value as an EQ condition. You can optionally provide a second condition, referring to the sort key."
Source: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html
Upvotes: 3