View Javadoc
1   package org.wikimedia.eventutilities.flink.table;
2   
3   import javax.annotation.Nonnull;
4   
5   import org.apache.flink.table.api.Schema;
6   import org.wikimedia.eventutilities.core.event.EventStream;
7   import org.wikimedia.eventutilities.flink.formats.json.JsonSchemaFlinkConverter;
8   
9   import com.fasterxml.jackson.databind.node.ObjectNode;
10  
11  /**
12   * Helper functions for creating and adding to Flink Schema.Builder for WMF event streams.
13   */
14  public final class SchemaBuilderUtil {
15  
16      /**
17       * Given an EventStream, converts its latest schema to
18       * Flink columns and returns a new Schema.Builder with all columns.
19       */
20      public static Schema.Builder createEventStreamSchemaBuilder(EventStream eventStream) {
21          return JsonSchemaFlinkConverter.toSchemaBuilder(
22              (ObjectNode) eventStream.schema()
23          );
24      }
25  
26      /**
27       * Given an EventStream, converts its schema at the given version to
28       * Flink columns and returns a new Schema.Builder with all columns.
29       */
30      public static Schema.Builder createEventStreamSchemaBuilder(EventStream eventStream, String schemaVersion) {
31          return JsonSchemaFlinkConverter.toSchemaBuilder(
32              (ObjectNode) eventStream.schema(schemaVersion)
33          );
34      }
35  
36      /**
37       * Adds a "kafka_timestamp" column to the schema builder and uses
38       * it as the watermark field with a default watermark delay of 10 seconds.
39       */
40      @Nonnull
41      public static Schema.Builder withKafkaTimestampAsWatermark(Schema.Builder schemaBuilder) {
42          return withKafkaTimestampAsWatermark(
43              schemaBuilder,
44              "kafka_timestamp",
45              10
46          );
47      }
48  
49      /**
50       * Adds kafka timestamp as a virtual column to the schema builder
51       * and uses it as the watermark with a delay of watermarkDelaySeconds.
52       *
53       * @param columnName            Name of the timestamp column to add to the schema.
54       * @param watermarkDelaySeconds Seconds to delay the watermark by.
55       */
56      @Nonnull
57      public static Schema.Builder withKafkaTimestampAsWatermark(
58          Schema.Builder schemaBuilder,
59          String columnName,
60          int watermarkDelaySeconds
61      ) {
62          String watermarkExpression;
63  
64          if (watermarkDelaySeconds == 0) {
65              watermarkExpression = columnName;
66          } else {
67              watermarkExpression = columnName + " - INTERVAL '" + watermarkDelaySeconds + "' SECOND";
68          }
69  
70          return withKafkaTimestampAsWatermark(
71              schemaBuilder,
72              columnName,
73              watermarkExpression
74          );
75      }
76  
77      /**
78       * Adds kafka timestamp as a virtual column to the schema builder
79       * and uses it as the watermark.
80       * See also {@link Schema.Builder#watermark(String, String)}
81       *
82       * @param columnName    Name of the column to add to the schema.
83       * @param sqlExpression SQL expression to use for the watermark.
84       *                      This should probably refer to the columnName, e.g. "$columnName - INTERVAL '30' seconds"
85       */
86      @Nonnull
87      public static Schema.Builder withKafkaTimestampAsWatermark(
88          Schema.Builder schemaBuilder,
89          String columnName,
90          String sqlExpression
91      ) {
92          schemaBuilder.columnByMetadata(
93              columnName,
94              "TIMESTAMP_LTZ(3) NOT NULL",
95              "timestamp",
96              true
97          ).watermark(columnName, sqlExpression);
98  
99          return schemaBuilder;
100     }
101 
102     private SchemaBuilderUtil() {
103     }
104 }