AWS DynamoDB trigger using Lambda in JAVA

前端 未结 5 1811
天命终不由人
天命终不由人 2021-01-12 13:44

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/lambd

5条回答
  •  旧时难觅i
    2021-01-12 14:16

    Create a handler that takes an InputStream, read in the contents of the InputStream (which is just JSON) and then deserialize it to get the data that you need.

    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import com.amazonaws.services.lambda.runtime.Context; 
    
    public class MyHandler {    
        public void handler(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {           
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int letter;        
            while((letter = inputStream.read()) != -1)
            {
                baos.write(letter);                     
            }        
    
            //Send the contents of baos to a JSON deserializer ...          
        }      
    }
    

    It's a bit cumbersome, but as far as I'm aware AWS doesn't currently provide a higher level Java lambda interface for consuming DynamoDB Streams. I have a full blown example here with details on how I deserialized the stream of JSON to get Java objects for the data.

提交回复
热议问题