Reputation: 2306
I'm trying to create a DynamoDB trigger using DynamoDB Streams and AWS Lambda. I researched a lot but I couldn't find any way to read and process a DynamoDB Stream event in Java 8. I'm completely new to both these technologies so don't know how to work with this.
Essentially, what I want to do is create a record in table B whenever a record is created in table A.
Could any of you please point me to a code or post that handles this use case in Java?
Thanks :)
Upvotes: 3
Views: 4951
Reputation: 1190
This code worked for me. You can use it to receive and process DynamoDB events in a Lambda function -
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
// Write to Table B using DynamoDB Java API
}
return null;
}
}
When you create your Lambda, add the stream from table A as your event source, and you're good to go
Upvotes: 6
Reputation: 21
DynamoDB Streams will send JSON to the handler. Just create a handler that takes a Java InputStream and deserialize the JSON from the inputstream. I posted an example to a similar question here.
Upvotes: 0
Reputation: 346
Hmm I can't seem to find the documentation integrating a Java Lambda function with DynamoDB streams, but the concept is the same as writing a NodeJS Lambda function with DDB streams, which is documented here: http://docs.aws.amazon.com/lambda/latest/dg/wt-ddb.html. Just replace the NodeJS function with a Java function (see here for the docs for creating a Java Lambda function: http://docs.aws.amazon.com/lambda/latest/dg/java-lambda.html). To replicate your data from table A to B, you can use the AWS Java SDK DynamoDB client to write the stream record from A to B in your Lambda function.
Upvotes: 0