-
Notifications
You must be signed in to change notification settings - Fork 974
DDBEnhanced - Support to flatten a Map into top level attributes of the object #6102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alextwoods
merged 4 commits into
aws:master
from
anasatirbasa:feature/support-to-flatten-a-map
Oct 29, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c2a145b
Support to flatten a Map into top level attributes of the object
anasatirbasa 3706208
Merge branch 'master' into feature/support-to-flatten-a-map
alextwoods d3038f2
Merge branch 'master' into feature/support-to-flatten-a-map
alextwoods 880387f
Merge branch 'master' into feature/support-to-flatten-a-map
alextwoods File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
.changes/next-release/feature-AmazonDynamoDBEnhancedClient-22723fc.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "type": "feature", | ||
| "category": "Amazon DynamoDB Enhanced Client", | ||
| "contributor": "", | ||
| "description": "Add support for @DynamoDbFlatten to flatten a Map<String, String> to top level attributes of an object" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
...java/software/amazon/awssdk/enhanced/dynamodb/ScanQueryWithFlattenMapIntegrationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package software.amazon.awssdk.enhanced.dynamodb; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
| import static org.hamcrest.Matchers.is; | ||
| import static org.hamcrest.Matchers.nullValue; | ||
| import static software.amazon.awssdk.enhanced.dynamodb.internal.AttributeValues.numberValue; | ||
| import static software.amazon.awssdk.enhanced.dynamodb.internal.AttributeValues.stringValue; | ||
| import static software.amazon.awssdk.enhanced.dynamodb.mapper.StaticAttributeTags.primaryPartitionKey; | ||
| import static software.amazon.awssdk.enhanced.dynamodb.mapper.StaticAttributeTags.primarySortKey; | ||
| import static software.amazon.awssdk.enhanced.dynamodb.mapper.StaticAttributeTags.secondaryPartitionKey; | ||
| import static software.amazon.awssdk.enhanced.dynamodb.mapper.StaticAttributeTags.secondarySortKey; | ||
| import static software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional.sortBetween; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
| import org.junit.AfterClass; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
| import software.amazon.awssdk.enhanced.dynamodb.mapper.StaticTableSchema; | ||
| import software.amazon.awssdk.enhanced.dynamodb.model.Page; | ||
| import software.amazon.awssdk.enhanced.dynamodb.model.QueryEnhancedRequest; | ||
| import software.amazon.awssdk.enhanced.dynamodb.model.Record; | ||
| import software.amazon.awssdk.services.dynamodb.DynamoDbClient; | ||
| import software.amazon.awssdk.services.dynamodb.model.AttributeValue; | ||
|
|
||
| public class ScanQueryWithFlattenMapIntegrationTest extends DynamoDbEnhancedIntegrationTestBase { | ||
|
|
||
| private static final String TABLE_NAME = createTestTableName(); | ||
|
|
||
| private static final TableSchema<Record> RECORD_WITH_FLATTEN_MAP_TABLE_SCHEMA = | ||
| StaticTableSchema.builder(Record.class) | ||
| .newItemSupplier(Record::new) | ||
| .addAttribute(String.class, a -> a.name("id") | ||
| .getter(Record::getId) | ||
| .setter(Record::setId) | ||
| .tags(primaryPartitionKey(), secondaryPartitionKey("index1"))) | ||
| .addAttribute(Integer.class, a -> a.name("sort") | ||
| .getter(Record::getSort) | ||
| .setter(Record::setSort) | ||
| .tags(primarySortKey(), secondarySortKey("index1"))) | ||
| .addAttribute(Integer.class, a -> a.name("value") | ||
| .getter(Record::getValue) | ||
| .setter(Record::setValue)) | ||
| .addAttribute(String.class, a -> a.name("gsi_id") | ||
| .getter(Record::getGsiId) | ||
| .setter(Record::setGsiId) | ||
| .tags(secondaryPartitionKey("gsi_keys_only"))) | ||
| .addAttribute(Integer.class, a -> a.name("gsi_sort") | ||
| .getter(Record::getGsiSort) | ||
| .setter(Record::setGsiSort) | ||
| .tags(secondarySortKey("gsi_keys_only"))) | ||
| .addAttribute(String.class, a -> a.name("stringAttribute") | ||
| .getter(Record::getStringAttribute) | ||
| .setter(Record::setStringAttribute)) | ||
| .flatten("attributesMap", | ||
| Record::getAttributesMap, | ||
| Record::setAttributesMap) | ||
| .build(); | ||
|
|
||
| private static final List<Record> RECORDS_WITH_FLATTEN_MAP = | ||
| IntStream.range(0, 9) | ||
| .mapToObj(i -> new Record() | ||
| .setId("id-value") | ||
| .setSort(i) | ||
| .setValue(i) | ||
| .setStringAttribute(getStringAttrValue(10 * 1024)) | ||
| .setGsiId("gsi-id-value") | ||
| .setGsiSort(i) | ||
| .setAttributesMap(new HashMap<String, String>() {{ | ||
| put("mapAttribute1", "mapValue1"); | ||
| put("mapAttribute2", "mapValue2"); | ||
| put("mapAttribute3", "mapValue3"); | ||
| }})) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| private static DynamoDbClient dynamoDbClient; | ||
| private static DynamoDbEnhancedClient enhancedClient; | ||
| private static DynamoDbTable<Record> mappedTable; | ||
|
|
||
| @BeforeClass | ||
| public static void setup() { | ||
| dynamoDbClient = createDynamoDbClient(); | ||
| enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(dynamoDbClient).build(); | ||
| mappedTable = enhancedClient.table(TABLE_NAME, RECORD_WITH_FLATTEN_MAP_TABLE_SCHEMA); | ||
| mappedTable.createTable(); | ||
| dynamoDbClient.waiter().waitUntilTableExists(r -> r.tableName(TABLE_NAME)); | ||
| } | ||
|
|
||
| @AfterClass | ||
| public static void teardown() { | ||
| try { | ||
| dynamoDbClient.deleteTable(r -> r.tableName(TABLE_NAME)); | ||
| } finally { | ||
| dynamoDbClient.close(); | ||
| } | ||
| } | ||
|
|
||
| private void insertRecords() { | ||
| RECORDS_WITH_FLATTEN_MAP.forEach(record -> mappedTable.putItem(r -> r.item(record))); | ||
| } | ||
|
|
||
| @Test | ||
| public void queryWithFlattenMapRecord_correctlyRetrievesProjectedAttributes() { | ||
| insertRecords(); | ||
|
|
||
| Iterator<Page<Record>> results = | ||
| mappedTable.query(QueryEnhancedRequest.builder() | ||
| .queryConditional(sortBetween(k -> k.partitionValue("id-value").sortValue(2), | ||
| k -> k.partitionValue("id-value").sortValue(6))) | ||
| .attributesToProject("mapAttribute1", "mapAttribute2") | ||
| .limit(3) | ||
| .build()) | ||
| .iterator(); | ||
|
|
||
| Page<Record> page1 = results.next(); | ||
| assertThat(results.hasNext(), is(true)); | ||
| Page<Record> page2 = results.next(); | ||
| assertThat(results.hasNext(), is(false)); | ||
|
|
||
| Map<String, String> expectedAttributesMap = new HashMap<>(); | ||
| expectedAttributesMap.put("mapAttribute1", "mapValue1"); | ||
| expectedAttributesMap.put("mapAttribute2", "mapValue2"); | ||
|
|
||
| List<Record> page1Items = page1.items(); | ||
| assertThat(page1Items.size(), is(3)); | ||
| assertThat(page1Items.get(0).getAttributesMap(), is(expectedAttributesMap)); | ||
| assertThat(page1Items.get(1).getAttributesMap(), is(expectedAttributesMap)); | ||
| assertThat(page1Items.get(2).getAttributesMap(), is(expectedAttributesMap)); | ||
| assertThat(page1.consumedCapacity(), is(nullValue())); | ||
| assertThat(page1.lastEvaluatedKey(), is(getKeyMap(4))); | ||
| assertThat(page1.count(), equalTo(3)); | ||
| assertThat(page1.scannedCount(), equalTo(3)); | ||
|
|
||
| List<Record> page2Items = page2.items(); | ||
| assertThat(page2Items.size(), is(2)); | ||
| assertThat(page2Items.get(0).getAttributesMap(), is(expectedAttributesMap)); | ||
| assertThat(page2Items.get(1).getAttributesMap(), is(expectedAttributesMap)); | ||
| assertThat(page2.lastEvaluatedKey(), is(nullValue())); | ||
| assertThat(page2.count(), equalTo(2)); | ||
| assertThat(page2.scannedCount(), equalTo(2)); | ||
| } | ||
|
|
||
| private Map<String, AttributeValue> getKeyMap(int sort) { | ||
| Map<String, AttributeValue> result = new HashMap<>(); | ||
| result.put("id", stringValue(RECORDS.get(sort).getId())); | ||
| result.put("sort", numberValue(RECORDS.get(sort).getSort())); | ||
| return Collections.unmodifiableMap(result); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.