Search in sources :

Example 1 with QueryRequest

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;
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) QueryRequest(software.amazon.awssdk.services.dynamodb.model.QueryRequest) HashMap(java.util.HashMap) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) QueryResponse(software.amazon.awssdk.services.dynamodb.model.QueryResponse)

Aggregations

HashMap (java.util.HashMap)1 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)1 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)1 QueryRequest (software.amazon.awssdk.services.dynamodb.model.QueryRequest)1 QueryResponse (software.amazon.awssdk.services.dynamodb.model.QueryResponse)1