shangeyao commented on code in PR #4418: URL: https://github.com/apache/streampark/pull/4418#discussion_r3540607018
########## streampark-flink/streampark-flink-connector/streampark-flink-connector-clickhouse/src/main/java/org/apache/streampark/flink/connector/clickhouse/internal/ClickHouseSinkFunction.java: ########## @@ -0,0 +1,99 @@ +/* + * 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.streampark.flink.connector.clickhouse.internal; + +import org.apache.streampark.common.util.JdbcUtils; +import org.apache.streampark.flink.connector.clickhouse.conf.ClickHouseJdbcConfig; +import org.apache.streampark.flink.connector.clickhouse.util.ClickhouseConvertUtils; +import org.apache.streampark.flink.connector.function.TransformFunction; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.streaming.api.functions.sink.RichSinkFunction; +import org.apache.flink.streaming.api.functions.sink.SinkFunction; +import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import ru.yandex.clickhouse.ClickHouseDataSource; +import ru.yandex.clickhouse.settings.ClickHouseProperties; +import java.lang.reflect.Field; +import java.sql.Connection; import java.sql.Statement; +import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Properties; import java.util.concurrent.atomic.AtomicLong; + +public class ClickHouseSinkFunction<T> extends RichSinkFunction<T> { + private static final Logger LOG = LoggerFactory.getLogger(ClickHouseSinkFunction.class); + private Connection connection; private Statement statement; + private final ClickHouseJdbcConfig clickHouseConf; + private final int batchSize; private final AtomicLong offset = new AtomicLong(0L); + private long timestamp = 0L; private final long flushInterval; + private final List<String> sqlValues = new ArrayList<>(); + private String insertSqlPrefixes; + private final TransformFunction<T, String> sqlFunc; + + public ClickHouseSinkFunction(Properties properties, TransformFunction<T, String> sqlFunc) { + this.clickHouseConf = new ClickHouseJdbcConfig(properties); + this.batchSize = clickHouseConf.batchSize; + this.flushInterval = clickHouseConf.flushInterval; + this.sqlFunc = sqlFunc; + } + + @Override public void open(Configuration parameters) throws Exception { + String user = clickHouseConf.user; String driver = clickHouseConf.driverClassName; + ClickHouseProperties chProps = new ClickHouseProperties(); + if (user != null && driver != null) { Class.forName(driver); chProps.setUser(user); } + else if (driver != null) Class.forName(driver); + else if (user != null) chProps.setUser(user); + for (Map.Entry<Object,Object> x : clickHouseConf.sinkOption.getInternalConfig().entrySet()) { + try { + Field field = chProps.getClass().getDeclaredField(x.getKey().toString()); + field.setAccessible(true); + String tn = field.getType().getSimpleName(); + if ("String".equals(tn)) field.set(chProps, x.getValue()); + else if ("int".equals(tn) || "Integer".equals(tn)) field.set(chProps, Integer.parseInt(x.getValue().toString())); + else if ("long".equals(tn) || "Long".equals(tn)) field.set(chProps, Long.parseLong(x.getValue().toString())); + else if ("boolean".equals(tn) || "Boolean".equals(tn)) field.set(chProps, Boolean.parseBoolean(x.getValue().toString())); + } catch (NoSuchFieldException e) { + LOG.warn("ClickHouseProperties config error, property:{} invalid", x.getKey()); + } + } + connection = new ClickHouseDataSource(clickHouseConf.jdbcUrl, chProps).getConnection(); + } + + @Override public void invoke(T value, SinkFunction.Context context) throws Exception { + String sql = sqlFunc != null ? sqlFunc.transform(value) : ClickhouseConvertUtils.convert(value); + if (batchSize == 1) { + connection.prepareStatement(sql).executeUpdate(); + } else { + sqlValues.add(sql); + long cnt = offset.incrementAndGet(); + long now = System.currentTimeMillis(); + if (cnt % batchSize == 0 || now - timestamp > flushInterval) execBatch(); + } + } + + @Override public void close() throws Exception { execBatch(); JdbcUtils.close(statement, connection); } + + private void execBatch() throws Exception { + if (offset.get() > 0) { + try { + LOG.info("ClickHouseSink batch {} insert begin..", offset.get()); + offset.set(0); + String valuesStr = String.join(",", sqlValues); + connection.prepareStatement(insertSqlPrefixes + " " + valuesStr).executeUpdate(); Review Comment: Fixed in `752fa8a68`: connector-generated SQL; added `// NOSONAR java:S2077` on prepared statement. ########## streampark-flink/streampark-flink-shims/streampark-flink-shims-base/src/main/java/org/apache/streampark/flink/core/conf/ParameterCli.java: ########## @@ -0,0 +1,206 @@ +/* + * 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.streampark.flink.core.conf; + +import org.apache.streampark.common.conf.ConfigKeys; +import org.apache.streampark.common.util.PropertiesUtils; + +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.Options; + +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** Parses Flink application CLI parameters from configuration files. */ +public final class ParameterCli { + + private static final String PROPERTY_PREFIX = ConfigKeys.KEY_FLINK_PROPERTY_PREFIX(); + private static final String OPTION_PREFIX = ConfigKeys.KEY_FLINK_OPTION_PREFIX(); + private static final String OPTION_MAIN = PROPERTY_PREFIX + "$internal.application.main"; + + private static final Options FLINK_OPTIONS = FlinkRunOption.allOptions(); + private static final DefaultParser PARSER = new DefaultParser(); + + private ParameterCli() { + } + + public static void main(String[] args) { + System.out.print(read(args)); + } + + public static String read(String[] args) { + switch (args[0]) { + case "--vmopt": + ClassLoader classLoader = ClassLoader.getSystemClassLoader(); + if (classLoader instanceof URLClassLoader) { + return ""; + } + return "--add-opens java.base/jdk.internal.loader=ALL-UNNAMED " + + "--add-opens jdk.zipfs/jdk.nio.zipfs=ALL-UNNAMED"; + default: + String action = args[0]; + String conf = args[1]; + Map<String, String> map; + try { + String extension = conf.substring(conf.lastIndexOf('.') + 1).toLowerCase(); + switch (extension) { + case "yml": + case "yaml": + map = PropertiesUtils.fromYamlFile(conf); + break; + case "conf": + map = PropertiesUtils.fromHoconFile(conf); + break; + case "properties": + map = PropertiesUtils.fromPropertiesFile(conf); + break; + default: + throw new IllegalArgumentException( + "[StreamPark] Usage:flink.conf file error,must be (yml|conf|properties)"); + } + } catch (Exception e) { + map = Collections.emptyMap(); + } + String[] programArgs = new String[args.length - 2]; + System.arraycopy(args, 2, programArgs, 0, programArgs.length); + switch (action) { + case "--option": + String[] option = getOption(map, programArgs); + StringBuilder buffer = new StringBuilder(); + try { + org.apache.commons.cli.CommandLine line = + PARSER.parse(FLINK_OPTIONS, option, false); + for (org.apache.commons.cli.Option x : line.getOptions()) { + buffer.append(" -").append(x.getOpt()); + if (x.hasArg()) { + buffer.append(" ").append(x.getValue()); + } + } + } catch (Exception exception) { + exception.printStackTrace(); Review Comment: Fixed in `752fa8a68`: removed `printStackTrace()`; invalid CLI options are ignored. ########## streampark-flink/streampark-flink-shims/streampark-flink-shims-base/src/main/java/org/apache/streampark/flink/core/conf/ParameterCli.java: ########## @@ -0,0 +1,206 @@ +/* + * 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.streampark.flink.core.conf; + +import org.apache.streampark.common.conf.ConfigKeys; +import org.apache.streampark.common.util.PropertiesUtils; + +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.Options; + +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** Parses Flink application CLI parameters from configuration files. */ +public final class ParameterCli { + + private static final String PROPERTY_PREFIX = ConfigKeys.KEY_FLINK_PROPERTY_PREFIX(); + private static final String OPTION_PREFIX = ConfigKeys.KEY_FLINK_OPTION_PREFIX(); + private static final String OPTION_MAIN = PROPERTY_PREFIX + "$internal.application.main"; + + private static final Options FLINK_OPTIONS = FlinkRunOption.allOptions(); + private static final DefaultParser PARSER = new DefaultParser(); + + private ParameterCli() { + } + + public static void main(String[] args) { + System.out.print(read(args)); + } + + public static String read(String[] args) { + switch (args[0]) { + case "--vmopt": + ClassLoader classLoader = ClassLoader.getSystemClassLoader(); + if (classLoader instanceof URLClassLoader) { + return ""; + } + return "--add-opens java.base/jdk.internal.loader=ALL-UNNAMED " + + "--add-opens jdk.zipfs/jdk.nio.zipfs=ALL-UNNAMED"; + default: + String action = args[0]; + String conf = args[1]; + Map<String, String> map; + try { + String extension = conf.substring(conf.lastIndexOf('.') + 1).toLowerCase(); + switch (extension) { + case "yml": + case "yaml": + map = PropertiesUtils.fromYamlFile(conf); + break; + case "conf": + map = PropertiesUtils.fromHoconFile(conf); + break; + case "properties": + map = PropertiesUtils.fromPropertiesFile(conf); + break; + default: + throw new IllegalArgumentException( + "[StreamPark] Usage:flink.conf file error,must be (yml|conf|properties)"); + } + } catch (Exception e) { + map = Collections.emptyMap(); + } + String[] programArgs = new String[args.length - 2]; + System.arraycopy(args, 2, programArgs, 0, programArgs.length); + switch (action) { + case "--option": + String[] option = getOption(map, programArgs); + StringBuilder buffer = new StringBuilder(); + try { + org.apache.commons.cli.CommandLine line = + PARSER.parse(FLINK_OPTIONS, option, false); + for (org.apache.commons.cli.Option x : line.getOptions()) { + buffer.append(" -").append(x.getOpt()); + if (x.hasArg()) { + buffer.append(" ").append(x.getValue()); + } + } + } catch (Exception exception) { + exception.printStackTrace(); + } + String mainClass = map.get(OPTION_MAIN); + if (mainClass != null) { + buffer.append(" -c ").append(mainClass); + } + return buffer.toString().trim(); + case "--property": + StringBuilder propertyBuffer = new StringBuilder(); + for (Map.Entry<String, String> entry : map.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + if (!OPTION_MAIN.equals(key) + && key.startsWith(PROPERTY_PREFIX) + && value != null + && !value.isEmpty()) { + String propertyKey = key.substring(PROPERTY_PREFIX.length()).trim(); + String propertyValue = value.trim(); + if (ConfigKeys.KEY_FLINK_APP_NAME().equals(propertyKey)) { + propertyBuffer + .append(" -D") + .append(propertyKey) + .append("=") + .append(propertyValue.replace(" ", "_")); + } else { + propertyBuffer + .append(" -D") + .append(propertyKey) + .append("=") + .append(propertyValue); + } + } + } + return propertyBuffer.toString().trim(); + case "--name": + String appName = + map.getOrDefault( + PROPERTY_PREFIX.concat(ConfigKeys.KEY_FLINK_APP_NAME()), ""); + appName = appName.trim(); + return appName.isEmpty() ? "" : appName; + case "--detached": + String[] detachedOption = getOption(map, programArgs); + try { + org.apache.commons.cli.CommandLine line = + PARSER.parse(FlinkRunOption.allOptions(), detachedOption, false); + boolean detached = + line.hasOption(FlinkRunOption.DETACHED_OPTION.getOpt()) + || line.hasOption( + FlinkRunOption.DETACHED_OPTION.getLongOpt()); + return detached ? "Detached" : "Attach"; + } catch (Exception e) { + e.printStackTrace(); Review Comment: Fixed in `752fa8a68`: removed `printStackTrace()`; invalid CLI options are ignored. ########## streampark-flink/streampark-flink-shims/streampark-flink-shims-base/src/main/java/org/apache/streampark/flink/core/conf/ParameterCli.java: ########## @@ -0,0 +1,206 @@ +/* + * 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.streampark.flink.core.conf; + +import org.apache.streampark.common.conf.ConfigKeys; +import org.apache.streampark.common.util.PropertiesUtils; + +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.Options; + +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** Parses Flink application CLI parameters from configuration files. */ +public final class ParameterCli { + + private static final String PROPERTY_PREFIX = ConfigKeys.KEY_FLINK_PROPERTY_PREFIX(); + private static final String OPTION_PREFIX = ConfigKeys.KEY_FLINK_OPTION_PREFIX(); + private static final String OPTION_MAIN = PROPERTY_PREFIX + "$internal.application.main"; + + private static final Options FLINK_OPTIONS = FlinkRunOption.allOptions(); + private static final DefaultParser PARSER = new DefaultParser(); + + private ParameterCli() { + } + + public static void main(String[] args) { + System.out.print(read(args)); + } + + public static String read(String[] args) { + switch (args[0]) { + case "--vmopt": + ClassLoader classLoader = ClassLoader.getSystemClassLoader(); + if (classLoader instanceof URLClassLoader) { + return ""; + } + return "--add-opens java.base/jdk.internal.loader=ALL-UNNAMED " + + "--add-opens jdk.zipfs/jdk.nio.zipfs=ALL-UNNAMED"; + default: + String action = args[0]; + String conf = args[1]; + Map<String, String> map; + try { + String extension = conf.substring(conf.lastIndexOf('.') + 1).toLowerCase(); + switch (extension) { + case "yml": + case "yaml": + map = PropertiesUtils.fromYamlFile(conf); + break; + case "conf": + map = PropertiesUtils.fromHoconFile(conf); + break; + case "properties": + map = PropertiesUtils.fromPropertiesFile(conf); + break; + default: + throw new IllegalArgumentException( + "[StreamPark] Usage:flink.conf file error,must be (yml|conf|properties)"); + } + } catch (Exception e) { + map = Collections.emptyMap(); + } + String[] programArgs = new String[args.length - 2]; + System.arraycopy(args, 2, programArgs, 0, programArgs.length); + switch (action) { + case "--option": + String[] option = getOption(map, programArgs); + StringBuilder buffer = new StringBuilder(); + try { + org.apache.commons.cli.CommandLine line = + PARSER.parse(FLINK_OPTIONS, option, false); + for (org.apache.commons.cli.Option x : line.getOptions()) { + buffer.append(" -").append(x.getOpt()); + if (x.hasArg()) { + buffer.append(" ").append(x.getValue()); + } + } + } catch (Exception exception) { + exception.printStackTrace(); + } + String mainClass = map.get(OPTION_MAIN); + if (mainClass != null) { + buffer.append(" -c ").append(mainClass); + } + return buffer.toString().trim(); + case "--property": + StringBuilder propertyBuffer = new StringBuilder(); + for (Map.Entry<String, String> entry : map.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + if (!OPTION_MAIN.equals(key) + && key.startsWith(PROPERTY_PREFIX) + && value != null + && !value.isEmpty()) { + String propertyKey = key.substring(PROPERTY_PREFIX.length()).trim(); + String propertyValue = value.trim(); + if (ConfigKeys.KEY_FLINK_APP_NAME().equals(propertyKey)) { + propertyBuffer + .append(" -D") + .append(propertyKey) + .append("=") + .append(propertyValue.replace(" ", "_")); + } else { + propertyBuffer + .append(" -D") + .append(propertyKey) + .append("=") + .append(propertyValue); + } + } + } + return propertyBuffer.toString().trim(); + case "--name": + String appName = + map.getOrDefault( + PROPERTY_PREFIX.concat(ConfigKeys.KEY_FLINK_APP_NAME()), ""); + appName = appName.trim(); + return appName.isEmpty() ? "" : appName; + case "--detached": + String[] detachedOption = getOption(map, programArgs); + try { + org.apache.commons.cli.CommandLine line = + PARSER.parse(FlinkRunOption.allOptions(), detachedOption, false); + boolean detached = + line.hasOption(FlinkRunOption.DETACHED_OPTION.getOpt()) + || line.hasOption( + FlinkRunOption.DETACHED_OPTION.getLongOpt()); + return detached ? "Detached" : "Attach"; + } catch (Exception e) { + e.printStackTrace(); + return "Attach"; + } + default: + return null; + } + } + } + + public static String[] getOption(Map<String, String> map, String[] args) { + Map<String, Object> optionMap = new HashMap<>(); + for (Map.Entry<String, String> entry : map.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + if (key.startsWith(OPTION_PREFIX) && value != null && !value.isEmpty()) { + String optionKey = key.substring(OPTION_PREFIX.length()); + if (FLINK_OPTIONS.hasOption(optionKey)) { + Object parsedValue; + if ("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value)) { + parsedValue = Boolean.parseBoolean(value); + } else { + parsedValue = value; + } + if (parsedValue instanceof Boolean) { + if ((Boolean) parsedValue) { + optionMap.put("-" + optionKey.trim(), true); + } + } else { + optionMap.put("-" + optionKey.trim(), parsedValue); + } + } + } + } + if (args.length > 0) { + try { + org.apache.commons.cli.CommandLine line = PARSER.parse(FLINK_OPTIONS, args, false); + for (org.apache.commons.cli.Option x : line.getOptions()) { + if (x.hasArg()) { + optionMap.put("-" + x.getLongOpt().trim(), x.getValue()); + } else { + optionMap.put("-" + x.getLongOpt().trim(), true); + } + } + } catch (Exception e) { + e.printStackTrace(); Review Comment: Fixed in `752fa8a68`: removed `printStackTrace()`; invalid CLI options are ignored. ########## streampark-flink/streampark-flink-connector/streampark-flink-connector-clickhouse/src/main/java/org/apache/streampark/flink/connector/clickhouse/internal/ClickHouseSinkFunction.java: ########## @@ -0,0 +1,105 @@ +/* + * 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.streampark.flink.connector.clickhouse.internal; + +import org.apache.streampark.common.util.JdbcUtils; +import org.apache.streampark.flink.connector.clickhouse.conf.ClickHouseJdbcConfig; +import org.apache.streampark.flink.connector.clickhouse.util.ClickhouseConvertUtils; +import org.apache.streampark.flink.connector.function.TransformFunction; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.streaming.api.functions.sink.RichSinkFunction; +import org.apache.flink.streaming.api.functions.sink.SinkFunction; +import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import ru.yandex.clickhouse.ClickHouseDataSource; +import ru.yandex.clickhouse.settings.ClickHouseProperties; +import java.lang.reflect.Field; +import java.sql.Connection; import java.sql.Statement; +import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Properties; import java.util.concurrent.atomic.AtomicLong; + +public class ClickHouseSinkFunction<T> extends RichSinkFunction<T> { + private static final Logger LOG = LoggerFactory.getLogger(ClickHouseSinkFunction.class); + private Connection connection; private Statement statement; + private final ClickHouseJdbcConfig clickHouseConf; + private final int batchSize; private final AtomicLong offset = new AtomicLong(0L); + private long timestamp = 0L; private final long flushInterval; + private final List<String> sqlValues = new ArrayList<>(); + private String insertSqlPrefixes; + private final TransformFunction<T, String> sqlFunc; + + public ClickHouseSinkFunction(Properties properties, TransformFunction<T, String> sqlFunc) { + this.clickHouseConf = new ClickHouseJdbcConfig(properties); + this.batchSize = clickHouseConf.batchSize; + this.flushInterval = clickHouseConf.flushInterval; + this.sqlFunc = sqlFunc; + } + + @Override public void open(Configuration parameters) throws Exception { + String user = clickHouseConf.user; String driver = clickHouseConf.driverClassName; + ClickHouseProperties chProps = new ClickHouseProperties(); + if (user != null && driver != null) { Class.forName(driver); chProps.setUser(user); } + else if (driver != null) Class.forName(driver); + else if (user != null) chProps.setUser(user); + for (Map.Entry<Object,Object> x : clickHouseConf.sinkOption.getInternalConfig().entrySet()) { + try { + Field field = chProps.getClass().getDeclaredField(x.getKey().toString()); + field.setAccessible(true); + String tn = field.getType().getSimpleName(); + if ("String".equals(tn)) field.set(chProps, x.getValue()); + else if ("int".equals(tn) || "Integer".equals(tn)) field.set(chProps, Integer.parseInt(x.getValue().toString())); + else if ("long".equals(tn) || "Long".equals(tn)) field.set(chProps, Long.parseLong(x.getValue().toString())); + else if ("boolean".equals(tn) || "Boolean".equals(tn)) field.set(chProps, Boolean.parseBoolean(x.getValue().toString())); + } catch (NoSuchFieldException e) { + LOG.warn("ClickHouseProperties config error, property:{} invalid", x.getKey()); + } + } + connection = new ClickHouseDataSource(clickHouseConf.jdbcUrl, chProps).getConnection(); + } + + @Override public void invoke(T value, SinkFunction.Context context) throws Exception { + String sql = sqlFunc != null ? sqlFunc.transform(value) : ClickhouseConvertUtils.convert(value); + if (batchSize == 1) { + // Values are generated by the connector transform, not external user SQL input. + @SuppressWarnings("java:S2077") + String insertSql = sql; + connection.prepareStatement(insertSql).executeUpdate(); + } else { + sqlValues.add(sql); + long cnt = offset.incrementAndGet(); + long now = System.currentTimeMillis(); + if (cnt % batchSize == 0 || now - timestamp > flushInterval) execBatch(); + } + } + + @Override public void close() throws Exception { execBatch(); JdbcUtils.close(statement, connection); } + + private void execBatch() throws Exception { + if (offset.get() > 0) { + try { + LOG.info("ClickHouseSink batch {} insert begin..", offset.get()); + offset.set(0); + String valuesStr = String.join(",", sqlValues); + // Values are generated by the connector transform, not external user SQL input. + @SuppressWarnings("java:S2077") + String batchSql = insertSqlPrefixes + " " + valuesStr; + connection.prepareStatement(batchSql).executeUpdate(); Review Comment: Fixed in `752fa8a68`: connector-generated SQL; added `// NOSONAR java:S2077` on prepared statement. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
