-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add source transformer SDK support (#17)
Signed-off-by: Keran Yang <[email protected]>
- Loading branch information
Showing
10 changed files
with
249 additions
and
16 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
52 changes: 52 additions & 0 deletions
52
.../java/io/numaproj/numaflow/examples/function/eventtimefilter/EventTimeFilterFunction.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,52 @@ | ||
package io.numaproj.numaflow.examples.function.eventtimefilter; | ||
|
||
import io.numaproj.numaflow.function.Datum; | ||
import io.numaproj.numaflow.function.FunctionServer; | ||
import io.numaproj.numaflow.function.MessageT; | ||
import io.numaproj.numaflow.function.mapt.MapTFunc; | ||
|
||
import java.io.IOException; | ||
import java.time.Instant; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* This is a simple User Defined Function example which receives a message, applies the following | ||
* data transformation, and returns the message. | ||
* <p> | ||
* If the message event time is before year 2022, drop the message. If it's within year 2022, update | ||
* the key to "within_year_2022" and update the message event time to Jan 1st 2022. | ||
* Otherwise, (exclusively after year 2022), update the key to "after_year_2022" and update the | ||
* message event time to Jan 1st 2023. | ||
*/ | ||
public class EventTimeFilterFunction { | ||
|
||
private static final Logger logger = Logger.getLogger(EventTimeFilterFunction.class.getName()); | ||
private static final Instant januaryFirst2022 = Instant.ofEpochMilli(1640995200000L); | ||
private static final Instant januaryFirst2023 = Instant.ofEpochMilli(1672531200000L); | ||
|
||
private static MessageT[] process(String key, Datum data) { | ||
Instant eventTime = data.getEventTime(); | ||
|
||
if (eventTime.isBefore(januaryFirst2022)) { | ||
return new MessageT[]{MessageT.toDrop()}; | ||
} else if (eventTime.isBefore(januaryFirst2023)) { | ||
return new MessageT[]{ | ||
MessageT.to( | ||
januaryFirst2022, | ||
"within_year_2022", | ||
data.getValue())}; | ||
} else { | ||
return new MessageT[]{ | ||
MessageT.to( | ||
januaryFirst2023, | ||
"after_year_2022", | ||
data.getValue())}; | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws IOException { | ||
new FunctionServer() | ||
.registerMapperT(new MapTFunc(EventTimeFilterFunction::process)) | ||
.start(); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.numaproj.numaflow.function; | ||
|
||
import static io.numaproj.numaflow.function.Message.ALL; | ||
import static io.numaproj.numaflow.function.Message.DROP; | ||
|
||
import java.time.Instant; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
/** | ||
* MessageT is used to wrap the data return by UDF functions. Compared with Message, MessageT | ||
* contains one more field, the event time, usually extracted from the payload. | ||
*/ | ||
@AllArgsConstructor | ||
@Getter | ||
public class MessageT { | ||
|
||
private Instant eventTime; | ||
private final String key; | ||
private final byte[] value; | ||
|
||
// creates a MessageT to be dropped | ||
public static MessageT toDrop() { | ||
return new MessageT(Instant.MIN, DROP, new byte[0]); | ||
} | ||
|
||
// creates a MessageT that will forward to all | ||
public static MessageT toAll(Instant eventTime, byte[] value) { | ||
return new MessageT(eventTime, ALL, value); | ||
} | ||
|
||
// creates a MessageT that will forward to specified "to" | ||
public static MessageT to(Instant eventTime, String to, byte[] value) { | ||
return new MessageT(eventTime, to, value); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/io/numaproj/numaflow/function/mapt/MapTFunc.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,23 @@ | ||
package io.numaproj.numaflow.function.mapt; | ||
|
||
import io.numaproj.numaflow.function.Datum; | ||
import io.numaproj.numaflow.function.MessageT; | ||
|
||
import java.util.function.BiFunction; | ||
|
||
/** | ||
* Implementation of MapTHandler instantiated from a function | ||
*/ | ||
public class MapTFunc implements MapTHandler { | ||
|
||
private final BiFunction<String, Datum, MessageT[]> mapTFn; | ||
|
||
public MapTFunc(BiFunction<String, Datum, MessageT[]> mapTFn) { | ||
this.mapTFn = mapTFn; | ||
} | ||
|
||
@Override | ||
public MessageT[] HandleDo(String key, Datum datum) { | ||
return mapTFn.apply(key, datum); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/io/numaproj/numaflow/function/mapt/MapTHandler.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,13 @@ | ||
package io.numaproj.numaflow.function.mapt; | ||
|
||
import io.numaproj.numaflow.function.Datum; | ||
import io.numaproj.numaflow.function.MessageT; | ||
|
||
/** | ||
* Interface of mapT function implementation. | ||
*/ | ||
public interface MapTHandler { | ||
|
||
// Function to process each coming message | ||
MessageT[] HandleDo(String key, Datum datum); | ||
} |
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
Oops, something went wrong.