Request all elements in DynamoDB from a given hash key using a hash range scheme using java sdk

EDIT: I was actually wrong. I was querying a table when I wanted to query an index that explains my error. Vikdor's solution is valid though.

ORIGINAL: I have a table with a hash key scheme in DynamoDB. I need to get all the elements associated with a specific hash key, but it seems that a range key condition is required. My problem is that I want the key "EVERY range", but there is no alternative. Right now, my range key is a string, and the only way I could do this is to request all range keys greater than or equal to the smallest ascii characters that I can use, as the documentation says that it is sorted based on characters ascii character.

I looked at the scan, but it seems to just read the entire table, which is NOT an option.

Is there a better way to request all hash key values, or can someone confirm that using a method with the ascii character will work?

+4
source share
2 answers

but it seems a range key condition is required.

This does not seem true.

I use DynamoDBMapper and use DynamoDBQueryExpression to query all records with a given HashKey as follows:

DynamoDBQueryExpression<DomainObject> query = 
    new DynamoDBQueryExpression<DomainObject>();
DomainObject hashKeyValues = new DomainObject();
hashKeyValues.setHashKey(hashKeyValue);
query.setHashKeyValues(hashKeyValues);
// getMapper() returns a DynamoDBMapper object with the appropriate 
// AmazonDynamoDBClient object.
List<DomainObject> results = getMapper().query(query);

NTN.

+5
source

You can use the DynamoDB query API, which allows you to query conditional expressions based on a database using the hash / range keys. Here you can see API examples here . Here is an example:

ItemCollection<QueryOutcome> items = table.query("theHashFieldName", "theHashFieldToQuery");

You can also request the use of more complex expressions. For instance:.

DynamoDB dynamoDB = new DynamoDB(
    new AmazonDynamoDBClient(new ProfileCredentialsProvider()));

Table table = dynamoDB.getTable("TableName");

QuerySpec spec = new QuerySpec()
    .withKeyConditionExpression("Id = :v_id")
    .withValueMap(new ValueMap()
        .withString(":v_id", "TheId"));

ItemCollection<QueryOutcome> items = table.query(spec);

Iterator<Item> iterator = items.iterator();
Item item = null;
while (iterator.hasNext()) {
    item = iterator.next();
    System.out.println(item.toJSONPretty());
}
+3
source

All Articles