-
Notifications
You must be signed in to change notification settings - Fork 123
Cross Language Unified Event #631
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
Changes from all commits
dfa0bbd
88a9308
49119c8
b889c9c
1e127fb
1f247bd
36b0859
06432e1
cda53c5
b17fe07
85298bf
3f52c6b
3b91ad8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,35 +20,62 @@ | |
|
|
||
| import com.fasterxml.jackson.annotation.JsonCreator; | ||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.UUID; | ||
|
|
||
| /** Base class for all event types in the system. */ | ||
| public abstract class Event { | ||
| public class Event { | ||
|
|
||
| private static final ObjectMapper MAPPER = new ObjectMapper(); | ||
|
|
||
| private final UUID id; | ||
| private final String type; | ||
| private final Map<String, Object> attributes; | ||
| /** The timestamp of the source record. */ | ||
|
|
||
| /** | ||
| * Runtime-internal timestamp from the source record. Not part of the cross-language event | ||
| * contract; used by the Flink runtime for timestamp propagation. | ||
| */ | ||
| private Long sourceTimestamp; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can remove this field to keep the align between python
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I could however, specifically not use in the cross-language event contract, without any repercussions. |
||
|
|
||
| public Event() { | ||
| this(UUID.randomUUID(), new HashMap<>()); | ||
| /** Unified event with user-defined type and attributes. */ | ||
| public Event(String type, Map<String, Object> attributes) { | ||
| this(UUID.randomUUID(), type, attributes); | ||
| } | ||
|
|
||
| /** Unified event with user-defined type and empty attributes. */ | ||
| public Event(String type) { | ||
| this(type, new HashMap<>()); | ||
| } | ||
|
|
||
| @JsonCreator | ||
| public Event( | ||
| @JsonProperty("id") UUID id, | ||
| @JsonProperty("type") String type, | ||
| @JsonProperty("attributes") Map<String, Object> attributes) { | ||
| if (type == null || type.isEmpty()) { | ||
| throw new IllegalArgumentException("Event 'type' must not be null or empty."); | ||
| } | ||
| this.id = id; | ||
| this.attributes = attributes; | ||
| this.type = type; | ||
| this.attributes = attributes != null ? attributes : new HashMap<>(); | ||
| } | ||
|
|
||
| public UUID getId() { | ||
| return id; | ||
| } | ||
|
|
||
| /** Returns the event type string used for routing. */ | ||
| @JsonProperty("type") | ||
| public String getType() { | ||
| return type; | ||
| } | ||
|
|
||
| public Map<String, Object> getAttributes() { | ||
| return attributes; | ||
| } | ||
|
|
@@ -73,17 +100,42 @@ public void setSourceTimestamp(long timestamp) { | |
| this.sourceTimestamp = timestamp; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a base Event from another Event, copying id, type, and attributes. Subclasses | ||
| * override this to reconstruct typed event objects with proper field deserialization. | ||
| */ | ||
| public static Event fromEvent(Event event) { | ||
| Event copy = | ||
| new Event(event.getId(), event.getType(), new HashMap<>(event.getAttributes())); | ||
| if (event.hasSourceTimestamp()) { | ||
| copy.setSourceTimestamp(event.getSourceTimestamp()); | ||
| } | ||
| return copy; | ||
| } | ||
|
|
||
| /** | ||
| * Creates an Event from a JSON string. | ||
| * | ||
| * @param json the JSON string to deserialize | ||
| * @return the deserialized Event | ||
| * @throws IOException if JSON parsing fails or the 'type' field is missing or empty | ||
| */ | ||
| public static Event fromJson(String json) throws IOException { | ||
| return MAPPER.readValue(json, Event.class); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| Event other = (Event) o; | ||
| return Objects.equals(this.id, other.id) | ||
| && Objects.equals(this.getType(), other.getType()) | ||
| && Objects.equals(this.attributes, other.attributes); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(id, attributes); | ||
| return Objects.hash(id, getType(), attributes); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.