我需要在 DynamoDB 表中保留一个属性,其中属性为 Set<Some Class>
.
基本上我需要将该属性放在 Map<String,AttributeValue>
中。
对于生成情况我们通常这样做
Map<String,AttributeValue> itemsInTable = new HashMap<String, AttributeValue>();
itemsInTable.put("Id", new AttributeValue("123"));
现在假设我有一个Set<Info>
哪里Info
是一个类。
如何放置这种属性?
如果属性值是一个列表??
最佳答案
经过两三天的研究,我发现没有直接的方法来放置像set<Info>
这样的东西。在 DynamoDB 中使用这种表示法。
Map<String,AttributeValue> itemsInTable = new HashMap<String, AttributeValue>();
itemsInTable.put("Id", new AttributeValue("123"));
但是我们可以选择一些中间方式来转换 Object
至JSONObject
或JSONString
并输入 Map<String,AttributeValue>
通过使用
Set<String> set = new HashSet<String>();
Map<String,AttributeValue> itemsInTable = new HashMap<String, AttributeValue>();
itemsInTable.put("Id", new AttributeValue().withSS(set));
要将对象转换为 jsonString,我们可以借助 jackson
图书馆。
导入这个:
import zaaa.org.codehaus.jackson.map.ObbjectMapper
定义ObjectMapper
private final ObjectMapper jacksonObjectMapper = new ObjectMapper();
现在假设类(class)是 Container
其对应的Object是`container'
现在主要工作是将其转换为jsonString
这可以通过使用 writevalueAsString
轻松完成功能。
String jsonString = jacksonObjectMapper.writeValueAsString(container)
现在我们有了jsonString,我们可以轻松地将其转换为JSONObject
或JSONArray
每当需要时。
我通过这种方式解决了我的问题。
关于java - 将值放入 DynamoDB 表中,并将属性作为对象集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30367496/