-
Notifications
You must be signed in to change notification settings - Fork 239
Added event class MskFirehoseEvent.java for Firehose Lambda transformation when MSK is the source #490
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
Added event class MskFirehoseEvent.java for Firehose Lambda transformation when MSK is the source #490
Changes from 24 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
ec8dca0
Create MskFirehoseEvent.java
ShashankAWS 7df3645
Update MskFirehoseEvent.java
ShashankAWS f6f4a60
Update MskFirehoseEvent.java
ShashankAWS 51b2640
Update MskFirehoseEvent.java
ShashankAWS 7d689bb
Update README.md
ShashankAWS eb08bbe
Update and rename MskFirehoseEvent.java to MSKFirehoseEvent.java
ShashankAWS 5eb7042
Create MSKFirehoseResponse.java
ShashankAWS a720f87
Create msk_firehose_event.json
ShashankAWS df1e0e4
Create MSKFirehoseEventHandler.java
ShashankAWS 57a9a81
Create MSKFirehoseEventHandlerTest.java
ShashankAWS 5ba689e
Create event.json
ShashankAWS f240a62
Update README.md
ShashankAWS 5abcb06
Update MSKFirehoseEventHandler.java
ShashankAWS f29f105
Update MSKFirehoseEventHandler.java
ShashankAWS 24e1b10
Update MSKFirehoseEventHandlerTest.java
ShashankAWS feec28f
Update MSKFirehoseEvent.java
ShashankAWS 6fa478e
Update MSKFirehoseResponse.java
ShashankAWS 243253b
Update EventLoader.java
ShashankAWS 4505f6d
Update EventLoaderTest.java
ShashankAWS cb37072
Update EventLoaderTest.java
ShashankAWS 8239c4a
Update msk_firehose_event.json
ShashankAWS 5769d76
Update EventLoaderTest.java
ShashankAWS e12da26
Update EventLoaderTest.java
ShashankAWS 6fd7282
Update EventLoaderTest.java
ShashankAWS 4c14fec
Update MSKFirehoseEvent.java
ShashankAWS b3b5f61
Update MSKFirehoseResponse.java
ShashankAWS 62d58c1
Update MSKFirehoseEventHandler.java
ShashankAWS 641b3f4
Update MSKFirehoseEventHandlerTest.java
ShashankAWS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
50 changes: 50 additions & 0 deletions
50
...a-events/src/main/java/com/amazonaws/services/lambda/runtime/events/MSKFirehoseEvent.java
This file contains hidden or 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,50 @@ | ||
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; | ||
|
||
/** | ||
* Created by vermshas on 6/28/24. | ||
*/ | ||
|
||
@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; | ||
|
||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...vents/src/main/java/com/amazonaws/services/lambda/runtime/events/MSKFirehoseResponse.java
This file contains hidden or 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,56 @@ | ||
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. | ||
msailes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* [+] 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 { | ||
|
||
/** | ||
msailes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* 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 hidden or 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 hidden or 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 hidden or 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 @@ | ||
{ | ||
msailes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"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==" | ||
} | ||
] | ||
} |
34 changes: 34 additions & 0 deletions
34
samples/msk-firehose-event-handler/src/main/java/example/MSKFirehoseEventHandler.java
This file contains hidden or 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,34 @@ | ||
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()); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
samples/msk-firehose-event-handler/src/test/java/example/MSKFirehoseEventHandlerTest.java
This file contains hidden or 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,27 @@ | ||
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 hidden or 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==" | ||
} | ||
] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is it needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed saw it added to some classes and thought its required. Added copyright/license text to MSKFirehoseEvent.java and MSKFirehoseResponse.java. Do we need to add license text to sample as well ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, to all source code, please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added license text to samples as well.