EricJoy2048 commented on code in PR #6975:
URL: https://github.com/apache/seatunnel/pull/6975#discussion_r1637599917


##########
seatunnel-connectors-v2/connector-clickhouse/src/main/java/org/apache/seatunnel/connectors/seatunnel/clickhouse/sink/file/ClickhouseFileSinkFactory.java:
##########
@@ -44,6 +67,72 @@ public String factoryIdentifier() {
         return "ClickhouseFile";
     }
 
+    @Override
+    public TableSink<SeaTunnelRow, ClickhouseSinkState, CKFileCommitInfo, 
CKFileAggCommitInfo>
+            createSink(TableSinkFactoryContext context) {
+        ReadonlyConfig readonlyConfig = context.getOptions();
+        List<ClickHouseNode> nodes = 
ClickhouseUtil.createNodes(readonlyConfig);
+
+        ClickhouseProxy proxy = new ClickhouseProxy(nodes.get(0));
+        Map<String, String> tableSchema = 
proxy.getClickhouseTableSchema(readonlyConfig.get(TABLE));
+        ClickhouseTable table =
+                proxy.getClickhouseTable(readonlyConfig.get(DATABASE), 
readonlyConfig.get(TABLE));
+        String shardKey = null;
+        String shardKeyType = null;
+        if (readonlyConfig.getOptional(SHARDING_KEY).isPresent()) {
+            shardKey = readonlyConfig.get(SHARDING_KEY);
+            shardKeyType = tableSchema.get(shardKey);
+        }
+        ShardMetadata shardMetadata =
+                new ShardMetadata(
+                        shardKey,
+                        shardKeyType,
+                        readonlyConfig.get(DATABASE),
+                        readonlyConfig.get(TABLE),
+                        table.getEngine(),
+                        true,
+                        new Shard(1, 1, nodes.get(0)),
+                        readonlyConfig.get(USERNAME),
+                        readonlyConfig.get(PASSWORD));
+        List<String> fields = new ArrayList<>(tableSchema.keySet());
+        Map<String, String> nodeUser =
+                readonlyConfig.get(NODE_PASS).stream()
+                        .collect(
+                                Collectors.toMap(
+                                        configObject -> 
configObject.getNodeAddress(),
+                                        configObject -> 
readonlyConfig.get(USERNAME)));
+        Map<String, String> nodePassword =
+                readonlyConfig.get(NODE_PASS).stream()
+                        .collect(
+                                Collectors.toMap(
+                                        NodePassConfig::getNodeAddress,
+                                        NodePassConfig::getPassword));
+
+        proxy.close();

Review Comment:
   same as above.



##########
seatunnel-connectors-v2/connector-clickhouse/src/main/java/org/apache/seatunnel/connectors/seatunnel/clickhouse/sink/client/ClickhouseSinkFactory.java:
##########
@@ -0,0 +1,158 @@
+/*
+ * 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.seatunnel.connectors.seatunnel.clickhouse.sink.client;
+
+import org.apache.seatunnel.api.configuration.ReadonlyConfig;
+import org.apache.seatunnel.api.configuration.util.OptionRule;
+import org.apache.seatunnel.api.table.catalog.CatalogTable;
+import org.apache.seatunnel.api.table.connector.TableSink;
+import org.apache.seatunnel.api.table.factory.Factory;
+import org.apache.seatunnel.api.table.factory.TableSinkFactory;
+import org.apache.seatunnel.api.table.factory.TableSinkFactoryContext;
+import org.apache.seatunnel.api.table.type.SeaTunnelRow;
+import org.apache.seatunnel.common.exception.CommonErrorCodeDeprecated;
+import 
org.apache.seatunnel.connectors.seatunnel.clickhouse.config.ReaderOption;
+import 
org.apache.seatunnel.connectors.seatunnel.clickhouse.exception.ClickhouseConnectorException;
+import org.apache.seatunnel.connectors.seatunnel.clickhouse.shard.Shard;
+import 
org.apache.seatunnel.connectors.seatunnel.clickhouse.shard.ShardMetadata;
+import 
org.apache.seatunnel.connectors.seatunnel.clickhouse.state.CKAggCommitInfo;
+import org.apache.seatunnel.connectors.seatunnel.clickhouse.state.CKCommitInfo;
+import 
org.apache.seatunnel.connectors.seatunnel.clickhouse.state.ClickhouseSinkState;
+import 
org.apache.seatunnel.connectors.seatunnel.clickhouse.util.ClickhouseProxy;
+import 
org.apache.seatunnel.connectors.seatunnel.clickhouse.util.ClickhouseTable;
+import 
org.apache.seatunnel.connectors.seatunnel.clickhouse.util.ClickhouseUtil;
+
+import com.clickhouse.client.ClickHouseNode;
+import com.google.auto.service.AutoService;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Properties;
+
+import static 
org.apache.seatunnel.connectors.seatunnel.clickhouse.config.ClickhouseConfig.ALLOW_EXPERIMENTAL_LIGHTWEIGHT_DELETE;
+import static 
org.apache.seatunnel.connectors.seatunnel.clickhouse.config.ClickhouseConfig.BULK_SIZE;
+import static 
org.apache.seatunnel.connectors.seatunnel.clickhouse.config.ClickhouseConfig.CLICKHOUSE_CONFIG;
+import static 
org.apache.seatunnel.connectors.seatunnel.clickhouse.config.ClickhouseConfig.DATABASE;
+import static 
org.apache.seatunnel.connectors.seatunnel.clickhouse.config.ClickhouseConfig.HOST;
+import static 
org.apache.seatunnel.connectors.seatunnel.clickhouse.config.ClickhouseConfig.PASSWORD;
+import static 
org.apache.seatunnel.connectors.seatunnel.clickhouse.config.ClickhouseConfig.PRIMARY_KEY;
+import static 
org.apache.seatunnel.connectors.seatunnel.clickhouse.config.ClickhouseConfig.SHARDING_KEY;
+import static 
org.apache.seatunnel.connectors.seatunnel.clickhouse.config.ClickhouseConfig.SPLIT_MODE;
+import static 
org.apache.seatunnel.connectors.seatunnel.clickhouse.config.ClickhouseConfig.SUPPORT_UPSERT;
+import static 
org.apache.seatunnel.connectors.seatunnel.clickhouse.config.ClickhouseConfig.TABLE;
+import static 
org.apache.seatunnel.connectors.seatunnel.clickhouse.config.ClickhouseConfig.USERNAME;
+
+@AutoService(Factory.class)
+public class ClickhouseSinkFactory implements TableSinkFactory {
+
+    @Override
+    public TableSink<SeaTunnelRow, ClickhouseSinkState, CKCommitInfo, 
CKAggCommitInfo> createSink(
+            TableSinkFactoryContext context) {
+        ReadonlyConfig readonlyConfig = context.getOptions();
+        CatalogTable catalogTable = context.getCatalogTable();
+        List<ClickHouseNode> nodes = 
ClickhouseUtil.createNodes(readonlyConfig);
+        Properties clickhouseProperties = new Properties();
+        readonlyConfig
+                .get(CLICKHOUSE_CONFIG)
+                .forEach((key, value) -> clickhouseProperties.put(key, 
String.valueOf(value)));
+
+        clickhouseProperties.put("user", readonlyConfig.get(USERNAME));
+        clickhouseProperties.put("password", readonlyConfig.get(PASSWORD));
+
+        ClickhouseProxy proxy = new ClickhouseProxy(nodes.get(0));
+        Map<String, String> tableSchema = 
proxy.getClickhouseTableSchema(readonlyConfig.get(TABLE));
+        String shardKey = null;
+        String shardKeyType = null;
+        ClickhouseTable table =
+                proxy.getClickhouseTable(readonlyConfig.get(DATABASE), 
readonlyConfig.get(TABLE));
+        if (readonlyConfig.get(SPLIT_MODE)) {
+            if (!"Distributed".equals(table.getEngine())) {
+                throw new ClickhouseConnectorException(
+                        CommonErrorCodeDeprecated.ILLEGAL_ARGUMENT,
+                        "split mode only support table which engine is "
+                                + "'Distributed' engine at now");
+            }
+            if (readonlyConfig.getOptional(SHARDING_KEY).isPresent()) {
+                shardKey = readonlyConfig.get(SHARDING_KEY);
+                shardKeyType = tableSchema.get(shardKey);
+            }
+        }
+        ShardMetadata metadata =
+                new ShardMetadata(
+                        shardKey,
+                        shardKeyType,
+                        table.getSortingKey(),
+                        readonlyConfig.get(DATABASE),
+                        readonlyConfig.get(TABLE),
+                        table.getEngine(),
+                        readonlyConfig.get(SPLIT_MODE),
+                        new Shard(1, 1, nodes.get(0)),
+                        readonlyConfig.get(USERNAME),
+                        readonlyConfig.get(PASSWORD));
+        proxy.close();

Review Comment:
   add `try.. catch and finally` to close `proxy`?



-- 
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: commits-unsubscr...@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to