| « PreviousNext » | |
![]() ![]() ![]() | Did this page help you? Yes | No | Tell us about it... |
In addition to the supported Java types (see Supported Data Types), you can use types in your application
for which there is no direct mapping to the Amazon DynamoDB types. To map these types, you must
provide an implementation that converts your complex type to an instance of String and
vice-versa and annotate the complex type accessor method using the
@DynamoDBMarshalling annotation type. The converter code transforms
data when objects are saved or loaded. It is also used for all operations that consume
complex types. Note that when comparing data during query and scan operations, the
comparisons are made against the data stored in Amazon DynamoDB.
For example, consider the following CatalogItem class that defines a
property, Dimension, that is of DimensionType. This property
stores the item dimensions, as height, width, and thickness. Assume that you decide to
store these item dimensions as a string (such as 8.5x11x.05) in Amazon DynamoDB. The following
example provides converter code that converts the DimensionType object to a
string and a string to the DimensionType.
Note
This code example assumes that you have already loaded data into Amazon DynamoDB for your account by following the instructions in the Getting Started section. Alternatively, you can load the data programmatically using the instructions in the Creating Example Tables and Uploading Data Using the AWS SDK for Java Low-Level API topic.
For step-by-step instructions to run the following example, see Running Java Examples for Amazon DynamoDB.
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMarshaller;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMarshalling;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
public class ObjectPersistenceMappingExample {
static AmazonDynamoDBClient client;
public static void main(String[] args) throws IOException {
AWSCredentials credentials = new PropertiesCredentials(
ObjectPersistenceMappingExample.class.getResourceAsStream("AwsCredentials.properties"));
client = new AmazonDynamoDBClient(credentials);
DimensionType dimType = new DimensionType();
dimType.setHeight("8.00");
dimType.setLength("11.0");
dimType.setThickness("1.0");
Book book = new Book();
book.setId(502);
book.setTitle("Book 502");
book.setISBN("555-5555555555");
book.setBookAuthors(new HashSet<String>(Arrays.asList("Author1", "Author2")));
book.setDimensions(dimType);
System.out.println(book);
DynamoDBMapper mapper = new DynamoDBMapper(client);
mapper.save(book);
Book bookRetrieved = mapper.load(Book.class, 502);
System.out.println(bookRetrieved);
bookRetrieved.getDimensions().setHeight("9.0");
bookRetrieved.getDimensions().setLength("12.0");
bookRetrieved.getDimensions().setThickness("2.0");
mapper.save(bookRetrieved);
bookRetrieved = mapper.load(Book.class, 502);
System.out.println(bookRetrieved);
}
@DynamoDBTable(tableName="ProductCatalog")
public static class Book {
private int id;
private String title;
private String ISBN;
private Set<String> bookAuthors;
private DimensionType dimensionType;
@DynamoDBHashKey(attributeName = "Id")
public int getId() { return id; }
public void setId(int id) { this.id = id; }
@DynamoDBAttribute(attributeName = "Title")
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
@DynamoDBAttribute(attributeName="ISBN")
public String getISBN() { return ISBN; }
public void setISBN(String ISBN) { this.ISBN = ISBN;}
@DynamoDBAttribute(attributeName = "Authors")
public Set<String> getBookAuthors() { return bookAuthors; }
public void setBookAuthors(Set<String> bookAuthors) { this.bookAuthors = bookAuthors; }
@DynamoDBMarshalling(marshallerClass = DimensionTypeConverter.class)
public DimensionType getDimensions() { return dimensionType; }
public void setDimensions(DimensionType dimensionType) { this.dimensionType = dimensionType; }
@Override
public String toString() {
return "Book [ISBN=" + ISBN + ", bookAuthors=" + bookAuthors
+ ", dimensionType=" + dimensionType + ", Id=" + id
+ ", Title=" + title + "]";
}
}
static public class DimensionType {
private String length;
private String height;
private String thickness;
public String getLength() { return length; }
public void setLength(String length) { this.length = length; }
public String getHeight() { return height; }
public void setHeight(String height) { this.height = height; }
public String getThickness() { return thickness; }
public void setThickness(String thickness) { this.thickness = thickness; }
}
// Converts the complex type DimensionType to a string and vice-versa.
static public class DimensionTypeConverter implements DynamoDBMarshaller<DimensionType> {
@Override
public String marshall(DimensionType value) {
DimensionType itemDimensions = (DimensionType)value;
String dimension = null;
try {
if (itemDimensions != null) {
dimension = String.format("%s x %s x %s",
itemDimensions.getLength(),
itemDimensions.getHeight(),
itemDimensions.getThickness());
}
} catch (Exception e) {
e.printStackTrace();
}
return dimension;
}
@Override
public DimensionType unmarshall(Class<DimensionType> dimensionType, String value) {
DimensionType itemDimension = new DimensionType();
try {
if (value != null && value.length() !=0 ) {
String[] data = value.split("x");
itemDimension.setLength(data[0].trim());
itemDimension.setHeight(data[1].trim());
itemDimension.setThickness(data[2].trim());
}
} catch (Exception e) {
e.printStackTrace();
}
return itemDimension;
}
}
}