View Javadoc
1   package org.wikimedia.eventutilities.flink.table.catalog;
2   
3   import static org.apache.flink.streaming.connectors.kafka.table.KafkaConnectorOptions.TOPIC;
4   import static org.wikimedia.eventutilities.flink.formats.json.EventJsonFormatOptions.STREAM_NAME;
5   import static org.wikimedia.eventutilities.flink.table.catalog.EventStreamCatalogFactoryOptions.STREAM_PREFIX;
6   
7   import java.util.Collection;
8   import java.util.HashMap;
9   import java.util.Map;
10  
11  import org.apache.flink.configuration.ConfigOption;
12  import org.apache.flink.configuration.Configuration;
13  import org.apache.flink.streaming.connectors.kafka.table.KafkaConnectorOptions;
14  import org.wikimedia.eventutilities.core.event.EventStreamFactory;
15  import org.wikimedia.eventutilities.flink.formats.json.EventJsonFormatFactory;
16  
17  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
18  
19  @SuppressFBWarnings("STT_TOSTRING_MAP_KEYING")
20  public class CreateTableEventStreamCatalogConfiguration extends EventStreamCatalogConfiguration {
21  
22      public CreateTableEventStreamCatalogConfiguration(
23          Configuration catalogOptions,
24          Configuration tableOptions,
25          Collection<ConfigOption<?>> pseudoTableOptions,
26          Collection<ConfigOption<?>> overrideOptions,
27          Collection<ConfigOption<?>> sharedOptions,
28          Map<String, String> defaultTableOptions,
29          Map<String, String> defaultKafkaOptions,
30          Map<String, String> defaultFormatOptions,
31          EventStreamFactory eventStreamFactory
32      ) {
33          super(
34              catalogOptions,
35              tableOptions,
36              pseudoTableOptions,
37              overrideOptions,
38              sharedOptions,
39              defaultTableOptions,
40              defaultKafkaOptions,
41              defaultFormatOptions,
42              eventStreamFactory
43          );
44      }
45  
46      /**
47       * Processes options for CREATE TABLE.
48       *
49       * CREATE TABLE tableOptions are set by the user and should
50       * take precedence over any funky things the catalog does
51       */
52      @Override
53      public Map<String, String> getProcessedOptions() {
54          Map<String, String> processedOptions = new HashMap<>(this.tableOptions.toMap());
55          this.defaultTableOptions.forEach(processedOptions::putIfAbsent);
56  
57          if (isKafkaTable()) {
58              this.defaultKafkaOptions.forEach(processedOptions::putIfAbsent);
59              if (!this.tableOptions.containsKey(KafkaConnectorOptions.TOPIC.key())) {
60                  if (processedOptions.containsKey(STREAM_PREFIX.key())) {
61                      processedOptions.put(TOPIC.key(), this.get(STREAM_PREFIX) + "." + this.get(STREAM_NAME));
62                  } else {
63                      processedOptions.put(
64                          TOPIC.key(),
65                          String.join(";",
66                              eventStreamFactory.createEventStream(
67                                  this.get(STREAM_NAME)
68                              ).topics()
69                          )
70                      );
71                  }
72              }
73          }
74  
75          if (isEventJson()) {
76              this.defaultFormatOptions.forEach(processedOptions::putIfAbsent);
77              Map<String, String> catalogOptionsMap = catalogOptions.toMap();
78  
79              this.sharedOptions.forEach(o -> {
80                  String value = catalogOptionsMap.getOrDefault(o.key(), processedOptions.get(o.key()));
81                  processedOptions.remove(o.key());
82                  String prefixedKey = EventJsonFormatFactory.IDENTIFIER + "." + o.key();
83                  processedOptions.put(prefixedKey, value);
84              });
85          }
86  
87          // Remove options not needed to create the table
88          // What happens if a table option just so happens to be the same name as a pseudoTableOption?
89          this.pseudoTableOptions.stream().map(ConfigOption::key).forEach(processedOptions::remove);
90          processedOptions.entrySet().removeIf(entry -> entry.getValue() == null);
91  
92          return processedOptions;
93      }
94  
95  }