We’re happy to announce that the enhanced DynamoDB consumer within the AWS SDK for Java 2.x now helps the mapping of immutable Java objects immediately with data in your DynamoDB tables. Beforehand, solely mutable ‘Java bean’ fashion objects have been supported.
Immutability in Java is a generally used fashion that permits builders to create courses which can be side-effect free and subsequently predictable in complicated and multi-threaded functions. Immutable objects in Java characteristically solely have getters (and no setters) and are constructed by passing all their properties directly to the constructor. It is usually typical to have a separate mutable ‘builder’ class to help with accumulating the initialization values for these properties.
Earlier than this launch, prospects utilizing immutable fashion objects had no method to immediately map data they have been studying and writing from their DynamoDB tables to those objects. As a substitute, they have been compelled to work with bean objects and use elaborate workarounds, similar to copying and reworking the objects learn by the improved consumer or wrapping them in an opaque container. Now the improved consumer can work immediately with the immutable courses with the addition of a only a few easy annotations.
Fast walk-through for mapping an immutable class
Conditions:
Earlier than getting began with mapping immutable courses, guarantee your SDK dependency is updated with all the newest launched bug-fixes and options. For immutable object mapping assist you should be utilizing model 2.14.10 or later. See our Java SDK Developer Information for the small print on handle the AWS Java SDK dependency in your challenge.
Step 1 : Annotate your current immutable class
First, create and annotate an immutable class that may map to data in your DynamoDB desk. You might both comply with the instance beneath or create your personal class. The immutable class you create ought to solely have getters, and there should be a static builder class that can be utilized to assemble situations of it.
Add the @DynamoDbImmutable annotation to the immutable class. You will need to cross a parameter of ‘builder’ to the annotation with the worth set to the builder class you employ to construct objects of your immutable class. An instance utilizing an inner-class of our immutable class ‘Document’ known as ‘Builder’ could be @DynamoDbImmutable(builder = Document.Builder.class)
.
Add different commonplace DynamoDB enhanced consumer annotations as required. These annotations are the identical and work in the identical means as the present bean class annotations. At a minimal it’s essential to specify a @DynamoDbPrimaryKey. Different annotations are optionally available.
This instance reveals a whole however minimally annotated immutable class that’s prepared to be used with the improved consumer:
@DynamoDbImmutable(builder = Document.Builder.class)
public class Document {
non-public closing String id;
non-public closing String attribute;
non-public Document(Builder b) {
this.id = b.id;
this.attribute = b.attribute;
}
public static Builder builder() {
return new Builder();
}
@DynamoDbPrimaryKey
public String id() { return this.id; }
public String attribute() { return this.attribute; }
public static closing class Builder {
non-public String id;
non-public String attribute;
public Builder id(String id) {
this.id = id;
return this;
}
public Builder attribute(String attribute) {
this.attribute = attribute;
return this;
}
public Document construct() {
return new Document(this);
}
}
}
For extra details about what structurally qualifies a category for use as a DynamoDB annotated immutable class, please discuss with “Working with immutable information courses” part within the DynamoDB enhanced consumer information.
Step 2 : Assemble a TableSchema to your immutable class
Subsequent, create a TableSchema object for the immutable class you created within the earlier step. A TableSchema is an object that defines the construction and attributes of DynamoDB data and is aware of map them to a Java class.
To maintain issues easy, this instance makes use of a brand new static constructor technique in TableSchema that auto-detects and works with each annotated immutable courses and annotated bean courses:
TableSchema Document recordTableSchema = tableSchema.forClass(Document.class);
Step 3 : Create a DynamoDbTable useful resource object
Subsequent, affiliate an actual DynamoDB desk along with your TableSchema to create a DynamoDBTable useful resource object to execute instructions in opposition to. This instance makes use of an actual DynamoDB desk named ‘my_table’:
DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.create();
DynamoDbTable<Document> recordTable = enhancedClient.desk("my_table", recordTableSchema);
Step 4 : Execute instructions in opposition to the DynamoDbTable useful resource object
Any objects learn from the database will now be created as immutable objects utilizing the category you offered. You may additionally cross immutable situations of this class to operations that write or replace data within the desk. For instance:
Document newRecord = Document.builder().id("123").attribute("foo").construct();
recordTable.putItem(newRecord);
Document key = Document.builder().id("123").construct();
Document persistedRecord = recordTable.getItem(key);
Conclusion
On this weblog publish we confirmed you set-up and start mapping immutable courses with the DynamoDB enhanced consumer. The improved consumer is open-source and resides in the identical repository because the AWS SDK for Java 2.0.
We hope you’ll discover this new function helpful. You’ll be able to at all times share your suggestions on our GitHub points web page or up-vote different concepts for options you’d additionally prefer to see within the DynamoDB enhanced consumer or the Java SDK on the whole.