You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by GitBox <gi...@apache.org> on 2022/10/25 03:10:31 UTC

[GitHub] [incubator-seatunnel] Hisoka-X commented on a diff in pull request #2832: [Feature][Connector-V2] add tdengine source

Hisoka-X commented on code in PR #2832:
URL: https://github.com/apache/incubator-seatunnel/pull/2832#discussion_r1003954814


##########
seatunnel-connectors-v2/connector-tdengine/src/main/java/org/apache/seatunnel/connectors/seatunnel/tdengine/sink/TDengineSinkWriter.java:
##########
@@ -0,0 +1,116 @@
+/*
+ * 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.tdengine.sink;
+
+import org.apache.seatunnel.api.table.type.SeaTunnelRow;
+import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
+import org.apache.seatunnel.connectors.seatunnel.common.sink.AbstractSinkWriter;
+import org.apache.seatunnel.connectors.seatunnel.tdengine.config.TDengineSourceConfig;
+
+import org.apache.seatunnel.shade.com.typesafe.config.Config;
+
+import com.google.common.base.Throwables;
+import com.google.common.collect.Lists;
+import com.taosdata.jdbc.TSDBDriver;
+import lombok.SneakyThrows;
+import org.apache.commons.lang3.ArrayUtils;
+import org.apache.commons.lang3.StringUtils;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Objects;
+import java.util.Properties;
+
+public class TDengineSinkWriter extends AbstractSinkWriter<SeaTunnelRow, Void> {
+
+    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
+    private final SeaTunnelRowType seaTunnelRowType;
+    private final Connection conn;
+    private final TDengineSourceConfig config;
+    private int tagsNum;
+
+    @SneakyThrows
+    public TDengineSinkWriter(Config pluginConfig, SeaTunnelRowType seaTunnelRowType) {
+        this.seaTunnelRowType = seaTunnelRowType;
+        this.tagsNum = pluginConfig.hasPath("tags") ? pluginConfig.getObject("tags").size() : 0;
+
+        config = TDengineSourceConfig.buildSourceConfig(pluginConfig);
+        String jdbcUrl = StringUtils.join(config.getUrl(), config.getDatabase(), "?user=", config.getUsername(), "&password=", config.getPassword());
+        Properties connProps = new Properties();
+        connProps.setProperty(TSDBDriver.PROPERTY_KEY_BATCH_LOAD, "true");
+        conn = DriverManager.getConnection(jdbcUrl, connProps);
+    }
+
+    @SneakyThrows
+    @Override
+    @SuppressWarnings("checkstyle:RegexpSingleline")
+    public void write(SeaTunnelRow element) {
+        final ArrayList<Object> tags = Lists.newArrayList();
+        for (int i = element.getArity() - tagsNum; i < element.getArity(); i++) {
+            tags.add(element.getField(i));
+        }
+        final String tagValues = StringUtils.join(convertDataType(tags.toArray()), ",");
+
+        final Object[] metrics = ArrayUtils.subarray(element.getFields(), 1, element.getArity() - tagsNum);
+
+        try (Statement statement = conn.createStatement()) {

Review Comment:
   ```suggestion
           try (Statement statement = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) {
   ```
   Use these parameter to save memory.



-- 
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-unsubscribe@seatunnel.apache.org

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