-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added event class MskFirehoseEvent.java for Firehose Lambda transform…
…ation when MSK is the source (#490) * Create MskFirehoseEvent.java * Create MSKFirehoseResponse.java
- Loading branch information
1 parent
9a5450a
commit c0b4f60
Showing
9 changed files
with
238 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...a-events/src/main/java/com/amazonaws/services/lambda/runtime/events/MSKFirehoseEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.amazonaws.services.lambda.runtime.events; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@Builder(setterPrefix = "with") | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
|
||
public class MSKFirehoseEvent { | ||
|
||
private String invocationId; | ||
|
||
private String deliveryStreamArn; | ||
|
||
private String sourceMSKArn; | ||
|
||
private String region; | ||
|
||
private List<Record> records; | ||
|
||
@Data | ||
@Builder(setterPrefix = "with") | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class Record { | ||
|
||
private ByteBuffer kafkaRecordValue; | ||
|
||
private String recordId; | ||
|
||
private Long approximateArrivalEpoch; | ||
|
||
private Long approximateArrivalTimestamp; | ||
|
||
private Map<String, String> mskRecordMetadata; | ||
|
||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...vents/src/main/java/com/amazonaws/services/lambda/runtime/events/MSKFirehoseResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.amazonaws.services.lambda.runtime.events; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.List; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* Response model for Amazon Data Firehose Lambda transformation with MSK as a source. | ||
* [+] Amazon Data Firehose Data Transformation - Data Transformation and Status Model - <a href="https://docs.aws.amazon.com/firehose/latest/dev/data-transformation.html#data-transformation-status-model">...</a> | ||
* OK : Indicates that processing of this item succeeded. | ||
* ProcessingFailed : Indicate that the processing of this item failed. | ||
* Dropped : Indicates that this item should be silently dropped | ||
*/ | ||
|
||
@Data | ||
@Builder(setterPrefix = "with") | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
|
||
public class MSKFirehoseResponse { | ||
|
||
public enum Result { | ||
|
||
/** | ||
* Indicates that processing of this item succeeded. | ||
*/ | ||
Ok, | ||
|
||
/** | ||
* Indicate that the processing of this item failed | ||
*/ | ||
ProcessingFailed, | ||
|
||
/** | ||
* Indicates that this item should be silently dropped | ||
*/ | ||
Dropped | ||
} | ||
public List<Record> records; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@Builder(setterPrefix = "with") | ||
@AllArgsConstructor | ||
|
||
public static class Record { | ||
public String recordId; | ||
public Result result; | ||
public ByteBuffer kafkaRecordValue; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
aws-lambda-java-tests/src/test/resources/msk_firehose_event.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"invocationId": "12345621-4787-0000-a418-36e56Example", | ||
"sourceMSKArn": "arn:aws:kafka:EXAMPLE", | ||
"deliveryStreamArn": "arn:aws:firehose:EXAMPLE", | ||
"region": "us-east-1", | ||
"records": [ | ||
{ | ||
"recordId": "00000000000000000000000000000000000000000000000000000000000000", | ||
"approximateArrivalTimestamp": 1716369573887, | ||
"mskRecordMetadata": { | ||
"offset": "0", | ||
"partitionId": "1", | ||
"approximateArrivalTimestamp": 1716369573887 | ||
}, | ||
"kafkaRecordValue": "eyJOYW1lIjoiSGVsbG8gV29ybGQifQ==" | ||
} | ||
] | ||
} |
39 changes: 39 additions & 0 deletions
39
samples/msk-firehose-event-handler/src/main/java/example/MSKFirehoseEventHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package example; | ||
|
||
import com.amazonaws.services.lambda.runtime.Context; | ||
import com.amazonaws.services.lambda.runtime.RequestHandler; | ||
import com.amazonaws.services.lambda.runtime.events.MSKFirehoseResponse; | ||
import com.amazonaws.services.lambda.runtime.events.MSKFirehoseEvent; | ||
import org.json.JSONObject; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* A sample MSKFirehoseEvent handler | ||
* For more information see the developer guide - <a href="https://docs.aws.amazon.com/firehose/latest/dev/data-transformation.html">...</a> | ||
*/ | ||
public class MSKFirehoseEventHandler implements RequestHandler<MSKFirehoseEvent, MSKFirehoseResponse> { | ||
|
||
@Override | ||
public MSKFirehoseResponse handleRequest(MSKFirehoseEvent MSKFirehoseEvent, Context context) { | ||
List<MSKFirehoseResponse.Record> records = new ArrayList<>(); | ||
|
||
for (MSKFirehoseEvent.Record record : MSKFirehoseEvent.getRecords()) { | ||
String recordData = new String(record.getKafkaRecordValue().array()); | ||
// Your business logic | ||
JSONObject jsonObject = new JSONObject(recordData); | ||
records.add(new MSKFirehoseResponse.Record(record.getRecordId(), MSKFirehoseResponse.Result.Ok, encode(jsonObject.toString()))); | ||
} | ||
return new MSKFirehoseResponse(records); | ||
} | ||
private ByteBuffer encode(String content) { | ||
return ByteBuffer.wrap(content.getBytes()); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
samples/msk-firehose-event-handler/src/test/java/example/MSKFirehoseEventHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package example; | ||
|
||
import com.amazonaws.services.lambda.runtime.Context; | ||
import com.amazonaws.services.lambda.runtime.tests.annotations.Event; | ||
import com.amazonaws.services.lambda.runtime.events.MSKFirehoseEvent; | ||
import com.amazonaws.services.lambda.runtime.events.MSKFirehoseResponse; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
public class MSKFirehoseEventHandlerTest { | ||
|
||
private Context context; // intentionally null as it's not used in the test | ||
|
||
@ParameterizedTest | ||
@Event(value = "event.json", type = MSKFirehoseEvent.class) | ||
public void testEventHandler(MSKFirehoseEvent event) { | ||
MSKFirehoseEventHandler Sample = new MSKFirehoseEventHandler(); | ||
MSKFirehoseResponse response = Sample.handleRequest(event, context); | ||
|
||
String expectedString = "{\"Name\":\"Hello World\"}"; | ||
MSKFirehoseResponse.Record firstRecord = response.getRecords().get(0); | ||
Assertions.assertEquals(expectedString, UTF_8.decode(firstRecord.getKafkaRecordValue()).toString()); | ||
Assertions.assertEquals(MSKFirehoseResponse.Result.Ok, firstRecord.getResult()); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
samples/msk-firehose-event-handler/src/test/resources/event.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"invocationId": "12345621-4787-0000-a418-36e56Example", | ||
"sourceMSKArn": "", | ||
"deliveryStreamArn": "", | ||
"region": "us-east-1", | ||
"records": [ | ||
{ | ||
"recordId": "00000000000000000000000000000000000000000000000000000000000000", | ||
"approximateArrivalTimestamp": 1716369573887, | ||
"mskRecordMetadata": { | ||
"offset": "0", | ||
"partitionId": "1", | ||
"approximateArrivalTimestamp": 1716369573887 | ||
}, | ||
"kafkaRecordValue": "eyJOYW1lIjoiSGVsbG8gV29ybGQifQ==" | ||
} | ||
] | ||
} |