Querying Dynamo DB Indexes: GSI and LSI - amazon-web-services

I am using the mobile application to query Dynamo DB tables. I have used the below query to fetch an item from the Dynamo DB Test table:
Test t = mapper.load(Test.class, DynamoDBHashKey, DynamoDBRangeKey);
My question is how to query an item from a Global Secondary Index? I have defined the annotations and parameters correctly in the Java class of the Test table.
Is there any other method to query Global Secondary Indexes and the Local Secondary Indexes.

The load api cannot be used to query GSI. The Query API can be used to query the GSI with key attributes.
Sample code:-
Map<String, AttributeValue> vals = new HashMap<>();
vals.put(":val1", new AttributeValue().withS("somevalue"));
DynamoDBQueryExpression<modelclass> queryExp = new DynamoDBQueryExpression<modelclass>()
.withKeyConditionExpression("category = :val1").withIndexName("indexname")
.withExpressionAttributeValues(vals);
dynamoDBMapper.query(modelclass.class, queryExp);
DynamodbQueryExpression class

Related

How to fetch all the values of one column using column name in dynamoDB table in java?

I have a dynamoDB table in my aws account. I can create client like this.
AmazonDynamoDB amazonDynamoDB =
AmazonDynamoDBClient.builder().withRegion("eu-west-1").withCredentials(creds).build();
DynamoDB dynamoDB = new DynamoDB(amazonDynamoDB);
Table table = dynamoDB.getTable("table name");
Suppose there is column name "content". I want to get a list or set of all values in "content" column.

fetch data in map from repository using JPA query

I want to get the data from table using this query:
select clientId, Count(countryID) from table groupBy countryID;
which gives me the count of country corresponding to clientId. Normally we fetch into the List<Object>.
How i can fecth these details into a Map<clientId, countryCount> with clientId as key and countryCount as value?

DynamoDB Can I query just the values of a GSI Index when it is a GSI with sort Key

I have a DynamoDB Table "Music". On this it has a GSI with partition key "Category" and sort key "UserRating".
I can query easily as an example for songs that are in "Category" = "Rap" and "UserRating" = 1
What I would like to do is query and just get back all the "Categories". As this is a a GSI and the partition key I have heard you can do it but I am not sure how.
Is it possible or will I have to create a separate GSI on "Category" without the sort key.
Thanks for your help.
When you don't want to filter by key. You may need to scan the index. The below solution is scanning the index to get all the category (not all distinct category).
Please find below the Java code to get all the Category from GSI. Replace the secondary index name in the below code accordingly.
List<String> categoryList = new ArrayList<>();
DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);
Table table = dynamoDB.getTable("Music");
Index index = table.getIndex("Secondary Index Name");
ItemCollection<ScanOutcome> items = null;
ScanSpec scanSpec = new ScanSpec().withSelect(Select.SPECIFIC_ATTRIBUTES).withAttributesToGet("Category");
items = index.scan(scanSpec);
Iterator<Item> pageIterator = items.iterator();
while (pageIterator.hasNext() ) {
categoryList.add(pageIterator.next().getString("Category"));
}

Can you query by a range in a GSI in DynamoDB

Suppose I had a users and images tables in DynamoDB.
users table
userId (hash) name email
images table
imagesId (hash) userId filename
How should I set up the range or GSI if I wanted to get all images for a single userId? Should images table be a composite key with imageId (hash) and userId (range) and search by range (if possible)? or should userId (be a GSI) and query just be userId?
It looks like you could add a GSI on your Images table with the reverse key schema of your table:
hash key - userId
range key - imagesId
filename - additional property
You can then query this GSI with a userId to get all of its associated imagesId.
You cannot query using just a range key in DynamoDB.
Unless you want your query results from the images table to be sorted by userId, there's no point to using it as a range key. If you want to retrieve all of the images with a certain userId from the images table using a query, you need to use a GSI on userId.

DyanamoDB - Global Secondary Index using GetItemRequest using Hash and Range

I am trying to use the Java AWS sdk to get a document based on a Global Secondary Index.
Setup as follows:
Hash Key: MyId - Number
Range Key: MyDate - String
Here is my code to:
Map<String, AttributeValue> key = new HashMap<String, AttributeValue>();
key.put("MyId", new AttributeValue().withN("1234"));
key.put("MyDate", new AttributeValue().withS("2014-10-12"));
GetItemRequest go = new GetItemRequest().withTableName(tableName).withKey(key);
GetItemResult result = getDynamoDBClient().getItem(gi);
But this always returns :
The provided key element does not match the schema (Service:
AmazonDynamoDBv2; Status Code: 400
What am I dong wrong?
A few notes, first you talk about GSI but you are doing GetItemRequest by primary key. So perhaps you're missing something in your question.
Did you write in your question the primary key of the table or the GSI definition?
You can only Query by GSI, Get is still based on primary key.