Skip to content

Example KinesisFirehose Lambda function #271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/samples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This workflow will be triggered if there will be changes to aws-lambda-java-core
# package and it builds the package and the packages that depend on it.

name: Java CI samples

on:
push:
branches: [ master ]
paths:
- 'samples/kinesis-firehose-event-handler/**'
pull_request:
branches: [ '*' ]
paths:
- 'samples/kinesis-firehose-event-handler/**'

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8

# Install base module
- name: Install Kinesis Firehose Sample with Maven
run: mvn -B install --file samples/kinesis-firehose-event-handler/pom.xml
75 changes: 75 additions & 0 deletions samples/kinesis-firehose-event-handler/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events-examples</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<name>AWS Lambda Java Events Samples - KinesisFirehose</name>
<description>
AWS Lambda Java Function Examples
</description>
<url>https://aws.amazon.com/lambda/</url>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>https://aws.amazon.com/apache2.0</url>
<distribution>repo</distribution>
</license>
</licenses>
<scm>
<url>https://github.com/aws/aws-lambda-java-libs.git</url>
</scm>
<developers>
<developer>
<name>AWS Lambda team</name>
<organization>Amazon Web Services</organization>
<organizationUrl>https://aws.amazon.com/</organizationUrl>
</developer>
</developers>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>3.10.0</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-tests</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package example;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.KinesisAnalyticsInputPreprocessingResponse;
import com.amazonaws.services.lambda.runtime.events.KinesisFirehoseEvent;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

import static com.amazonaws.services.lambda.runtime.events.KinesisAnalyticsInputPreprocessingResponse.Result.Ok;
import static java.nio.charset.StandardCharsets.UTF_8;

/**
* A sample KinesisFirehoseEvent handler
*
* For more information see the developer guide - https://docs.aws.amazon.com/firehose/latest/dev/data-transformation.html
*/
public class KinesisFirehoseEventHandler implements RequestHandler<KinesisFirehoseEvent, KinesisAnalyticsInputPreprocessingResponse> {

@Override
public KinesisAnalyticsInputPreprocessingResponse handleRequest(KinesisFirehoseEvent kinesisFirehoseEvent, Context context) {
List<KinesisAnalyticsInputPreprocessingResponse.Record> records = new ArrayList<>();

for (KinesisFirehoseEvent.Record record : kinesisFirehoseEvent.getRecords()) {
String recordData = new String(record.getData().array());
// Your business logic
String reversedString = new StringBuilder(recordData).reverse().toString();

records.add(new KinesisAnalyticsInputPreprocessingResponse.Record(record.getRecordId(), Ok, ByteBuffer.wrap(reversedString.getBytes(UTF_8))));
}

return new KinesisAnalyticsInputPreprocessingResponse(records);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package example;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.events.KinesisAnalyticsInputPreprocessingResponse;
import com.amazonaws.services.lambda.runtime.events.KinesisFirehoseEvent;
import com.amazonaws.services.lambda.runtime.tests.annotations.Event;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;

import static java.nio.charset.StandardCharsets.UTF_8;

public class KinesisFirehoseEventHandlerTest {

private Context context; // intentionally null as it's not used in the test

@ParameterizedTest
@Event(value = "event.json", type = KinesisFirehoseEvent.class)
public void testEventHandler(KinesisFirehoseEvent event) {
KinesisFirehoseEventHandler kinesisFirehoseEventHandler = new KinesisFirehoseEventHandler();
KinesisAnalyticsInputPreprocessingResponse response = kinesisFirehoseEventHandler.handleRequest(event, context);

String expectedString = "\n!dlroW olleH";
KinesisAnalyticsInputPreprocessingResponse.Record firstRecord = response.getRecords().get(0);
Assertions.assertEquals(expectedString, UTF_8.decode(firstRecord.getData()).toString());
Assertions.assertEquals(KinesisAnalyticsInputPreprocessingResponse.Result.Ok, firstRecord.getResult());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"invocationId": "invoked123",
"deliveryStreamArn": "aws:lambda:events",
"region": "us-west-2",
"records": [
{
"data": "SGVsbG8gV29ybGQhCg==",
"recordId": "record2",
"approximateArrivalTimestamp": 1510772160000,
"kinesisRecordMetadata": {
"shardId": "shardId-000000000000",
"partitionKey": "4d1ad2b9-24f8-4b9d-a088-76e9947c317a",
"approximateArrivalTimestamp": "2012-04-23T18:25:43.511Z",
"sequenceNumber": "49546986683135544286507457936321625675700192471156785154",
"subsequenceNumber": ""
}
}
]
}