简体   繁体   English

Amazon DynamoDB 映射枚举

[英]Amazon DynamoDB mapping enums

I need to map a User class for Amazon DynamoDB.我需要 map 用户 class 用于 Amazon DynamoDB。 One of the properties on the User object is AccountType (an enum).用户 object 的属性之一是 AccountType(枚举)。 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.任何帮助,将不胜感激。

The AWS SDK supports the special annotation DynamoDBTypeConvertedEnum to convert an enum into a string. AWS开发工具包支持特殊注释DynamoDBTypeConvertedEnum,以将枚举转换为字符串。

@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 : AWS SDK for Java提供的Amazon DynamoDB的高级API( 对象持久性模型 )尚不支持enum ,请参阅支持的数据类型

Amazon DynamoDB supports the following primitive data types and primitive wrapper classes. Amazon DynamoDB支持以下原始数据类型和原始包装类。

  • String
  • Boolean, boolean 布尔值,布尔值
  • Byte, byte 字节,字节
  • Date (as ISO8601 millisecond-precision string, shifted to UTC) 日期(作为ISO8601毫秒精度字符串,转换为UTC)
  • Calendar (as ISO8601 millisecond-precision string, shifted to UTC) 日历(作为ISO8601毫秒精度字符串,转换为UTC)
  • Long, long 很久很久
  • Integer, int 整数,整数
  • Double, double 双人,双人
  • Float, float 漂浮,漂浮
  • BigDecimal BigDecimal的
  • BigInteger 的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: 但是, Amazon DynamoDB原则上支持任意数据类型 ,因此您可以解决该限制,请参阅使用AWS SDK for Java对象持久性模型将任意数据映射到Amazon DynamoDB以获取详细信息:

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. 除了支持的Java类型[...]之外,您还可以在应用程序中使用没有直接映射到Amazon DynamoDB类型的类型。 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. 要映射这些类型,必须提供将复杂类型转换为String实例的实现,反之亦然,并使用@DynamoDBMarshalling批注类型注释复杂类型访问器方法。 [...] [...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM