Query Hash Key Only in DynamoDB Using Java (Guide w/ Code Examples)

Provided by Rafal Wilinski
In DynamoDB, a query operation allows you to retrieve items from a table based on their primary key values. The primary key is composed of one or two attributes, a partition key (also known as a "hash key"), and an optional sort key (also known as a "range key").
Querying a table using only the partition key is called a "query by hash key" operation. In Java, you can use the DynamoDBMapper class to perform a query by hash key, by creating a DynamoDBQueryExpression object and specifying the partition key value.
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper; import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression; public class QueryHashKeyExample { public static void main(String[] args) { // Create a new DynamoDBMapper AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build(); DynamoDBMapper mapper = new DynamoDBMapper(client); // Define the hash key value to query String hashKeyValue = "123"; // Define the query expression DynamoDBQueryExpression<Product> queryExpression = new DynamoDBQueryExpression<Product>() .withHashKeyValues(new Product(hashKeyValue)) .withConsistentRead(false); // Perform the query List<Product> items = mapper.query(Product.class, queryExpression); // Print the query results for (Product item : items) { System.out.println(item); } } }
Similar Code Examples
- Delete in DynamoDB Using Java
- Get All Items from DynamoDB Using Java
- Remove Item in DynamoDB Using Java
- Update Multiple Items in DynamoDB Using Java
- Batch Update Using DynamoDB Mapper
- Batch Save Using DynamoDB Mapper
- Update Item in DynamoDB Using Java
- Batch Get in DynamoDB Using Java
- Get Item Request in DynamoDB Using Java
- Remove Attribute in DynamoDB Using Java
- Batch Write Using DynamoDB Mapper
- Batch Write Item in DynamoDB Using Java
- Query Date Range in DynamoDB Using Java
- Batch Insert in DynamoDB Using Java
- Remove with DynamoDB Mapper
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