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

Provided by Rafal Wilinski
Last updated: October 23rd, 2026
Set IndexName on Query and use the index key attributes in the key condition. This example assumes CustomerIndex has customerId as its partition key and projects the fields you need.
This example uses Java 17+ and the software.amazon.awssdk:dynamodb SDK 2.x dependency. Configure an AWS Region and credentials through your usual AWS profile or runtime role. The Orders table has String keys pk and sk; adapt the table and keys to your schema.
import java.util.*; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.*; public class Example { static AttributeValue s(String value) { return AttributeValue.builder().s(value).build(); } static Map<String, AttributeValue> key(String order) { return Map.of("pk", s("CUSTOMER#123"), "sk", s(order)); } public static void main(String[] args) throws InterruptedException { try (DynamoDbClient ddb = DynamoDbClient.create()) { QueryRequest request = QueryRequest.builder().tableName("Orders") .indexName("CustomerIndex") .keyConditionExpression("#pk = :pk") .expressionAttributeNames(Map.of("#pk", "customerId")) .expressionAttributeValues(Map.of(":pk", s("CUSTOMER#123"))).build(); for (QueryResponse page : ddb.queryPaginator(request)) { page.items().forEach(System.out::println); } } } }
Global secondary indexes support eventually consistent reads only. Query returns projected attributes; fetch full table items separately if needed.
Reference: AWS Query API. For related access patterns, see DynamoDB query examples.
Similar Code Examples
- Delete Multiple Items in DynamoDB Using Java
- Update Expression in DynamoDB Using Java
- Update Multiple Attributes in DynamoDB Using Java
- Delete All Items in DynamoDB Using Java
- Delete in DynamoDB Using Java
- GetItem in DynamoDB Using Java
- Batch Query in DynamoDB Using Java
- Batch Write Item in DynamoDB Using Java
- Get Multiple Items in DynamoDB Using Java
- Delete by Hashkey Using DynamoDB Mapper
- Batch Get in DynamoDB Using Java
- Batch Insert in DynamoDB Using Java
- Query Index in DynamoDB Using Java
- Conditional Update in DynamoDB Using Java
- Batch Save Using DynamoDB Mapper
Dynobase is a Professional GUI Client for DynamoDB
Start your 7-day free trial today
Product Features
DynamoDB Tools
- SSH GUI Client
- Chat With Cloud
- MongoDB GUI Client
- AWS Cost Monitoring
- DynamoDB Query Builder
- DynamoDB Code Examples
- DynamoDB Node.js Queries
- DynamoDB JSON Converter
- DynamoDB UUID Generator
- DynamoDB Local Admin GUI
- DynamoDB Pricing Calculator
- DynamoDB Single-Table Design
- Best DynamoDB GUI Clients
- Dynobase vs DynoTable