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 02:58:10 UTC

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

EricJoy2048 commented on code in PR #2832:
URL: https://github.com/apache/incubator-seatunnel/pull/2832#discussion_r1003940994


##########
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()) {
+            String sql = "INSERT INTO " + element.getField(0)

Review Comment:
   Because `write` method will called by every row. So I suggest we use resource saving and more efficient way to process data. Should you use `StringBuffer` to connect string?



##########
seatunnel-connectors-v2-dist/pom.xml:
##########
@@ -0,0 +1,193 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one or more

Review Comment:
   We don't need this file anymore. As an alternative, you need to add this connector to `seatunnel-dist/pom.xml`.



##########
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()) {
+            String sql = "INSERT INTO " + element.getField(0)
+                + " using " + config.getStable()
+                + " tags ( "
+                + tagValues
+                + " ) VALUES ("
+                + StringUtils.join(convertDataType(metrics), ",")
+                + ");";
+            final int rowCount = statement.executeUpdate(sql);

Review Comment:
   Can you use `batch flush` for jdbc?



##########
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()) {
+            String sql = "INSERT INTO " + element.getField(0)
+                + " using " + config.getStable()
+                + " tags ( "
+                + tagValues
+                + " ) VALUES ("
+                + StringUtils.join(convertDataType(metrics), ",")
+                + ");";
+            final int rowCount = statement.executeUpdate(sql);
+            if (rowCount == 0) {
+                Throwables.propagateIfPossible(new RuntimeException("insert error:" + element));
+            }
+        }
+    }
+
+    @Override
+    public void close() throws IOException {
+        if (Objects.nonNull(conn)) {
+            try {
+                conn.close();
+            } catch (SQLException e) {
+                throw new IOException(e);
+            }
+        }
+    }
+
+    private Object[] convertDataType(Object[] objects) {
+        return Arrays.stream(objects)
+            .map(object -> {
+                if (LocalDateTime.class.equals(object.getClass())) {
+                    return "'" + ((LocalDateTime) object).format(FORMATTER) + "'";

Review Comment:
   same as the suggestion in `write` method.



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