没有注释的DynamoDB文档字段的DynamoDBTypeConverter [英] DynamoDBTypeConverter for DynamoDB document field without annotations
问题描述
我在DynamoDB中有一些条目,这些条目具有DynamoDB文档格式的以下字段:
I have some entries in DynamoDB, which have an field as follows, in DynamoDB document format:
{" foo":{"N":"45";},"bar":{"N":"12";}}
我有一个表示文档类型的Java类:
I've got a Java class representing the document type:
public class FooBar {
private final int foo;
private final int bar;
public FooBar(
int foo,
int bar
) {
this.foo = foo;
this.bar = bar;
}
public int getFoo() {
return foo;
}
public int getBar() {
return bar;
}
}
我想使用 DynamoDB映射器获取并将行放入表中(为简洁起见,省略了代表整个行的中间类).我不想将DynamoDB注释( @DynamoDBDocument
, @DynamoDBAttribute
等)添加到 FooBar
类,因为它们需要零参数构造方法和设置方法,但这应该是不可变的数据对象.
I'd like to use a DynamoDB mapper to get and put rows to the table (the intermediate class representing the entire row is omitted for brevity). I don't want to add the DynamoDB annotations (@DynamoDBDocument
, @DynamoDBAttribute
etc) to the FooBar
class as they require a zero-argument constructor and setter methods but this should be an immutable data object.
因此,我想使用 DynamoDBTypeConverter (如这里)来编写自己的转换,但找不到合适的源"类型(以替换 [?]
):
Therefore, I'd like to use a DynamoDBTypeConverter (as described here) to write my own conversion, but I cannot find a suitable 'source' type (to replace [?]
):
class FooBarConverter implements DynamoDBTypeConverter<[?], FooBar> {
@Override
public [?] convert(FooBar object) {
...
}
@Override
public FooBar unconvert([?] object) {
...
}
}
- 看似最自然的地图失败,并出现
DynamoDBMappingException:不支持;需要@DynamoDBTyped或@DynamoDBTypeConverted
- 似乎是合理的默认值的字符串不会出错,但是提供给
convert()
方法的字符串始终为空 Dliamo不支持 - 对象,这似乎是一个包罗万象的对象,并且由于
DynamoDBMappingException而失败:无法解析FooBarConverter类的类型
- Map, which would seem the most natural, fails with
DynamoDBMappingException: not supported; requires @DynamoDBTyped or @DynamoDBTypeConverted
- String, which would seem to be a reasonable default, doesn't error but the string supplied to the
convert()
method is always empty - Object, which would seem to be a catch-all, isn't supported by Dynamo and fails with
DynamoDBMappingException: could not resolve type of class FooBarConverter
如果我们认为Java类和存储格式都是不可修改的,那有什么解决方案?
If we regard both the Java class and the storage format as being unmodifiable, what solution is there?
推荐答案
I've found an appropriate type to use for the source - AttributeValue
. Here's the converter:
public class FooBarConverter implements DynamoDBTypeConverter<AttributeValue, FooBar> {
@Override
public AttributeValue convert(FooBar object) {
return new AttributeValue()
.addMEntry("foo", new AttributeValue().withN(Integer.toString(object.getFoo())))
.addMEntry("bar", new AttributeValue().withN(Integer.toString(object.getBar())));
}
@Override
public FooBar unconvert(AttributeValue object) {
Map<String, AttributeValue> objectM = object.getM();
int foo = Integer.parseInt(objectM.get("foo").getN());
int bar = Integer.parseInt(objectM.get("bar").getN());
return new FooBar(foo, bar);
}
}
另外,一个代表Dynamo中整个行的类:
Also, a class to represent the whole row in Dynamo:
public class FooBarRow {
private String key;
private FooBar fooBar;
public FooBarRow(
String key,
FooBar fooBar
) {
this.key = key;
this.fooBar = fooBar;
}
public FooBarRow() {
}
@DynamoDBHashKey
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
@DynamoDBTypeConverted(converter = FooBarConverter.class)
public FooBar getFooBar() {
return fooBar;
}
public void setFooBar(FooBar fooBar) {
this.fooBar = fooBar;
}
}
现在,使用 DynamoDBMapper
保存和加载 FooBarRow
实例非常正常.
Now, saving and loading FooBarRow
instances using a DynamoDBMapper
works perfectly.
public class FooBarMain {
public static void main(String[] args) {
DynamoDBMapper mapper = new DynamoDBMapper(
AmazonDynamoDBClientBuilder.standard().build(),
DynamoDBMapperConfig
.builder()
.withTableNameOverride(new DynamoDBMapperConfig.TableNameOverride("foobar"))
.build()
);
mapper.save(new FooBarRow("foobar1", new FooBar(123, 4)));
mapper.load(FooBarRow.class, "foobar1").getFooBar();
}
}
这篇关于没有注释的DynamoDB文档字段的DynamoDBTypeConverter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!