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

Provided by Rafal Wilinski
To query a global secondary index (GSI) in a DynamoDB table in Java, you can use the query method of the AmazonDynamoDB client by providing the GSI Name, Table Name, and Key Condition Expression.
In the below example, YourTableName is the name of the table that contains the GSI you want to query, YourGSI is the name of the GSI you want to query, GSIKeyAttribute is the name of the key attribute on the GSI, and YourSearchValue is the value you want to search for in the GSI.
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.model.Attribute.Attributevalue; import com.amazonaws.services.dynamodbv2.model.queryrequest; import com.amazonaws.services.dynamodbv2.model.queryresult; public class DynamoDBQueryGSIExample { public static void main(String[] args) { AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient(); QueryRequest request = new QueryRequest() .withTableName("YourTableName") .withIndexName("YourGSI") .withKeyConditionExpression("GSIKeyAttribute = :value") .withExpressionAttributeValues( new AttributeValue().withS(":value", "YourSearchValue")); QueryResult result = client.query(request); System.out.println("Query successful!"); System.out.println(result); } }
Similar Code Examples
- Get All Items from DynamoDB Using Java
- Batch Save Using DynamoDB Mapper
- Delete All Items in DynamoDB Using Java
- Delete Multiple Items in DynamoDB Using Java
- Batch Write Using DynamoDB Mapper
- Update Multiple Items in DynamoDB Using Java
- Query Hash Key Only in DynamoDB Using Java
- Batch Update Using DynamoDB Mapper
- GetItem in DynamoDB Using Java
- Batch Read in DynamoDB Using Java
- Batch Get in DynamoDB Using Java
- Update Expression in DynamoDB Using Java
- Query Index in DynamoDB Using Java
- Query Date Range in DynamoDB Using Java
- Batch Write Item 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