use of software.amazon.awssdk.services.dynamodb.model.QueryRequest in project aws-doc-sdk-examples by awsdocs.
the class Query method queryTable.
// snippet-start:[dynamodb.java2.query.main]
public static int queryTable(DynamoDbClient ddb, String tableName, String partitionKeyName, String partitionKeyVal, String partitionAlias) {
// Set up an alias for the partition key name in case it's a reserved word
HashMap<String, String> attrNameAlias = new HashMap<String, String>();
attrNameAlias.put(partitionAlias, partitionKeyName);
// Set up mapping of the partition name with the value
HashMap<String, AttributeValue> attrValues = new HashMap<String, AttributeValue>();
attrValues.put(":" + partitionKeyName, AttributeValue.builder().s(partitionKeyVal).build());
QueryRequest queryReq = QueryRequest.builder().tableName(tableName).keyConditionExpression(partitionAlias + " = :" + partitionKeyName).expressionAttributeNames(attrNameAlias).expressionAttributeValues(attrValues).build();
try {
QueryResponse response = ddb.query(queryReq);
return response.count();
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
return -1;
}
Aggregations