wuchong commented on a change in pull request #8930: [FLINK-12955] HBase 
LookupableTableSource
URL: https://github.com/apache/flink/pull/8930#discussion_r300621562
 
 

 ##########
 File path: 
flink-connectors/flink-hbase/src/main/java/org/apache/flink/connectors/hbase/table/HBaseTableFactory.java
 ##########
 @@ -0,0 +1,146 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.connectors.hbase.table;
+
+import org.apache.flink.addons.hbase.HBaseTableSource;
+import org.apache.flink.table.api.TableSchema;
+import org.apache.flink.table.descriptors.DescriptorProperties;
+import org.apache.flink.table.factories.StreamTableSinkFactory;
+import org.apache.flink.table.factories.StreamTableSourceFactory;
+import org.apache.flink.table.sinks.StreamTableSink;
+import org.apache.flink.table.sources.StreamTableSource;
+import org.apache.flink.types.Row;
+import org.apache.flink.util.Preconditions;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hbase.HBaseConfiguration;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static 
org.apache.flink.connectors.hbase.table.HBaseValidator.COLUMNFAMILY_QUALIFIER_DELIMITER_PATTERN;
+import static 
org.apache.flink.connectors.hbase.table.HBaseValidator.CONNECTOR_HBASE_CLIENT_PARAM_PREFIX;
+import static 
org.apache.flink.connectors.hbase.table.HBaseValidator.CONNECTOR_HBASE_TABLE_NAME;
+import static 
org.apache.flink.connectors.hbase.table.HBaseValidator.CONNECTOR_QUALIFIER_DELIMITER;
+import static 
org.apache.flink.connectors.hbase.table.HBaseValidator.CONNECTOR_ROW_KEY;
+import static 
org.apache.flink.connectors.hbase.table.HBaseValidator.CONNECTOR_TYPE_VALUE_HBASE;
+import static 
org.apache.flink.connectors.hbase.table.HBaseValidator.CONNECTOR_VERSION_VALUE_143;
+import static 
org.apache.flink.table.descriptors.ConnectorDescriptorValidator.CONNECTOR_PROPERTY_VERSION;
+import static 
org.apache.flink.table.descriptors.ConnectorDescriptorValidator.CONNECTOR_TYPE;
+import static 
org.apache.flink.table.descriptors.ConnectorDescriptorValidator.CONNECTOR_VERSION;
+import static org.apache.flink.table.descriptors.Schema.SCHEMA;
+import static org.apache.flink.table.descriptors.Schema.SCHEMA_NAME;
+import static org.apache.flink.table.descriptors.Schema.SCHEMA_TYPE;
+
+/**
+ * Factory for creating configured instances of {@link HBaseTableSource} or 
sink.
+ */
+public class HBaseTableFactory implements StreamTableSourceFactory<Row>, 
StreamTableSinkFactory<Row> {
+       @Override
+       public StreamTableSink<Row> createStreamTableSink(Map<String, String> 
properties) {
+               //TODO NEED to wait FLINK-10206
+               throw new UnsupportedOperationException("not support 
createStreamTableSink now.");
+       }
+
+       @Override
+       public StreamTableSource<Row> createStreamTableSource(Map<String, 
String> properties) {
+               final DescriptorProperties descriptorProperties = 
getValidatedProperties(properties);
+               Configuration hbaseClientConf = 
createClientConfiguration(properties);
+               //TODO , now only for the first version, it's to hack to get 
the rowkey from TableSchema.
+               hackRowkey(descriptorProperties);
+               HBaseTableContext hBaseTableContext = 
HBaseTableContext.create(descriptorProperties, hbaseClientConf);
+               return new HBaseStreamTableSource(hBaseTableContext);
+       }
+
+       /**
+        * now it's a hack. in the future, we will remove it.
+        */
+       private void hackRowkey(DescriptorProperties descriptorProperties) {
+               TableSchema tableSchema = 
descriptorProperties.getTableSchema(SCHEMA);
+               String[] columnNames = tableSchema.getFieldNames();
+               String delimiter = 
descriptorProperties.getOptionalString(CONNECTOR_QUALIFIER_DELIMITER).orElse(
+                       COLUMNFAMILY_QUALIFIER_DELIMITER_PATTERN);
+               String rowkey = null;
+               int rowkeyCount = 0;
+               for (String column : columnNames) {
+                       if (!column.contains(delimiter)) {
+                               rowkey = column;
+                               rowkeyCount++;
+                       }
+               }
+               Preconditions.checkArgument(rowkeyCount == 1,
+                       "a column which doesn't contain delimiter(" + delimiter 
+ ") is regarded as rowkey, now only support 1 rowkey, but now has " + 
rowkeyCount);
+               descriptorProperties.putString(CONNECTOR_ROW_KEY, rowkey);
 
 Review comment:
   I don't think it's a good idea to modify properties. We can pass the rowkey 
to HBaseTableSchema directly.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to