在JAVA中使用lambda AWS DynamoDB触发 [英] AWS DynamoDB trigger using Lambda in JAVA
问题描述
我试图触发Java编写的AWS lambda表达式,在dynamodb流事件。亚马逊具有相同的指导,采用NodeJS这里 HTTP://docs.aws.amazon .COM /λ/最新/ DG / WT-DDB创建,测试function.html
I am trying to trigger an AWS lambda function written in Java, on dynamodb stream events. Amazon has a guide for the same, using NodeJS here http://docs.aws.amazon.com/lambda/latest/dg/wt-ddb-create-test-function.html
测试输入NodeJS(从上面的链接)看起来像一个SNS的事件,所以我试图用相应的SNSEvent类在Java中,作为给我的处理方法。
The testing input for NodeJS (from the above link) looks like an SNS event, so I tried to use the corresponding SNSEvent class in Java as an input to 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());
}
}
}
}
}
不幸的是,record.getSNS()产生一个空指针异常返回NULL
Unfortunately, record.getSNS() returns NULL resulting to a NullPointer exception
有一个相关的问题,但一个明确的答案没有给出: 设置DynamoDB触发使用lambda
There is a related question, however a specific answer was not given: Setup DynamoDB Trigger using Lambda
推荐答案
这工作对我来说 - 案例DynamoDB流事件:
This worked for me - case DynamoDB stream events:
import com.amazonaws.services.lambda.runtime.RequestHandler;
...
public class DynamoStreamHandler implements RequestHandler<Object, Void> {
@Override
public Void handleRequest(Object o, Context context) {
LinkedHashMap lhm = (LinkedHashMap) o;
...etc.
}
}
看来他们使用自定义的JSON映射器,它利用地图
和列表
的对象。这是非常简单的(但繁琐),以验证这一点通过测试和打印郊游日志等事件类型。 (叹气)
It seems they use a customized JSON mapper which utilizes Map
and List
objects. It's quite straightforward (but tedious) to verify this for other event types by testing and print-outing logs. (sigh)
编辑::如果〜5 MB的开销就可以了,你可以使用 DynamodbEvent.DynamodbStreamRecord
按提供的AWS-拉姆达-java-事件
库中的 AWS LAMBDA演练3:处理亚马逊DynamoDB活动(爪哇)的在AWS LAMBDA文档
If ~5 MB overhead is ok, you can use DynamodbEvent.DynamodbStreamRecord
provided by aws-lambda-java-events
library as described in AWS Lambda Walkthrough 3: Process Amazon DynamoDB Events (Java) in AWS Lambda Documentation.
这篇关于在JAVA中使用lambda AWS DynamoDB触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!