Skip to content

Commit cb0b363

Browse files
msailescarlzogh
andauthored
Add sample KinesisFirehose Lambda function (#271)
Co-authored-by: Carl Zogheib <[email protected]>
1 parent d1b016a commit cb0b363

File tree

5 files changed

+187
-0
lines changed

5 files changed

+187
-0
lines changed

.github/workflows/samples.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This workflow will be triggered if there will be changes to aws-lambda-java-core
2+
# package and it builds the package and the packages that depend on it.
3+
4+
name: Java CI samples
5+
6+
on:
7+
push:
8+
branches: [ master ]
9+
paths:
10+
- 'samples/kinesis-firehose-event-handler/**'
11+
pull_request:
12+
branches: [ '*' ]
13+
paths:
14+
- 'samples/kinesis-firehose-event-handler/**'
15+
16+
jobs:
17+
build:
18+
19+
runs-on: ubuntu-latest
20+
21+
steps:
22+
- uses: actions/checkout@v2
23+
- name: Set up JDK 1.8
24+
uses: actions/setup-java@v1
25+
with:
26+
java-version: 1.8
27+
28+
# Install base module
29+
- name: Install Kinesis Firehose Sample with Maven
30+
run: mvn -B install --file samples/kinesis-firehose-event-handler/pom.xml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.amazonaws</groupId>
7+
<artifactId>aws-lambda-java-events-examples</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>AWS Lambda Java Events Samples - KinesisFirehose</name>
12+
<description>
13+
AWS Lambda Java Function Examples
14+
</description>
15+
<url>https://aws.amazon.com/lambda/</url>
16+
<licenses>
17+
<license>
18+
<name>Apache License, Version 2.0</name>
19+
<url>https://aws.amazon.com/apache2.0</url>
20+
<distribution>repo</distribution>
21+
</license>
22+
</licenses>
23+
<scm>
24+
<url>https://github.com/aws/aws-lambda-java-libs.git</url>
25+
</scm>
26+
<developers>
27+
<developer>
28+
<name>AWS Lambda team</name>
29+
<organization>Amazon Web Services</organization>
30+
<organizationUrl>https://aws.amazon.com/</organizationUrl>
31+
</developer>
32+
</developers>
33+
34+
<properties>
35+
<maven.compiler.source>1.8</maven.compiler.source>
36+
<maven.compiler.target>1.8</maven.compiler.target>
37+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
38+
</properties>
39+
40+
<dependencies>
41+
<dependency>
42+
<groupId>com.amazonaws</groupId>
43+
<artifactId>aws-lambda-java-core</artifactId>
44+
<version>1.2.1</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>com.amazonaws</groupId>
48+
<artifactId>aws-lambda-java-events</artifactId>
49+
<version>3.10.0</version>
50+
</dependency>
51+
52+
<dependency>
53+
<groupId>org.junit.jupiter</groupId>
54+
<artifactId>junit-jupiter</artifactId>
55+
<version>RELEASE</version>
56+
<scope>test</scope>
57+
</dependency>
58+
<dependency>
59+
<groupId>com.amazonaws</groupId>
60+
<artifactId>aws-lambda-java-tests</artifactId>
61+
<version>1.1.0</version>
62+
<scope>test</scope>
63+
</dependency>
64+
</dependencies>
65+
66+
<build>
67+
<plugins>
68+
<plugin>
69+
<groupId>org.apache.maven.plugins</groupId>
70+
<artifactId>maven-surefire-plugin</artifactId>
71+
<version>2.22.2</version>
72+
</plugin>
73+
</plugins>
74+
</build>
75+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package example;
2+
3+
import com.amazonaws.services.lambda.runtime.Context;
4+
import com.amazonaws.services.lambda.runtime.RequestHandler;
5+
import com.amazonaws.services.lambda.runtime.events.KinesisAnalyticsInputPreprocessingResponse;
6+
import com.amazonaws.services.lambda.runtime.events.KinesisFirehoseEvent;
7+
8+
import java.nio.ByteBuffer;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
import static com.amazonaws.services.lambda.runtime.events.KinesisAnalyticsInputPreprocessingResponse.Result.Ok;
13+
import static java.nio.charset.StandardCharsets.UTF_8;
14+
15+
/**
16+
* A sample KinesisFirehoseEvent handler
17+
*
18+
* For more information see the developer guide - https://docs.aws.amazon.com/firehose/latest/dev/data-transformation.html
19+
*/
20+
public class KinesisFirehoseEventHandler implements RequestHandler<KinesisFirehoseEvent, KinesisAnalyticsInputPreprocessingResponse> {
21+
22+
@Override
23+
public KinesisAnalyticsInputPreprocessingResponse handleRequest(KinesisFirehoseEvent kinesisFirehoseEvent, Context context) {
24+
List<KinesisAnalyticsInputPreprocessingResponse.Record> records = new ArrayList<>();
25+
26+
for (KinesisFirehoseEvent.Record record : kinesisFirehoseEvent.getRecords()) {
27+
String recordData = new String(record.getData().array());
28+
// Your business logic
29+
String reversedString = new StringBuilder(recordData).reverse().toString();
30+
31+
records.add(new KinesisAnalyticsInputPreprocessingResponse.Record(record.getRecordId(), Ok, ByteBuffer.wrap(reversedString.getBytes(UTF_8))));
32+
}
33+
34+
return new KinesisAnalyticsInputPreprocessingResponse(records);
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package example;
2+
3+
import com.amazonaws.services.lambda.runtime.Context;
4+
import com.amazonaws.services.lambda.runtime.events.KinesisAnalyticsInputPreprocessingResponse;
5+
import com.amazonaws.services.lambda.runtime.events.KinesisFirehoseEvent;
6+
import com.amazonaws.services.lambda.runtime.tests.annotations.Event;
7+
import org.junit.jupiter.api.Assertions;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
10+
import static java.nio.charset.StandardCharsets.UTF_8;
11+
12+
public class KinesisFirehoseEventHandlerTest {
13+
14+
private Context context; // intentionally null as it's not used in the test
15+
16+
@ParameterizedTest
17+
@Event(value = "event.json", type = KinesisFirehoseEvent.class)
18+
public void testEventHandler(KinesisFirehoseEvent event) {
19+
KinesisFirehoseEventHandler kinesisFirehoseEventHandler = new KinesisFirehoseEventHandler();
20+
KinesisAnalyticsInputPreprocessingResponse response = kinesisFirehoseEventHandler.handleRequest(event, context);
21+
22+
String expectedString = "\n!dlroW olleH";
23+
KinesisAnalyticsInputPreprocessingResponse.Record firstRecord = response.getRecords().get(0);
24+
Assertions.assertEquals(expectedString, UTF_8.decode(firstRecord.getData()).toString());
25+
Assertions.assertEquals(KinesisAnalyticsInputPreprocessingResponse.Result.Ok, firstRecord.getResult());
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"invocationId": "invoked123",
3+
"deliveryStreamArn": "aws:lambda:events",
4+
"region": "us-west-2",
5+
"records": [
6+
{
7+
"data": "SGVsbG8gV29ybGQhCg==",
8+
"recordId": "record2",
9+
"approximateArrivalTimestamp": 1510772160000,
10+
"kinesisRecordMetadata": {
11+
"shardId": "shardId-000000000000",
12+
"partitionKey": "4d1ad2b9-24f8-4b9d-a088-76e9947c317a",
13+
"approximateArrivalTimestamp": "2012-04-23T18:25:43.511Z",
14+
"sequenceNumber": "49546986683135544286507457936321625675700192471156785154",
15+
"subsequenceNumber": ""
16+
}
17+
}
18+
]
19+
}

0 commit comments

Comments
 (0)