yuxiqian commented on code in PR #3916: URL: https://github.com/apache/flink-cdc/pull/3916#discussion_r2022484480
########## flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-jdbc-parent/flink-cdc-pipeline-connector-jdbc-core/src/main/java/org/apache/flink/cdc/connectors/jdbc/factory/JdbcDataSinkFactory.java: ########## @@ -0,0 +1,163 @@ +/* + * 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.cdc.connectors.jdbc.factory; + +import org.apache.flink.cdc.common.configuration.ConfigOption; +import org.apache.flink.cdc.common.configuration.Configuration; +import org.apache.flink.cdc.common.factories.DataSinkFactory; +import org.apache.flink.cdc.common.factories.FactoryHelper; +import org.apache.flink.cdc.common.sink.DataSink; +import org.apache.flink.cdc.connectors.base.utils.OptionUtils; +import org.apache.flink.cdc.connectors.jdbc.config.JdbcSinkConfig; +import org.apache.flink.cdc.connectors.jdbc.dialect.JdbcSinkDialectFactory; +import org.apache.flink.cdc.connectors.jdbc.options.JdbcSinkOptions; +import org.apache.flink.cdc.connectors.jdbc.sink.JdbcDataSink; +import org.apache.flink.configuration.ConfigurationUtils; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.ServiceConfigurationError; +import java.util.ServiceLoader; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.apache.flink.cdc.connectors.jdbc.options.JdbcSinkOptions.CONNECTION_POOL_SIZE; +import static org.apache.flink.cdc.connectors.jdbc.options.JdbcSinkOptions.CONNECT_MAX_RETRIES; +import static org.apache.flink.cdc.connectors.jdbc.options.JdbcSinkOptions.CONNECT_TIMEOUT; +import static org.apache.flink.cdc.connectors.jdbc.options.JdbcSinkOptions.CONN_URL; +import static org.apache.flink.cdc.connectors.jdbc.options.JdbcSinkOptions.DRIVER_CLASS_NAME; +import static org.apache.flink.cdc.connectors.jdbc.options.JdbcSinkOptions.JDBC_PROPERTIES_PROP_PREFIX; +import static org.apache.flink.cdc.connectors.jdbc.options.JdbcSinkOptions.PASSWORD; +import static org.apache.flink.cdc.connectors.jdbc.options.JdbcSinkOptions.SERVER_TIME_ZONE; +import static org.apache.flink.cdc.connectors.jdbc.options.JdbcSinkOptions.USERNAME; +import static org.apache.flink.cdc.connectors.jdbc.options.JdbcSinkOptions.WRITE_BATCH_INTERVAL_MS; +import static org.apache.flink.cdc.connectors.jdbc.options.JdbcSinkOptions.WRITE_BATCH_SIZE; +import static org.apache.flink.cdc.connectors.jdbc.options.JdbcSinkOptions.WRITE_MAX_RETRIES; + +/** A {@link DataSinkFactory} for creating JDBC sinks. */ +public class JdbcDataSinkFactory implements DataSinkFactory { + + public static final String IDENTIFIER = "jdbc"; + + private static final Logger LOG = LoggerFactory.getLogger(JdbcDataSinkFactory.class); + + @Override + public DataSink createDataSink(Context context) { + FactoryHelper.createFactoryHelper(this, context) + .validateExcept(JDBC_PROPERTIES_PROP_PREFIX); + + // Construct JdbcSinkConfig from FactoryConfigurations + final Configuration config = context.getFactoryConfiguration(); + JdbcSinkConfig.Builder<?> builder = new JdbcSinkConfig.Builder<>(); + + List<JdbcSinkDialectFactory<JdbcSinkConfig>> dialectFactories = + discoverDialectFactories(getClass().getClassLoader()); + config.getOptional(CONN_URL).ifPresent(builder::connUrl); + config.getOptional(USERNAME).ifPresent(builder::username); + config.getOptional(PASSWORD).ifPresent(builder::password); + + builder.serverTimeZone(config.getOptional(SERVER_TIME_ZONE).orElse("UTC")); + builder.connectTimeout(config.get(CONNECT_TIMEOUT)); + builder.connectionPoolSize(config.get(CONNECTION_POOL_SIZE)); + builder.connectMaxRetries(config.get(CONNECT_MAX_RETRIES)); + builder.writeBatchIntervalMs(config.get(WRITE_BATCH_INTERVAL_MS)); + builder.writeBatchSize(config.get(WRITE_BATCH_SIZE)); + builder.writeMaxRetries(config.get(WRITE_MAX_RETRIES)); + builder.driverClassName(config.get(DRIVER_CLASS_NAME)); + + Properties properties = new Properties(); + Map<String, String> jdbcProperties = + JdbcSinkOptions.getPropertiesByPrefix(config, JDBC_PROPERTIES_PROP_PREFIX); + properties.putAll(jdbcProperties); + builder.jdbcProperties(properties); + JdbcSinkConfig jdbcSinkConfig = builder.build(); + + // Discover corresponding factory + String dialect = jdbcSinkConfig.getDialect(); + JdbcSinkDialectFactory<JdbcSinkConfig> dialectFactory = Review Comment: Exception will be thrown if `.findFirst()` returns empty by calling `.orElseThrow`. -- 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. To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org