dynobase-icon
Dynobase

Query Local Secondary Index in DynamoDB Using Java (Guide w/ Code Examples)

Rafal Wilinski

Provided by Rafal Wilinski

The following example queries the MyTable table using the FirstNameIndex local secondary index. The key condition expression partitionKey = :val is specified, along with a map of attribute values (expressionAttributeValues) that includes the value "John" mapped to :val. The query returns items in the table where the partitionKey attribute is John.

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.model.AttributeValue; import com.amazonaws.services.dynamodbv2.model.QueryRequest; import com.amazonaws.services.dynamodbv2.model.QueryResult; public class QueryLocalSecondaryIndexExample { public static void main(String[] args) { AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient(); Map<String, AttributeValue> expressionAttributeValues = new HashMap<>(); expressionAttributeValues.put(":val", new AttributeValue().withS("John")); QueryRequest queryRequest = new QueryRequest() .withTableName("MyTable") .withIndexName("FirstNameIndex") .withKeyConditionExpression("partitionKey = :val") .withExpressionAttributeValues(expressionAttributeValues); QueryResult result = client.query(queryRequest); result.getItems().forEach(item -> System.out.println(item)); } }

Better DynamoDB experience.

Try 7-day free trial. No credit card needed.

Product Features

Download
/
Changelog
/
Pricing
/
Member Portal
/
Privacy
/
EULA
/
Twitter
/
Affiliates & Influencers
© 2024 Dynobase
+
Tired of AWS Console?
Try Dynobase to accelerate your DynamoDB workflow. Start your 7-day free trial today.