如何在 Lambda 处理程序中反序列化 DynamoDB 映射类型
How to deserialize DynamoDB Map type in Lambda handler
我有一个简单的 POJO:
public class MyClass {
public String id;
public String name;
}
我想将其序列化为 JSON 相当于 DynamoDB 事件的 AWS Lambda 处理程序为 DynamoDB 类型的嵌入式文档获取的格式 Map
:
M: {
id= { S: "abc123", },
name= { S: "whatever", },
}
更重要的是,我想从 JSON 我的 Lambda 函数接收到的每个插入反序列化它。我能找到的唯一例子是非常简单的例子。 None 其中包括在 Lambda 处理程序内部的事件中处理 DynamoDB 映射。
我想知道我是否必须手动编写一个序列化程序,或者 SDK 中是否有一些我还没有找到的东西。
您可以将 POJO 映射到 com.amazonaws.services.dynamodbv2.model.AttributeValue
的实例,然后 serialize/deserialize 使用 GSON
。
的方法
下面是如何使用 Gson 操作 AttributeValue 的示例。
AttributeValue av1 = new AttributeValue("abc123");
AttributeValue av2 = new AttributeValue("whatever");
AttributeValue avM = new AttributeValue();
Map<String, AttributeValue> m = new HashMap<String, AttributeValue>();
m.put("id", av1);
m.put("name", av2);
avM.setM(m);
System.out.println(new Gson().toJson(avM));
// {"m":{"name":{"s":"whatever"},"id":{"s":"abc123"}}}
Map<String, Object> simpleMap = new HashMap<String, Object>();
simpleMap.put("id", "abc123");
simpleMap.put("name", "whatever");
AttributeValue avM2 = new AttributeValue().withM(InternalUtils.fromSimpleMap(simpleMap));
System.out.println(new Gson().toJson(avM2));
// {"m":{"name":{"s":"whatever"},"id":{"s":"abc123"}}}
String jsonStr = new Gson().toJson(avM2);
AttributeValue avM3 = new Gson().fromJson(jsonStr, AttributeValue.class);
System.out.println(avM3.getM().get("id").getS());
// abc123
System.out.println(avM3.getM().get("name").getS());
// whatever
如果您开始使用 Lambda 和 DynamoDB Java,我建议您看一下 Secure Pet Store。
我有一个简单的 POJO:
public class MyClass {
public String id;
public String name;
}
我想将其序列化为 JSON 相当于 DynamoDB 事件的 AWS Lambda 处理程序为 DynamoDB 类型的嵌入式文档获取的格式 Map
:
M: {
id= { S: "abc123", },
name= { S: "whatever", },
}
更重要的是,我想从 JSON 我的 Lambda 函数接收到的每个插入反序列化它。我能找到的唯一例子是非常简单的例子。 None 其中包括在 Lambda 处理程序内部的事件中处理 DynamoDB 映射。
我想知道我是否必须手动编写一个序列化程序,或者 SDK 中是否有一些我还没有找到的东西。
您可以将 POJO 映射到 com.amazonaws.services.dynamodbv2.model.AttributeValue
的实例,然后 serialize/deserialize 使用 GSON
。
下面是如何使用 Gson 操作 AttributeValue 的示例。
AttributeValue av1 = new AttributeValue("abc123");
AttributeValue av2 = new AttributeValue("whatever");
AttributeValue avM = new AttributeValue();
Map<String, AttributeValue> m = new HashMap<String, AttributeValue>();
m.put("id", av1);
m.put("name", av2);
avM.setM(m);
System.out.println(new Gson().toJson(avM));
// {"m":{"name":{"s":"whatever"},"id":{"s":"abc123"}}}
Map<String, Object> simpleMap = new HashMap<String, Object>();
simpleMap.put("id", "abc123");
simpleMap.put("name", "whatever");
AttributeValue avM2 = new AttributeValue().withM(InternalUtils.fromSimpleMap(simpleMap));
System.out.println(new Gson().toJson(avM2));
// {"m":{"name":{"s":"whatever"},"id":{"s":"abc123"}}}
String jsonStr = new Gson().toJson(avM2);
AttributeValue avM3 = new Gson().fromJson(jsonStr, AttributeValue.class);
System.out.println(avM3.getM().get("id").getS());
// abc123
System.out.println(avM3.getM().get("name").getS());
// whatever
如果您开始使用 Lambda 和 DynamoDB Java,我建议您看一下 Secure Pet Store。