View Javadoc
1   package org.wikimedia.eventutilities.flink.table.catalog;
2   
3   import static org.wikimedia.eventutilities.flink.formats.json.EventJsonFormatOptions.SCHEMA_VERSION;
4   import static org.wikimedia.eventutilities.flink.formats.json.EventJsonFormatOptions.STREAM_NAME;
5   
6   import java.util.Collection;
7   import java.util.HashMap;
8   import java.util.Map;
9   
10  import org.apache.flink.configuration.ConfigOption;
11  import org.apache.flink.configuration.Configuration;
12  import org.apache.flink.streaming.connectors.kafka.table.KafkaDynamicTableFactory;
13  import org.apache.flink.streaming.connectors.kafka.table.UpsertKafkaDynamicTableFactory;
14  import org.apache.flink.table.factories.FactoryUtil;
15  import org.wikimedia.eventutilities.core.event.EventStream;
16  import org.wikimedia.eventutilities.core.event.EventStreamFactory;
17  import org.wikimedia.eventutilities.flink.formats.json.EventJsonFormatFactory;
18  
19  import lombok.AllArgsConstructor;
20  
21  /**
22   * A wrapper for all the configuration of the EventStreamCatalog.
23   */
24  @AllArgsConstructor
25  public abstract class EventStreamCatalogConfiguration {
26  
27      /**
28       * Option values that are passed in when first instantiating the catalog via CREATE CATALOG.
29       */
30      protected final Configuration catalogOptions;
31  
32      /**
33       * Option values are passed in when instantiating a table within the catalog via CREATE TABLE.
34       */
35      protected final Configuration tableOptions;
36  
37      /**
38       * List of options that can be passed in via the table but not used by the table.
39       */
40      protected final Collection<ConfigOption<?>> pseudoTableOptions;
41  
42      /**
43       * List of options that can be overridden by pseudoTableOptions.
44       */
45      protected final Collection<ConfigOption<?>> overrideOptions;
46  
47      /**
48       * List of options that are shared across the catalog and format.
49       */
50      protected final Collection<ConfigOption<?>> sharedOptions;
51      protected Map<String, String> defaultTableOptions;
52      protected Map<String, String> defaultKafkaOptions;
53      protected Map<String, String> defaultFormatOptions;
54  
55      protected EventStreamFactory eventStreamFactory;
56  
57      /**
58       * This should implement logic that produces a map of finalized table options
59       * that is ready to be used in CatalogTable.of.
60       */
61      public abstract Map<String, String> getProcessedOptions();
62  
63      public boolean isKafkaTable() {
64          String connector = this.tableOptions.getString(FactoryUtil.CONNECTOR, KafkaDynamicTableFactory.IDENTIFIER);
65          return connector.equals(KafkaDynamicTableFactory.IDENTIFIER) ||
66              connector.equals(UpsertKafkaDynamicTableFactory.IDENTIFIER);
67      }
68  
69      public boolean isEventJson() {
70          String format = this.tableOptions.getString(FactoryUtil.FORMAT, EventJsonFormatFactory.IDENTIFIER);
71          return format.equals(EventJsonFormatFactory.IDENTIFIER);
72      }
73  
74      /**
75       * Gets values from Event Configuration.
76       * This retrieves the correct values from catalog and table options.
77       * It figures out overrides and format prefixes automatically.
78       * It can be thought of retrieving options from an intermediate state
79       * after overrides have been applied but before any options have been removed.
80       *
81       * @param option
82       * @param <T>
83       * @return
84       */
85      public <T> T get(ConfigOption<T> option) {
86          boolean isOverride = this.overrideOptions.contains(option);
87          boolean isShared = this.sharedOptions.contains(option);
88          boolean isPseudoTable = this.pseudoTableOptions.contains(option);
89  
90          // Schema version should dynamically default to the latest version
91          if (option.equals(SCHEMA_VERSION)) {
92              option = option.withFallbackKeys(EventJsonFormatFactory.IDENTIFIER + "." + option.key());
93              String val = tableOptions.getString((ConfigOption<String>) option, "");
94              if (val.isEmpty() || val.equals("latest")) {
95                  EventStream eventStream = eventStreamFactory.createEventStream(this.get(STREAM_NAME));
96                  // T is always the type of SCHEMA_VERSION (which should be a string)
97                  return (T) eventStream.latestSchemaVersion();
98              }
99              return (T) val;
100         }
101 
102         if (isShared) {
103             // Option should also match with prefixed option
104             option = option.withFallbackKeys(EventJsonFormatFactory.IDENTIFIER + "." + option.key());
105         }
106 
107         if (isPseudoTable && isOverride) {
108             // Need to reassign option to work in lambda function
109             ConfigOption<T> finalOption = option;
110             return tableOptions.getOptional(option).orElseGet(() -> catalogOptions.get(finalOption));
111         }
112 
113         if (isPseudoTable) {
114             return tableOptions.get(option);
115         }
116 
117         if (isOverride) {
118             ConfigOption<T> finalOption = option;
119             return tableOptions.getOptional(option).orElseGet(() -> {
120                 Map<String, String> allDefaults = new HashMap<>(defaultTableOptions);
121                 allDefaults.putAll(defaultKafkaOptions);
122                 allDefaults.putAll(defaultFormatOptions);
123                 Configuration defaults = Configuration.fromMap(allDefaults);
124                 return defaults.get(finalOption);
125             });
126         }
127 
128         ConfigOption<T> finalOption = option;
129         return tableOptions.getOptional(option).orElseGet(() -> catalogOptions.get(finalOption));
130     }
131 }