Using lambda’s AWS dynamodb trigger in Java

I tried to trigger AWS lambda function written in Java on dynamodb stream event Amazon has the same guide, which uses nodejs here

The test input of nodejs (from the link above) looks like an SNS event, so I try to use the corresponding snevent class in Java as the input of my handler method

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.events.SNSEvent;
import com.amazonaws.services.lambda.runtime.events.SNSEvent.SNSRecord;
import java.util.List;

public class RecomFunction {

    public void handler(SNSEvent event,Context context) {

        LambdaLogger logger = context.getLogger();

        List<SNSRecord> records = event.getRecords();

        if (records != null) {
            for (SNSRecord record : records) {
                if (record != null) {
                    logger.log("SNS record: " + record.getSNS().getMessage());
                }
            }
        }
    }

}

Unfortunately, record Getsns() returned null, resulting in a null pointer exception

There is a related question, but no specific answer is given: setup dynamodb trigger using lambda

Solution

This code is useful to me You can use it to receive and process dynamodb events in lambda functions –

public class Handler implements RequestHandler<DynamodbEvent,Void> {

    @Override
    public Void handleRequest(DynamodbEvent dynamodbEvent,Context context) {

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

            if (record == null) {
                continue;
            }

            // Your code here
        }

        return null;
    }
}

Similarly, you can use snevent and snsrecord to handle Amazon SNS events

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>