TsReaper commented on a change in pull request #9029: [FLINK-13118][jdbc] 
Introduce JDBC table factory and bridge JDBC table source with streaming table 
source
URL: https://github.com/apache/flink/pull/9029#discussion_r301882466
 
 

 ##########
 File path: 
flink-connectors/flink-jdbc/src/main/java/org/apache/flink/api/java/io/jdbc/JDBCTableSourceSinkFactory.java
 ##########
 @@ -0,0 +1,268 @@
+/*
+ * 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.api.java.io.jdbc;
+
+import org.apache.flink.api.java.io.jdbc.dialect.JDBCDialect;
+import org.apache.flink.api.java.io.jdbc.dialect.JDBCDialects;
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.table.descriptors.DescriptorProperties;
+import org.apache.flink.table.descriptors.SchemaValidator;
+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 java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+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.Rowtime.ROWTIME_TIMESTAMPS_CLASS;
+import static 
org.apache.flink.table.descriptors.Rowtime.ROWTIME_TIMESTAMPS_FROM;
+import static 
org.apache.flink.table.descriptors.Rowtime.ROWTIME_TIMESTAMPS_SERIALIZED;
+import static 
org.apache.flink.table.descriptors.Rowtime.ROWTIME_TIMESTAMPS_TYPE;
+import static 
org.apache.flink.table.descriptors.Rowtime.ROWTIME_WATERMARKS_CLASS;
+import static 
org.apache.flink.table.descriptors.Rowtime.ROWTIME_WATERMARKS_DELAY;
+import static 
org.apache.flink.table.descriptors.Rowtime.ROWTIME_WATERMARKS_SERIALIZED;
+import static 
org.apache.flink.table.descriptors.Rowtime.ROWTIME_WATERMARKS_TYPE;
+import static org.apache.flink.table.descriptors.Schema.SCHEMA;
+import static org.apache.flink.table.descriptors.Schema.SCHEMA_FROM;
+import static org.apache.flink.table.descriptors.Schema.SCHEMA_NAME;
+import static org.apache.flink.table.descriptors.Schema.SCHEMA_PROCTIME;
+import static org.apache.flink.table.descriptors.Schema.SCHEMA_TYPE;
+
+/**
+ * Factory for creating configured instances of {@link JDBCTableSource} and 
{@link JDBCUpsertTableSink}.
+ */
+public class JDBCTableSourceSinkFactory implements
+       StreamTableSourceFactory<Row>,
+       StreamTableSinkFactory<Tuple2<Boolean, Row>> {
+
+       public static final String CONNECTOR_DRIVER_NAME = 
"connector.driver.name";
+       public static final String CONNECTOR_URL = "connector.url";
+       public static final String CONNECTOR_TABLE_NAME = 
"connector.table.name";
+       public static final String CONNECTOR_USERNAME = "connector.username";
+       public static final String CONNECTOR_PASSWORD = "connector.password";
+
+       public static final String CONNECTOR_SCAN_PARTITION_COLUMN_NAME = 
"connector.scan.partition.column.name";
+       public static final String CONNECTOR_SCAN_PARTITION_SIZE = 
"connector.scan.partition.size";
+       public static final String CONNECTOR_SCAN_PARTITION_LOWER = 
"connector.scan.partition.lower";
+       public static final String CONNECTOR_SCAN_PARTITION_UPPER = 
"connector.scan.partition.upper";
+
+       public static final String CONNECTOR_LOOKUP_CACHE_MAX_SIZE = 
"connector.lookup.cache.max-size";
+       public static final String CONNECTOR_LOOKUP_CACHE_EXPIRE_MS = 
"connector.lookup.cache.expire-ms";
+       public static final String CONNECTOR_LOOKUP_MAX_RETRY_TIMES = 
"connector.lookup.max-retry-times";
+
+       public static final String CONNECTOR_SINK_UPDATE_MODE = 
"connector.sink.update-mode";
+       public static final String CONNECTOR_SINK_FLUSH_MAX_SIZE = 
"connector.sink.flush.max-size";
+       public static final String CONNECTOR_SINK_FLUSH_INTERVAL_MS = 
"connector.sink.flush.interval-ms";
+       public static final String CONNECTOR_SINK_MAX_RETRY_TIMES = 
"connector.sink.max-retry-times";
+
+       @Override
+       public Map<String, String> requiredContext() {
+               Map<String, String> context = new HashMap<>();
+               context.put(CONNECTOR_TYPE, "jdbc"); // jdbc
+               context.put(CONNECTOR_PROPERTY_VERSION, "1"); // backwards 
compatibility
+               return context;
+       }
+
+       @Override
+       public List<String> supportedProperties() {
+               List<String> properties = new ArrayList<>();
+
+               // common options
+               properties.add(CONNECTOR_DRIVER_NAME);
+               properties.add(CONNECTOR_URL);
+               properties.add(CONNECTOR_TABLE_NAME);
+               properties.add(CONNECTOR_USERNAME);
+               properties.add(CONNECTOR_PASSWORD);
+
+               // scan partition options
+               properties.add(CONNECTOR_SCAN_PARTITION_COLUMN_NAME);
+               properties.add(CONNECTOR_SCAN_PARTITION_SIZE);
+               properties.add(CONNECTOR_SCAN_PARTITION_LOWER);
+               properties.add(CONNECTOR_SCAN_PARTITION_UPPER);
+
+               // lookup options
+               properties.add(CONNECTOR_LOOKUP_CACHE_MAX_SIZE);
+               properties.add(CONNECTOR_LOOKUP_CACHE_EXPIRE_MS);
+               properties.add(CONNECTOR_LOOKUP_MAX_RETRY_TIMES);
+
+               // sink options
+               properties.add(CONNECTOR_SINK_UPDATE_MODE);
+               properties.add(CONNECTOR_SINK_FLUSH_MAX_SIZE);
+               properties.add(CONNECTOR_SINK_FLUSH_INTERVAL_MS);
+               properties.add(CONNECTOR_SINK_MAX_RETRY_TIMES);
+
+               // schema
+               properties.add(SCHEMA + ".#." + SCHEMA_TYPE);
+               properties.add(SCHEMA + ".#." + SCHEMA_NAME);
+               properties.add(SCHEMA + ".#." + SCHEMA_FROM);
+
+               // time attributes
+               properties.add(SCHEMA + ".#." + SCHEMA_PROCTIME);
+               properties.add(SCHEMA + ".#." + ROWTIME_TIMESTAMPS_TYPE);
+               properties.add(SCHEMA + ".#." + ROWTIME_TIMESTAMPS_FROM);
+               properties.add(SCHEMA + ".#." + ROWTIME_TIMESTAMPS_CLASS);
+               properties.add(SCHEMA + ".#." + ROWTIME_TIMESTAMPS_SERIALIZED);
+               properties.add(SCHEMA + ".#." + ROWTIME_WATERMARKS_TYPE);
+               properties.add(SCHEMA + ".#." + ROWTIME_WATERMARKS_CLASS);
+               properties.add(SCHEMA + ".#." + ROWTIME_WATERMARKS_SERIALIZED);
+               properties.add(SCHEMA + ".#." + ROWTIME_WATERMARKS_DELAY);
+
+               return properties;
+       }
+
+       @Override
+       public StreamTableSource<Row> createStreamTableSource(Map<String, 
String> properties) {
+               final DescriptorProperties descriptorProperties = 
getValidatedProperties(properties);
+
+               final JDBCOptions options = 
getJDBCOptions(descriptorProperties);
+
+               final Optional<String> partitionColumnName =
+                       
descriptorProperties.getOptionalString(CONNECTOR_SCAN_PARTITION_COLUMN_NAME);
+               final Optional<String> partitionSize = 
descriptorProperties.getOptionalString(CONNECTOR_SCAN_PARTITION_SIZE);
+               final Optional<String> partitionLower = 
descriptorProperties.getOptionalString(CONNECTOR_SCAN_PARTITION_LOWER);
+               final Optional<String> partitionUpper = 
descriptorProperties.getOptionalString(CONNECTOR_SCAN_PARTITION_UPPER);
+               final JDBCScanOptions scanOptions;
+               if (partitionColumnName.isPresent()) {
+                       Preconditions.checkArgument(
+                               partitionSize.isPresent() && 
partitionLower.isPresent() && partitionUpper.isPresent(),
+                               "Either all or none of the properties of scan 
options should be provided");
+                       scanOptions = JDBCScanOptions.builder()
+                               
.setPartitionColumnName(partitionColumnName.get())
+                               
.setPartitionSize(getLongOrThrow(partitionSize.get(), 
CONNECTOR_SCAN_PARTITION_SIZE))
+                               
.setPartitionLowerBound(getLongOrThrow(partitionLower.get(), 
CONNECTOR_SCAN_PARTITION_LOWER))
+                               
.setPartitionUpperBound(getLongOrThrow(partitionUpper.get(), 
CONNECTOR_SCAN_PARTITION_UPPER))
+                               .build();
+               } else {
+                       scanOptions = null;
+               }
+
+               final Optional<String> lookupCacheMaxSize =
+                       
descriptorProperties.getOptionalString(CONNECTOR_LOOKUP_CACHE_MAX_SIZE);
+               final Optional<String> lookupCacheExpireMs =
+                       
descriptorProperties.getOptionalString(CONNECTOR_LOOKUP_CACHE_EXPIRE_MS);
+               final Optional<String> lookupMaxRetryTimes =
+                       
descriptorProperties.getOptionalString(CONNECTOR_LOOKUP_MAX_RETRY_TIMES);
+               JDBCLookupOptions.Builder lookupOptionsBuilder = 
JDBCLookupOptions.builder();
+               if (lookupCacheMaxSize.isPresent()) {
+                       lookupOptionsBuilder = 
lookupOptionsBuilder.setCacheMaxSize(
+                               getLongOrThrow(lookupCacheMaxSize.get(), 
CONNECTOR_LOOKUP_CACHE_MAX_SIZE));
+               }
+               if (lookupCacheExpireMs.isPresent()) {
+                       lookupOptionsBuilder = 
lookupOptionsBuilder.setCacheExpireMs(
+                               getLongOrThrow(lookupCacheExpireMs.get(), 
CONNECTOR_LOOKUP_CACHE_EXPIRE_MS));
+               }
+               if (lookupMaxRetryTimes.isPresent()) {
+                       lookupOptionsBuilder = 
lookupOptionsBuilder.setMaxRetryTimes(
+                               getIntOrThrow(lookupMaxRetryTimes.get(), 
CONNECTOR_LOOKUP_MAX_RETRY_TIMES));
+               }
+               final JDBCLookupOptions lookupOptions = 
lookupOptionsBuilder.build();
+
+               return JDBCTableSource.builder()
+                               .setOptions(options)
+                               .setScanOptions(scanOptions)
+                               .setLookupOptions(lookupOptions)
+                               
.setSchema(descriptorProperties.getTableSchema(SCHEMA))
+                               .build();
+       }
+
+       @Override
+       public StreamTableSink<Tuple2<Boolean, Row>> 
createStreamTableSink(Map<String, String> properties) {
+               final DescriptorProperties descriptorProperties = 
getValidatedProperties(properties);
+
+               JDBCOptions options = getJDBCOptions(descriptorProperties);
+               JDBCUpsertTableSink.Builder builder = 
JDBCUpsertTableSink.builder()
+                       .setOptions(options)
+                       
.setTableSchema(descriptorProperties.getTableSchema(SCHEMA));
+
+               Optional<String> size = 
descriptorProperties.getOptionalString(CONNECTOR_SINK_FLUSH_MAX_SIZE);
+               Optional<String> time = 
descriptorProperties.getOptionalString(CONNECTOR_SINK_FLUSH_INTERVAL_MS);
+               Optional<String> retry = 
descriptorProperties.getOptionalString(CONNECTOR_SINK_MAX_RETRY_TIMES);
+               if (size.isPresent()) {
+                       builder = 
builder.setFlushMaxSize(getIntOrThrow(size.get(), 
CONNECTOR_SINK_FLUSH_MAX_SIZE));
+               }
+               if (time.isPresent()) {
+                       builder = 
builder.setFlushIntervalMills(getLongOrThrow(time.get(), 
CONNECTOR_SINK_FLUSH_INTERVAL_MS));
+               }
+               if (retry.isPresent()) {
+                       builder = 
builder.setMaxRetryTimes(getIntOrThrow(retry.get(), 
CONNECTOR_SINK_MAX_RETRY_TIMES));
+               }
+
+               JDBCUpsertTableSink sink = builder.build();
+               
sink.setIsAppendOnly(descriptorProperties.getString(CONNECTOR_SINK_UPDATE_MODE).equals("append"));
+               return sink;
+       }
+
+       private DescriptorProperties getValidatedProperties(Map<String, String> 
properties) {
+               final DescriptorProperties descriptorProperties = new 
DescriptorProperties(true);
+               descriptorProperties.putProperties(properties);
+               new SchemaValidator(true, false, 
false).validate(descriptorProperties);
+               return descriptorProperties;
+       }
+
+       private JDBCOptions getJDBCOptions(DescriptorProperties 
descriptorProperties) {
+               final String driverName = 
descriptorProperties.getString(CONNECTOR_DRIVER_NAME);
 
 Review comment:
   ok

----------------------------------------------------------------
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