I need to map a User class for Amazon DynamoDB. One of the properties on the User object is AccountType (an enum). How do I handle this? Below is the enum and the code I have tried.
public enum AccountType { TYPE_A, TYPE_B }
-
@DynamoDBAttribute(attributeName="AccountType") *<< THIS FAILS* public AccountType getAccountType() { return accountType; }
Any help would be appreciated.
With OPM enum support, enums are stored as their numeric representations in DynamoDB. (The default underlying type is int , but you can change it, as described in this MSDN article.)
The following example shows a map that contains a string, a number, and a nested list that contains another map. DynamoDB lets you work with individual elements within maps, even if those elements are deeply nested.
DynamoDB supports binary attributes. However, JSON does not natively support encoding binary data. To send binary data in a request, you will need to encode it in base64 format. Upon receiving the request, DynamoDB decodes the base64 data back to binary.
When it comes to time-series data though, break from these design principles and create multiple tables for each period. In this post, I show you how to use such an anti-pattern for DynamoDB, but it is a great fit for time-series data.
The AWS SDK supports the special annotation DynamoDBTypeConvertedEnum to convert an enum into a string.
@DynamoDBTypeConvertedEnum @DynamoDBAttribute(attributeName="AccountType") public AccountType getAccountType() { return accountType; }
The high-level API (the Object Persistence model) for Amazon DynamoDB provided by the AWS SDK for Java doesn't support enum
yet, see Supported Data Types:
Amazon DynamoDB supports the following primitive data types and primitive wrapper classes.
- String
- Boolean, boolean
- Byte, byte
- Date (as ISO8601 millisecond-precision string, shifted to UTC)
- Calendar (as ISO8601 millisecond-precision string, shifted to UTC)
- Long, long
- Integer, int
- Double, double
- Float, float
- BigDecimal
- BigInteger
However, Amazon DynamoDB supports arbitrary data types in principle, so you might be able to work around that limitation, see Mapping Arbitrary Data with Amazon DynamoDB Using the AWS SDK for Java Object Persistence Model for details:
In addition to the supported Java 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. [...]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With