使用Java将DynamoDB JSON转换为标准JSON

使用Java将DynamoDB JSON转换为标准JSON,第1张

使用Java将DynamoDB JSON转换为标准JSON

以下是将Dynamo JSON转换为标准JSON的完整代码

import com.amazonaws.services.dynamodbv2.document.Item;import com.amazonaws.services.dynamodbv2.document.internal.InternalUtils;import com.amazonaws.services.dynamodbv2.model.Attributevalue;import com.amazonaws.services.lambda.runtime.Context;import com.amazonaws.services.lambda.runtime.RequestHandler;import com.amazonaws.services.lambda.runtime.events.DynamodbEvent;import com.amazonaws.services.lambda.runtime.events.DynamodbEvent.DynamodbStreamRecord;import com.google.gson.Gson;import java.util.ArrayList;import java.util.List;import java.util.Map;public class SurveyEventProcessor implements        RequestHandler<DynamodbEvent, String> {    private static final String INSERT = "INSERT";    private static final String MODIFY = "MODIFY";    public String handleRequest(DynamodbEvent ddbEvent, Context context) {        List<Item> listOfItem = new ArrayList<>();        List<Map<String, Attributevalue>> listOfMaps = null;        for (DynamodbStreamRecord record : ddbEvent.getRecords()) { if (INSERT.equals(record.getEventName()) || MODIFY.equals(record.getEventName())) {     listOfMaps = new ArrayList<Map<String, Attributevalue>>();     listOfMaps.add(record.getDynamodb().getNewImage());     listOfItem = InternalUtils.toItemList(listOfMaps); } System.out.println(listOfItem); try {    // String json = new ObjectMapper().writevalueAsString(listOfItem.get(0));     Gson gson = new Gson();     Item item = listOfItem.get(0);     String json = gson.toJson(item.asMap());     System.out.println("JSON is ");     System.out.println(json); }catch (Exception e){     e.printStackTrace(); }        }        return "Successfully processed " + ddbEvent.getRecords().size() + " records.";    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5051527.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存