如何从DynamoDB流新的图像中获取纯Json字符串?

时间:2023-01-24 14:43:35

I've a Dynamodb table with streaming enabled. Also I've created a trigger for this table which calls an AWS Lambda function. Within this lambda function, I'm trying read the new image (Dynamodb item after the modification) from the Dynamodb stream and trying to get the pure json string out of it. My Question is how can i get the pure json string of the DynamoDB item that's been sent over the stream? I'm using the code snippet given below to get the new Image, but I've no clue how to get the json string out of it. Appreciate your help.

我有一个启用了流媒体的Dynamodb表。我还为此表创建了一个触发AWS Lambda函数的触发器。在这个lambda函数中,我正在尝试从Dynamodb流中读取新图像(修改后的Dynamodb项)并尝试从中获取纯json字符串。我的问题是如何获得通过流发送的DynamoDB项的纯json字符串?我正在使用下面给出的代码片段来获取新的Image,但我不知道如何从中获取json字符串。感谢你的帮助。

public class LambdaFunctionHandler implements RequestHandler<DynamodbEvent, Object> {

@Override
public Object handleRequest(DynamodbEvent input, Context context) {
    context.getLogger().log("Input: " + input);

    for (DynamodbStreamRecord record : input.getRecords()){

        context.getLogger().log(record.getEventID());
        context.getLogger().log(record.getEventName());
        context.getLogger().log(record.getDynamodb().toString());
        Map<String,AttributeValue> currentRecord = record.getDynamodb().getNewImage();

        //how to get the pure json string of the new image
        //..............................................
     }
     return "Successfully processed " + input.getRecords().size() + " records.";
}

}

}

5 个解决方案

#1


4  

Below is the complete code for converting from Dynamo JSON to Standard JSON:

以下是从Dynamo JSON转换为Standard 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;

/**
 * Main Lambda class to receive event stream, parse it to Survey
 * and process them.
 */
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.";
    }
} 

#2


2  

Found a way of doing it cleanly. Using InternalUtils from aws-java-sdk-dynamodb-1.11.15.jar

找到一种干净利落的方式。使用aws-java-sdk-dynamodb-1.11.15.jar中的InternalUtils

com.amazonaws.services.dynamodbv2.model.Record streamRecord = ((RecordAdapter) record).getInternalObject();
            // get order ready //
            OrderFinal order = Utils.mapO2Object(
                    InternalUtils.toSimpleMapValue(streamRecord.getDynamodb().getNewImage().get("document").getM()), 
                    OrderFinal.class );

#3


0  

Did you figure out a way to do this. One crude way is to create your own parser but even we don't want to go with that approach

你有没有办法做到这一点。一种粗略的方法是创建自己的解析器,但即使我们不想采用这种方法

#4


0  

Just summarizing the answer of Himanshu Parmar:

只是总结一下Himanshu Parmar的答案:

Map<String, AttributeValue> newImage = record.getDynamodb().getNewImage();
List<Map<String, AttributeValue>> listOfMaps = new ArrayList<Map<String, AttributeValue>>();
listOfMaps.add(newImage);
List<Item> itemList = InternalUtils.toItemList(listOfMaps);
for (Item item : itemList) {
    String json = item.toJSON();
}

#5


-1  

This library do the job: dynamoDb-marshaler

这个库完成这项工作:dynamoDb-marshaler

var unmarshalJson = require('dynamodb-marshaler').unmarshalJson;

console.log('jsonItem Record: %j', unmarshalJson(record.dynamodb.NewImage));

#1


4  

Below is the complete code for converting from Dynamo JSON to Standard JSON:

以下是从Dynamo JSON转换为Standard 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;

/**
 * Main Lambda class to receive event stream, parse it to Survey
 * and process them.
 */
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.";
    }
} 

#2


2  

Found a way of doing it cleanly. Using InternalUtils from aws-java-sdk-dynamodb-1.11.15.jar

找到一种干净利落的方式。使用aws-java-sdk-dynamodb-1.11.15.jar中的InternalUtils

com.amazonaws.services.dynamodbv2.model.Record streamRecord = ((RecordAdapter) record).getInternalObject();
            // get order ready //
            OrderFinal order = Utils.mapO2Object(
                    InternalUtils.toSimpleMapValue(streamRecord.getDynamodb().getNewImage().get("document").getM()), 
                    OrderFinal.class );

#3


0  

Did you figure out a way to do this. One crude way is to create your own parser but even we don't want to go with that approach

你有没有办法做到这一点。一种粗略的方法是创建自己的解析器,但即使我们不想采用这种方法

#4


0  

Just summarizing the answer of Himanshu Parmar:

只是总结一下Himanshu Parmar的答案:

Map<String, AttributeValue> newImage = record.getDynamodb().getNewImage();
List<Map<String, AttributeValue>> listOfMaps = new ArrayList<Map<String, AttributeValue>>();
listOfMaps.add(newImage);
List<Item> itemList = InternalUtils.toItemList(listOfMaps);
for (Item item : itemList) {
    String json = item.toJSON();
}

#5


-1  

This library do the job: dynamoDb-marshaler

这个库完成这项工作:dynamoDb-marshaler

var unmarshalJson = require('dynamodb-marshaler').unmarshalJson;

console.log('jsonItem Record: %j', unmarshalJson(record.dynamodb.NewImage));