You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by GitBox <gi...@apache.org> on 2021/04/27 12:53:14 UTC

[GitHub] [iotdb] qiaojialin commented on a change in pull request #3049: add rabbitmq example module.

qiaojialin commented on a change in pull request #3049:
URL: https://github.com/apache/iotdb/pull/3049#discussion_r621178205



##########
File path: example/rabbitmq/src/main/java/org/apache/iotdb/rabbitmq/RabbitMQConsumerThread.java
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.iotdb.rabbitmq;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+public class RabbitMQConsumerThread implements Runnable {
+
+  private Connection connection = null;
+  private Statement statement = null;
+  private String message;
+  private static boolean setStorageGroup = true;
+  private static boolean createTimeSeries = true;
+  private String createStorageGroupSqlTemplate = "SET STORAGE GROUP TO %s";
+  private String createTimeseriesSqlTemplate =
+      "CREATE TIMESERIES %s WITH DATATYPE=TEXT, ENCODING=PLAIN";
+  private String insertDataSqlTemplate =
+      "INSERT INTO root.vehicle.deviceid(timestamp,%s) VALUES (%s,'%s')";
+  private static final Logger logger = LoggerFactory.getLogger(RabbitMQConsumerThread.class);
+
+  public RabbitMQConsumerThread(String message) {
+    this.message = message;
+    /** Establish JDBC connection of IoTDB */
+    initIoTDB();
+  }
+
+  private void initIoTDB() {
+    try {
+      Class.forName("org.apache.iotdb.jdbc.IoTDBDriver");
+      connection =
+          DriverManager.getConnection(
+              Constant.IOTDB_CONNECTION_URL,
+              Constant.IOTDB_CONNECTION_USER,
+              Constant.IOTDB_CONNECTION_PASSWORD);
+      statement = connection.createStatement();
+      if (setStorageGroup) {
+        try {
+          statement.execute(String.format(createStorageGroupSqlTemplate, Constant.STORAGE_GROUP));
+        } catch (SQLException e) {
+        }
+        setStorageGroup = false;
+      }
+      if (createTimeSeries) {
+        for (String timeseries : Constant.ALL_TIMESERIES) {
+          statement.addBatch(String.format(createTimeseriesSqlTemplate, timeseries));
+        }
+        statement.executeBatch();
+        statement.clearBatch();
+        createTimeSeries = false;
+      }
+    } catch (ClassNotFoundException | SQLException e) {
+      logger.error(e.getMessage());
+    }
+  }
+
+  @Override
+  public void run() {
+    String[] items = message.split(",");
+    try {
+      String sql = String.format(insertDataSqlTemplate, items[0], items[1], items[2]);
+      statement.execute(sql);

Review comment:
       It's better to use Session to insertRecords

##########
File path: example/rabbitmq/pom.xml
##########
@@ -0,0 +1,49 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <!-- you need to add the following content if you remove the parent pom.
+  <groupId>org.apache.iotdb.example</groupId>
+  <artifactId>kafka-example</artifactId>
+  <version>0.10.0</version>
+  -->

Review comment:
       please remove this

##########
File path: example/rabbitmq/src/main/java/org/apache/iotdb/rabbitmq/RabbitMQConsumerThread.java
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.iotdb.rabbitmq;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+public class RabbitMQConsumerThread implements Runnable {
+
+  private Connection connection = null;
+  private Statement statement = null;
+  private String message;
+  private static boolean setStorageGroup = true;
+  private static boolean createTimeSeries = true;
+  private String createStorageGroupSqlTemplate = "SET STORAGE GROUP TO %s";
+  private String createTimeseriesSqlTemplate =
+      "CREATE TIMESERIES %s WITH DATATYPE=TEXT, ENCODING=PLAIN";
+  private String insertDataSqlTemplate =
+      "INSERT INTO root.vehicle.deviceid(timestamp,%s) VALUES (%s,'%s')";
+  private static final Logger logger = LoggerFactory.getLogger(RabbitMQConsumerThread.class);
+
+  public RabbitMQConsumerThread(String message) {
+    this.message = message;
+    /** Establish JDBC connection of IoTDB */
+    initIoTDB();
+  }
+
+  private void initIoTDB() {
+    try {
+      Class.forName("org.apache.iotdb.jdbc.IoTDBDriver");
+      connection =
+          DriverManager.getConnection(
+              Constant.IOTDB_CONNECTION_URL,
+              Constant.IOTDB_CONNECTION_USER,
+              Constant.IOTDB_CONNECTION_PASSWORD);
+      statement = connection.createStatement();
+      if (setStorageGroup) {
+        try {
+          statement.execute(String.format(createStorageGroupSqlTemplate, Constant.STORAGE_GROUP));

Review comment:
       This is ok, another way is to use auto create :)




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

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