How To Query For All Partition Keys in DynamoDB?

Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability.

With DynamoDB, you can create database tables that can store and retrieve any amount of data, and serve any level of request traffic.

How to put data in AWS Kinesis Stream

The following code shows how to query a DynamoDB to retrieve all partition keys.

import com.amazonaws.client.builder.AwsClientBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PartitionKeys {
    static String serviceEndpoint = "";
    static String region = "eu-west-1";

    static AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
            .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(serviceEndpoint, region))

    public static List<String> getAllPartitionKeys() {
        List<String> partitionKeys = new ArrayList<>(0);
        // dynamo returns results in chunks - 
        // you'll need this to get the next one
        Map<String, AttributeValue> lastKeyEvaluated = null;

        do {
            ScanRequest scanRequest = new ScanRequest()

            ScanResult result = client.scan(scanRequest);
            for (Map<String, AttributeValue> item : result.getItems()){
                // for each item in the result set, examine the partition key
                // to determine if it's a match
                String key = item.get("YOUR_PARTITION_KEY").getS();
            lastKeyEvaluated = result.getLastEvaluatedKey();
        } while (lastKeyEvaluated != null);

        return partitionKeys;

Leave a Reply