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

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)); } }
Similar Code Examples
- Batch Query in DynamoDB Using Java
- Get Item Request in DynamoDB Using Java
- Update Item in DynamoDB Using Java
- Batch Insert in DynamoDB Using Java
- Remove Item in DynamoDB Using Java
- Remove Attribute in DynamoDB Using Java
- Batch Delete Using DynamoDB Mapper
- Delete All Items in DynamoDB Using Java
- Batch Write Item in DynamoDB Using Java
- Delete by Hashkey Using DynamoDB Mapper
- Update Multiple Attributes in DynamoDB Using Java
- Update Multiple Items in DynamoDB Using Java
- Batch Read in DynamoDB Using Java
- Query Date Range in DynamoDB Using Java
- Delete in DynamoDB Using Java
Dynobase is a Professional GUI Client for DynamoDB
Start your 7-day free trial today
Product Features
- New in Dynobase 3.0
- Semantic and Vector Search
- Schema Explorer
- Visual JSON Path Filtering
- Visual Item Diff
- MCP Server for AI Tools
- Use SQL with DynamoDB
- Import CSV To DynamoDB
- Import JSON To DynamoDB
- Export DynamoDB To JSON
- Export DynamoDB To S3
- Export DynamoDB To CSV
- DynamoDB Update Item
- DynamoDB Delete All Items
- DynamoDB Delete Item
- DynamoDB Create Table
- DynamoDB Delete Table
- DynamoDB Conditional Update
DynamoDB Tools
DynamoDB Info
© 2026 Dynobase