1 package org.wikimedia.eventutilities.flink.formats.json;
2
3 import java.time.Instant;
4
5 import javax.annotation.Nullable;
6 import javax.annotation.ParametersAreNonnullByDefault;
7
8 import org.apache.flink.api.common.serialization.SerializationSchema;
9 import org.apache.flink.api.java.typeutils.RowTypeInfo;
10 import org.apache.flink.connector.kafka.sink.KafkaRecordSerializationSchema;
11 import org.apache.flink.connector.kafka.sink.KafkaPartitioner;
12 import org.apache.flink.types.Row;
13 import org.apache.kafka.clients.producer.ProducerRecord;
14 import org.wikimedia.eventutilities.core.SerializableClock;
15 import org.wikimedia.eventutilities.flink.EventRowTypeInfo;
16
17
18
19
20
21 @ParametersAreNonnullByDefault
22 public class KafkaEventSerializationSchema implements KafkaRecordSerializationSchema<Row> {
23 private final String topic;
24 @Nullable
25 private final KafkaPartitioner<Row> partitioner;
26 private final SerializationSchema<Row> serializationSchema;
27
28 private final SerializableClock ingestionTimeClock;
29
30 private final EventRowTypeInfo eventRowTypeInfo;
31 private final KafkaRecordTimestampStrategy timestampStrategy;
32 @Nullable private final JsonRowSerializationSchema jsonKeySerializer;
33 public KafkaEventSerializationSchema(
34 RowTypeInfo typeInformation,
35 SerializationSchema<Row> serializationSchema,
36 SerializableClock ingestionTimeClock,
37 String topic,
38 KafkaRecordTimestampStrategy timestampStrategy,
39 @Nullable KafkaPartitioner<Row> partitioner
40 ) {
41 this.timestampStrategy = timestampStrategy;
42 this.topic = topic;
43 this.partitioner = partitioner;
44 this.serializationSchema = serializationSchema;
45
46 this.ingestionTimeClock = ingestionTimeClock;
47 this.eventRowTypeInfo = typeInformation instanceof EventRowTypeInfo ? (EventRowTypeInfo) typeInformation : EventRowTypeInfo.create(typeInformation);
48
49 if (eventRowTypeInfo.hasKey()) {
50 this.jsonKeySerializer = JsonRowSerializationSchema
51 .builder()
52 .withTypeInfo(eventRowTypeInfo.keyTypeInfo())
53 .withoutNormalization()
54 .build();
55 } else {
56 this.jsonKeySerializer = null;
57 }
58
59
60 }
61
62 @Override
63 public void open(SerializationSchema.InitializationContext context, KafkaSinkContext sinkContext) {
64 if (partitioner != null) {
65 partitioner.open(sinkContext.getParallelInstanceId(), sinkContext.getNumberOfParallelInstances());
66 }
67 }
68
69 @Override
70 public ProducerRecord<byte[], byte[]> serialize(Row element, KafkaSinkContext context, @Nullable Long timestamp) {
71 Instant ingestionTime = ingestionTimeClock.get();
72 eventRowTypeInfo.setIngestionTime(element, ingestionTime);
73 Instant kafkaTimestamp;
74 switch (timestampStrategy) {
75 case ROW_EVENT_TIME:
76 kafkaTimestamp = eventRowTypeInfo.getEventTime(element);
77 if (kafkaTimestamp == null) {
78 throw new IllegalArgumentException("The row " + element + " does not have its event time set");
79 }
80 break;
81 case FLINK_RECORD_EVENT_TIME:
82 if (timestamp == null) {
83 throw new IllegalArgumentException("The stream record timestamp is null, " +
84 "the pipeline must set record timestamps");
85 }
86 kafkaTimestamp = Instant.ofEpochMilli(timestamp);
87 eventRowTypeInfo.setEventTime(element, kafkaTimestamp);
88 break;
89 case ROW_INGESTION_TIME:
90 kafkaTimestamp = ingestionTime;
91 break;
92 default:
93 throw new UnsupportedOperationException("Unsupported timestamp strategy: " + timestampStrategy);
94 }
95
96 byte[] messageKey = null;
97
98 if (eventRowTypeInfo.hasKey()) {
99 messageKey = jsonKeySerializer.serialize(eventRowTypeInfo.extractKey(element));
100 }
101 byte[] messageBody = serializationSchema.serialize(element);
102
103 Integer partition = partitioner != null ? partitioner.partition(element, null, messageBody, topic, context.getPartitionsForTopic(topic)) : null;
104 return new ProducerRecord<>(topic, partition, kafkaTimestamp.toEpochMilli(), messageKey, messageBody);
105 }
106
107
108 }