|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.inlong.sort.hbase; |
| 20 | + |
| 21 | +import org.apache.flink.configuration.ConfigOption; |
| 22 | +import org.apache.flink.configuration.ReadableConfig; |
| 23 | +import org.apache.flink.connector.hbase.options.HBaseLookupOptions; |
| 24 | +import org.apache.flink.connector.hbase.options.HBaseWriteOptions; |
| 25 | +import org.apache.flink.connector.hbase.util.HBaseTableSchema; |
| 26 | +import org.apache.flink.connector.hbase2.source.HBaseDynamicTableSource; |
| 27 | +import org.apache.flink.table.api.TableSchema; |
| 28 | +import org.apache.flink.table.connector.sink.DynamicTableSink; |
| 29 | +import org.apache.flink.table.connector.source.DynamicTableSource; |
| 30 | +import org.apache.flink.table.factories.DynamicTableSinkFactory; |
| 31 | +import org.apache.flink.table.factories.DynamicTableSourceFactory; |
| 32 | +import org.apache.flink.table.factories.FactoryUtil.TableFactoryHelper; |
| 33 | +import org.apache.hadoop.conf.Configuration; |
| 34 | +import org.apache.inlong.sort.hbase.sink.HBaseDynamicTableSink; |
| 35 | + |
| 36 | +import java.util.HashSet; |
| 37 | +import java.util.Map; |
| 38 | +import java.util.Set; |
| 39 | + |
| 40 | +import static org.apache.flink.connector.hbase.options.HBaseOptions.LOOKUP_ASYNC; |
| 41 | +import static org.apache.flink.connector.hbase.options.HBaseOptions.LOOKUP_CACHE_MAX_ROWS; |
| 42 | +import static org.apache.flink.connector.hbase.options.HBaseOptions.LOOKUP_CACHE_TTL; |
| 43 | +import static org.apache.flink.connector.hbase.options.HBaseOptions.LOOKUP_MAX_RETRIES; |
| 44 | +import static org.apache.flink.connector.hbase.options.HBaseOptions.NULL_STRING_LITERAL; |
| 45 | +import static org.apache.flink.connector.hbase.options.HBaseOptions.PROPERTIES_PREFIX; |
| 46 | +import static org.apache.flink.connector.hbase.options.HBaseOptions.SINK_BUFFER_FLUSH_INTERVAL; |
| 47 | +import static org.apache.flink.connector.hbase.options.HBaseOptions.SINK_BUFFER_FLUSH_MAX_ROWS; |
| 48 | +import static org.apache.flink.connector.hbase.options.HBaseOptions.SINK_BUFFER_FLUSH_MAX_SIZE; |
| 49 | +import static org.apache.flink.connector.hbase.options.HBaseOptions.TABLE_NAME; |
| 50 | +import static org.apache.flink.connector.hbase.options.HBaseOptions.ZOOKEEPER_QUORUM; |
| 51 | +import static org.apache.flink.connector.hbase.options.HBaseOptions.ZOOKEEPER_ZNODE_PARENT; |
| 52 | +import static org.apache.flink.connector.hbase.options.HBaseOptions.getHBaseConfiguration; |
| 53 | +import static org.apache.flink.connector.hbase.options.HBaseOptions.getHBaseLookupOptions; |
| 54 | +import static org.apache.flink.connector.hbase.options.HBaseOptions.getHBaseWriteOptions; |
| 55 | +import static org.apache.flink.connector.hbase.options.HBaseOptions.validatePrimaryKey; |
| 56 | +import static org.apache.flink.table.factories.FactoryUtil.SINK_PARALLELISM; |
| 57 | +import static org.apache.flink.table.factories.FactoryUtil.createTableFactoryHelper; |
| 58 | +import static org.apache.inlong.sort.hbase.options.InLongOptions.INLONG_METRIC; |
| 59 | + |
| 60 | +/** HBase connector factory. */ |
| 61 | +public class HBase2DynamicTableFactory |
| 62 | + implements DynamicTableSourceFactory, DynamicTableSinkFactory { |
| 63 | + |
| 64 | + private static final String IDENTIFIER = "hbase-2.2-inlong"; |
| 65 | + |
| 66 | + @Override |
| 67 | + public DynamicTableSource createDynamicTableSource(Context context) { |
| 68 | + TableFactoryHelper helper = createTableFactoryHelper(this, context); |
| 69 | + helper.validateExcept(PROPERTIES_PREFIX); |
| 70 | + |
| 71 | + final ReadableConfig tableOptions = helper.getOptions(); |
| 72 | + |
| 73 | + TableSchema tableSchema = context.getCatalogTable().getSchema(); |
| 74 | + Map<String, String> options = context.getCatalogTable().getOptions(); |
| 75 | + |
| 76 | + validatePrimaryKey(tableSchema); |
| 77 | + |
| 78 | + String tableName = tableOptions.get(TABLE_NAME); |
| 79 | + Configuration hbaseConf = getHBaseConfiguration(options); |
| 80 | + HBaseLookupOptions lookupOptions = getHBaseLookupOptions(tableOptions); |
| 81 | + String nullStringLiteral = tableOptions.get(NULL_STRING_LITERAL); |
| 82 | + HBaseTableSchema hbaseSchema = HBaseTableSchema.fromTableSchema(tableSchema); |
| 83 | + |
| 84 | + return new HBaseDynamicTableSource( |
| 85 | + hbaseConf, tableName, hbaseSchema, nullStringLiteral, lookupOptions); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public DynamicTableSink createDynamicTableSink(Context context) { |
| 90 | + TableFactoryHelper helper = createTableFactoryHelper(this, context); |
| 91 | + helper.validateExcept(PROPERTIES_PREFIX); |
| 92 | + |
| 93 | + final ReadableConfig tableOptions = helper.getOptions(); |
| 94 | + |
| 95 | + TableSchema tableSchema = context.getCatalogTable().getSchema(); |
| 96 | + Map<String, String> options = context.getCatalogTable().getOptions(); |
| 97 | + |
| 98 | + validatePrimaryKey(tableSchema); |
| 99 | + |
| 100 | + String tableName = tableOptions.get(TABLE_NAME); |
| 101 | + Configuration hbaseConf = getHBaseConfiguration(options); |
| 102 | + HBaseWriteOptions hBaseWriteOptions = getHBaseWriteOptions(tableOptions); |
| 103 | + String nullStringLiteral = tableOptions.get(NULL_STRING_LITERAL); |
| 104 | + HBaseTableSchema hbaseSchema = HBaseTableSchema.fromTableSchema(tableSchema); |
| 105 | + String inLongMetric = tableOptions.get(INLONG_METRIC); |
| 106 | + |
| 107 | + return new HBaseDynamicTableSink( |
| 108 | + tableName, hbaseSchema, hbaseConf, hBaseWriteOptions, nullStringLiteral, inLongMetric); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public String factoryIdentifier() { |
| 113 | + return IDENTIFIER; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public Set<ConfigOption<?>> requiredOptions() { |
| 118 | + Set<ConfigOption<?>> set = new HashSet<>(); |
| 119 | + set.add(TABLE_NAME); |
| 120 | + return set; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public Set<ConfigOption<?>> optionalOptions() { |
| 125 | + Set<ConfigOption<?>> set = new HashSet<>(); |
| 126 | + set.add(ZOOKEEPER_ZNODE_PARENT); |
| 127 | + set.add(ZOOKEEPER_QUORUM); |
| 128 | + set.add(NULL_STRING_LITERAL); |
| 129 | + set.add(SINK_BUFFER_FLUSH_MAX_SIZE); |
| 130 | + set.add(SINK_BUFFER_FLUSH_MAX_ROWS); |
| 131 | + set.add(SINK_BUFFER_FLUSH_INTERVAL); |
| 132 | + set.add(SINK_PARALLELISM); |
| 133 | + set.add(LOOKUP_ASYNC); |
| 134 | + set.add(LOOKUP_CACHE_MAX_ROWS); |
| 135 | + set.add(LOOKUP_CACHE_TTL); |
| 136 | + set.add(LOOKUP_MAX_RETRIES); |
| 137 | + set.add(INLONG_METRIC); |
| 138 | + return set; |
| 139 | + } |
| 140 | +} |
0 commit comments