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  public class CreateTableLikeEventStreamCatalogConfiguration extends EventStreamCatalogConfiguration {
18  
19      public CreateTableLikeEventStreamCatalogConfiguration(
20          Configuration catalogOptions,
21          Configuration tableOptions,
22          Collection<ConfigOption<?>> pseudoTableOptions,
23          Collection<ConfigOption<?>> overrideOptions,
24          Collection<ConfigOption<?>> sharedOptions,
25          Map<String, String> defaultTableOptions,
26          Map<String, String> defaultKafkaOptions,
27          Map<String, String> defaultFormatOptions,
28          EventStreamFactory eventStreamFactory
29      ) {
30          super(
31              catalogOptions,
32              tableOptions,
33              pseudoTableOptions,
34              overrideOptions,
35              sharedOptions,
36              defaultTableOptions,
37              defaultKafkaOptions,
38              defaultFormatOptions,
39              eventStreamFactory
40          );
41      }
42  
43      /**
44       * Processes options for CREATE TABLE LIKE.
45       *
46       * CREATE TABLE LIKE tableOptions have already been processed once by
47       * CREATE TABLE, so this handles removing options if the user switches
48       * from kafka or event-json
49       */
50      @Override
51      public Map<String, String> getProcessedOptions() {
52          Map<String, String> processedOptions = new HashMap<>(this.catalogOptions.toMap());
53          processedOptions.putAll(this.tableOptions.toMap());
54  
55          if (!isKafkaTable()) {
56              this.defaultKafkaOptions.forEach(
57                  (k, v) -> processedOptions.entrySet().removeIf(
58                      entry -> entry.getKey().equals(k)
59                  )
60              );
61              processedOptions.remove(KafkaConnectorOptions.TOPIC.key());
62          } else {
63              if (processedOptions.containsKey(STREAM_PREFIX.key())) {
64                  processedOptions.put(TOPIC.key(), this.get(STREAM_PREFIX) + "." + this.get(STREAM_NAME));
65              }
66          }
67  
68          if (!isEventJson()) {
69              processedOptions.entrySet().removeIf(entry -> entry.getKey().startsWith(EventJsonFormatFactory.IDENTIFIER));
70          }
71  
72          this.pseudoTableOptions.stream().map(ConfigOption::key).forEach(processedOptions::remove);
73          processedOptions.entrySet().removeIf(entry -> entry.getValue() == null);
74  
75          return processedOptions;
76      }
77  
78  }