aws-sdk-java-v2 icon indicating copy to clipboard operation
aws-sdk-java-v2 copied to clipboard

[DRAFT]Provide JSON helper methods to input and output data to DynamoDB in Json Format

Open joviegas opened this issue 3 years ago • 2 comments

Motivation and Context

  • PR created to review the design approach, will update the java docs and detailed test cases in future revision of this PR

  • Issue-1862 Preserve JSON helper methods in the DynamoDB Document API #1862

  • Currently V2 did not have a way to input/output in json format.

Modifications

  • Added a Public class called JsonItem

 public static JsonItem fromJson(String jsonString) {}
 public static JsonItem fromAttributeValueMap(Map<String, AttributeValue> attributeMap) {}

 public String toJson(){};

 public boolean contains(String key)

 public String toString()

  • Rest of the classes are Internal.

  • Added convertors to convert from AttributeValue to Json and vice-versa.

In order to create DDB Enhanced client table which Supports Json , we need to create the Table with Json Schema as below

 TableSchema.withDocumentSchema(
              StaticTableMetadata.builder().addIndexPartitionKey(
                        TableMetadata.primaryIndexName(), "hash-key", AttributeValueType.S).build()));

Note : The StaticTableMetadata needs to be built by the user since we need to specify the Table meta data like primary key and sort key etc , this is one time operation.


        DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder()
                                               .dynamoDbClient(getDynamoDbClient())
                                               .build();

        DynamoDbTable<JsonItem> mappedTable1 = enhancedClient.table(getConcreteTableName("table-name-1"),
                                            TableSchema.withDocumentSchema(
                                                StaticTableMetadata.builder().addIndexPartitionKey(
                                                    TableMetadata.primaryIndexName(), "hash-key", AttributeValueType.S).build()));

        mappedTable1.createTable(r -> r.provisionedThroughput(getDefaultProvisionedThroughput()));


        mappedTable1.putItem(JsonItem.fromJson("{\"hash-key\":\"1234\",\"updateDone\":false}"));


Testing

  • Junits for put,get,scan, query and delete items.

Todo

  • Will update the Java docs
  • Will add more test cases to check how to deal with Bytes

Screenshots (if appropriate)

Types of changes

  • [ ] Bug fix (non-breaking change which fixes an issue)
  • [x] New feature (non-breaking change which adds functionality)

Checklist

  • [ ] I have read the CONTRIBUTING document
  • [ ] Local run of mvn install succeeds
  • [ ] My code follows the code style of this project
  • [ ] My change requires a change to the Javadoc documentation
  • [ ] I have updated the Javadoc documentation accordingly
  • [ ] I have added tests to cover my changes
  • [ ] All new and existing tests passed
  • [ ] I have added a changelog entry. Adding a new entry must be accomplished by running the scripts/new-change script and following the instructions. Commit the new file created by the script in .changes/next-release with your changes.
  • [ ] My change is to implement 1.11 parity feature and I have updated LaunchChangelog

License

  • [x] I confirm that this pull request can be released under the Apache 2 license

joviegas avatar Feb 28 '22 18:02 joviegas

Will create a PR

joviegas avatar Aug 29 '22 18:08 joviegas

Closing it , as I will be raising a new one by end of Q4 2022

joviegas avatar Sep 29 '22 20:09 joviegas