Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon DynamoDB mapping enums

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.

like image 494
user1007895 Avatar asked Sep 25 '12 19:09

user1007895


People also ask

Does DynamoDB support enum?

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.)

Does DynamoDB support map?

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.

Which one of the following data types does Amazon DynamoDB not support?

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.

Is DynamoDB good for time series data?

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.


2 Answers

The AWS SDK supports the special annotation DynamoDBTypeConvertedEnum to convert an enum into a string.

@DynamoDBTypeConvertedEnum @DynamoDBAttribute(attributeName="AccountType") public AccountType getAccountType() {     return accountType; } 
like image 54
H6. Avatar answered Oct 03 '22 05:10

H6.


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. [...]

like image 34
Steffen Opel Avatar answered Oct 03 '22 05:10

Steffen Opel