Java DynamoDB-对象到属性值
我知道DynamoDBMapper,但就我而言,我不能使用它,因为我事先不知道所有属性 我有一个JSON,使用Java DynamoDB-对象到属性值,java,amazon-dynamodb,Java,Amazon Dynamodb,我知道DynamoDBMapper,但就我而言,我不能使用它,因为我事先不知道所有属性 我有一个JSON,使用Jacksonparser将其解析为对象映射: Map<String, Object> userData = mapper.readValue(new File("user.json"), Map.class); Map userData=mapper.readValue(新文件(“user.json”)、Map.class); 循环遍历每个属性,如何将值转换为Attri
Jackson
parser将其解析为对象映射:
Map<String, Object> userData = mapper.readValue(new File("user.json"), Map.class);
Map userData=mapper.readValue(新文件(“user.json”)、Map.class);
循环遍历每个属性,如何将值转换为AttributeValue
,因为DynamoDBAttributeValue
支持布尔值、字符串、数字、字节、列表等
有没有一种有效的方法可以做到这一点?这已经有图书馆了吗?我最天真的方法是检查每个值是否为Boolean/String/Number/etc类型,然后调用相应的
AttributeValue
方法,例如:newattributeValue().withN(value.toString())
——这让我看到了一长行if,else if
,最后通过查看如何
基本上,这是代码:
Item item = new Item().withJSON("document", jsonStr);
Map<String,AttributeValue> attributes = InternalUtils.toAttributeValues(item);
return attributes.get("document").getM();
Item Item=newitem().withJSON(“document”,jsonStr);
映射属性=InternalUtils.ToAttributeValue(项);
返回attributes.get(“文档”).getM();
非常简洁。下面是一个简单的解决方案,可用于将任何DynamoDB Json转换为简单Json
//passing the reponse.getItems()
public static Object getJson(List<Map<String,AttributeValue>> mapList) {
List<Object> finalJson= new ArrayList();
for(Map<String,AttributeValue> eachEntry : mapList) {
finalJson.add(mapToJson(eachEntry));
}
return finalJson;
}
//if the map is null then it add the key and value(string) in the finalKeyValueMap
public static Map<String,Object> mapToJson(Map<String,AttributeValue> keyValueMap){
Map<String,Object> finalKeyValueMap = new HashMap();
for(Map.Entry<String, AttributeValue> entry : keyValueMap.entrySet())
{
if(entry.getValue().getM() == null) {
finalKeyValueMap.put(entry.getKey(),entry.getValue().getS());
}
else {
finalKeyValueMap.put(entry.getKey(),mapToJson(entry.getValue().getM()));
}
}
return finalKeyValueMap;
}
//传递reponse.getItems()
公共静态对象getJson(列表映射列表){
List finalJson=new ArrayList();
对于(映射每个尝试:映射列表){
add(mapToJson(eachEntry));
}
返回finalJson;
}
//如果映射为null,则在finalKeyValueMap中添加键和值(字符串)
公共静态映射mapToJson(MapKeyValueMap){
Map finalKeyValueMap=新HashMap();
对于(Map.Entry:keyValueMap.entrySet())
{
if(entry.getValue().getM()==null){
finalKeyValueMap.put(entry.getKey(),entry.getValue().getS());
}
否则{
finalKeyValueMap.put(entry.getKey(),mapToJson(entry.getValue().getM());
}
}
返回finalKeyValueMap;
}
这将以列表
的形式生成所需的Json,它是对象
的子集 我曾经将JsonNode
转换为Map
ObjectMapper ObjectMapper=new ObjectMapper();
JsonNode=objectMapper.readValue(jsonString,JsonNode.class);
最终JacksonConverter转换器=新JacksonConverterImpl();
Map Map=converter.jsonObjectToMap(jsonNode);
希望这有帮助
谢谢,
Jay我有一个类似的用例。如何将映射转换为项目?我启用了streams,但是DynamodbKinesStreamsAdapter返回一个地图。。。看见我想将AttributeValue转换为Object(Item在.asMap上返回一个Map)@shrewquest你找到解决方案了吗?@shrewquest Item.fromMap(InternalUtils.toSimpleMapValue(mapOfAttributes))我如何从
Map
转换为Object?这是一个奇妙的发现。谢谢。@bohdankorinyi@BPm采用的方法还有一种方法,可以将映射
转换为列表
,这样您就可以访问实际的基元类型(例如item.getBoolean(attrName)
)。如果这符合您的用例,请参阅:InternalUtils类已弃用(请改用)。如果源对象具有ByteBuffer字段,则此方法不起作用。Json将其转换为struct,AttributeValue将其视为映射。
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readValue(jsonString, JsonNode.class);
final JacksonConverter converter = new JacksonConverterImpl();
Map<String, AttributeValue> map = converter.jsonObjectToMap(jsonNode);