AWS: Java Kinesis Lambda Handler

This entry is part 2 of 5 in the series AWS & Java
(Last Updated On: )

If you want to write a Lambda for AWS in Java that connects to a Kinesis Stream. You need to have the handler.

Maven:

  1. <dependency>
  2. <groupId>com.amazonaws</groupId>
  3. <artifactId>aws-java-sdk</artifactId>
  4. <version>1.11.109</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.fasterxml.jackson.core</groupId>
  8. <artifactId>jackson-databind</artifactId>
  9. <version>2.7.1</version>
  10. </dependency>

This is the method that AWS Lambda will call. It will look similar to the one below.

  1. import com.amazonaws.services.lambda.runtime.Context;
  2. import com.amazonaws.services.lambda.runtime.events.KinesisEvent;
  3. import com.amazonaws.services.lambda.runtime.events.KinesisEvent.KinesisEventRecord;
  4. import com.amazonaws.services.kinesis.model.Record;
  5. import com.fasterxml.jackson.databind.ObjectMapper;
  6. import com.fasterxml.jackson.databind.JsonNode;
  7.  
  8. public void kinesisRecordHandler(KinesisEvent kinesisEvent, Context context) {
  9. final String awsRequestId = context.getAwsRequestId();
  10. final int memoryLimitMb = context.getMemoryLimitInMB();
  11. final int remainingTimeInMillis = context.getRemainingTimeInMillis();
  12.  
  13. for (final KinesisEventRecord kinesisRec : kinesisEvent.getRecords()) {
  14. final Record record = kinesisRec.getKinesis();
  15.  
  16. //We get the kinesis data information
  17. final JsonNode recData = new ObjectMapper().readValue(record.getData().array(), JsonNode.class);
  18.  
  19. final String bucketName = recData.get("bucket").asText();
  20. final String key = recData.get("key").asText();
  21. }
  22. }

The thing to note when you setup you Lambda is how to setup the “Handler” field in the “Configuration” section on AWS. It is in the format “##PACKAGE##.##CLASS##::##METHOD##”.

Series Navigation<< AWS: Java S3 Lambda HandlerAWS: Java S3 Upload >>