Skip to content

Instantly share code, notes, and snippets.

Created May 8, 2021 09:48
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
#DynamoDB #JavaSDK #AWS
private Result getPeopleByGSI(@NotNull final String indexedReference, final String... attributes) {
// make reference the index
final DynamoDbIndex<People> index = DynamoUtil.peopleTable.index("someIndexedAttribute-index");
// Apply value to the indexed attribute (as the PartitionKey)
final QueryConditional queryConditional = QueryConditional.keyEqualTo(
// Compose Filter expressions
final List<String> expressions = new ArrayList<>();
final Expression.Builder expressionBuilder = Expression.builder();
// Build other
if (attributes[0] != null) {
expressions.add("contains(#attribute1, :attribute1)");
expressionBuilder.putExpressionName("#attribute1", "attribute1");
expressionBuilder.putExpressionValue(":attribute1", AttributeValue.builder()
// etc...
if (attribute[n] != null) {
expressions.add("#attributeN = :attributeN");
expressionBuilder.putExpressionName("#attributeN", "attributeN");
expressionBuilder.putExpressionValue(":attributeN", AttributeValue.builder()
expressionBuilder.expression(String.join(" AND ", expressions));
final QueryEnhancedRequest.Builder requestBuilder = QueryEnhancedRequest.builder()
.consistentRead(false); // ⚠️ ConsistentRead is _not_ supported on queries with GSIs
final QueryEnhancedRequest request;
if (expressions.isEmpty()) {
// ⚠️ Empty Filter-Expressions are not allowed!
request =;
} else {
request = requestBuilder.filterExpression(
// Run the query
final SdkIterable<Page<People>> pagesInterable = index.query(request);
// map the pages to items
// ⚠️ Pages returned my be empty
final List<People> people =
return Response.ok(people).build();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment