You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ha...@apache.org on 2022/07/27 08:24:29 UTC
[iotdb] branch master updated: [IOTDB-3934] Compatibility of Apache IoTDB with InfluxDB——example client (#6757)
This is an automated email from the ASF dual-hosted git repository.
haonan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/master by this push:
new c31eb3cb34 [IOTDB-3934] Compatibility of Apache IoTDB with InfluxDB——example client (#6757)
c31eb3cb34 is described below
commit c31eb3cb346246855874266018be2ead1fb08d00
Author: Xieqijun <44...@users.noreply.github.com>
AuthorDate: Wed Jul 27 16:24:24 2022 +0800
[IOTDB-3934] Compatibility of Apache IoTDB with InfluxDB——example client (#6757)
---
docs/UserGuide/API/InfluxDB-Protocol.md | 2 +-
docs/zh/UserGuide/API/InfluxDB-Protocol.md | 2 +-
example/inflxudb-protocol-example/pom.xml | 39 ++++++++++++
.../apache/iotdb/influxdb}/InfluxDBExample.java | 74 +++++++++++-----------
.../function/aggregator/InfluxSpreadFunction.java | 6 +-
.../db/protocol/influxdb/handler/QueryHandler.java | 4 +-
.../db/protocol/influxdb/meta/TagInfoRecords.java | 6 +-
7 files changed, 92 insertions(+), 41 deletions(-)
diff --git a/docs/UserGuide/API/InfluxDB-Protocol.md b/docs/UserGuide/API/InfluxDB-Protocol.md
index df2a6112bd..571b6345d8 100644
--- a/docs/UserGuide/API/InfluxDB-Protocol.md
+++ b/docs/UserGuide/API/InfluxDB-Protocol.md
@@ -29,7 +29,7 @@
</dependency>
```
-Here are some [examples](https://github.com/apache/iotdb/tree/master/influxdb-protocol/src/main/java/org/apache/iotdb/influxdb/example) of connecting IoTDB using the InfluxDB-Protocol adapter.
+Here are some [examples](https://github.com/apache/iotdb/tree/master/example/inflxudb-protocol-example/src/main/java/org/apache/iotdb/influxdb/InfluxDBExample.java) of connecting IoTDB using the InfluxDB-Protocol adapter.
## 1. Switching Scheme
diff --git a/docs/zh/UserGuide/API/InfluxDB-Protocol.md b/docs/zh/UserGuide/API/InfluxDB-Protocol.md
index ea8ff1a791..a8631a9454 100644
--- a/docs/zh/UserGuide/API/InfluxDB-Protocol.md
+++ b/docs/zh/UserGuide/API/InfluxDB-Protocol.md
@@ -29,7 +29,7 @@
</dependency>
```
-这里是一些使用 InfluxDB-Protocol 适配器连接 IoTDB 的[示例](https://github.com/apache/iotdb/tree/master/influxdb-protocol/src/main/java/org/apache/iotdb/influxdb/example)
+这里是一些使用 InfluxDB-Protocol 适配器连接 IoTDB 的[示例](https://github.com/apache/iotdb/tree/master/example/inflxudb-protocol-example/src/main/java/org/apache/iotdb/influxdb/InfluxDBExample.java)
## 1.切换方案
diff --git a/example/inflxudb-protocol-example/pom.xml b/example/inflxudb-protocol-example/pom.xml
new file mode 100644
index 0000000000..01892bcb1e
--- /dev/null
+++ b/example/inflxudb-protocol-example/pom.xml
@@ -0,0 +1,39 @@
+<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <artifactId>iotdb-examples</artifactId>
+ <groupId>org.apache.iotdb</groupId>
+ <version>0.14.0-SNAPSHOT</version>
+ <relativePath>../pom.xml</relativePath>
+ </parent>
+ <artifactId>inflxudb-protocol-example</artifactId>
+ <name>inflxudb-protocol-example</name>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.iotdb</groupId>
+ <artifactId>influxdb-protocol</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ </dependencies>
+</project>
diff --git a/influxdb-protocol/src/main/java/org/apache/iotdb/influxdb/example/InfluxDBExample.java b/example/inflxudb-protocol-example/src/main/java/org/apache/iotdb/influxdb/InfluxDBExample.java
similarity index 58%
rename from influxdb-protocol/src/main/java/org/apache/iotdb/influxdb/example/InfluxDBExample.java
rename to example/inflxudb-protocol-example/src/main/java/org/apache/iotdb/influxdb/InfluxDBExample.java
index 995948a58a..64a5d17822 100644
--- a/influxdb-protocol/src/main/java/org/apache/iotdb/influxdb/example/InfluxDBExample.java
+++ b/example/inflxudb-protocol-example/src/main/java/org/apache/iotdb/influxdb/InfluxDBExample.java
@@ -17,9 +17,7 @@
* under the License.
*/
-package org.apache.iotdb.influxdb.example;
-
-import org.apache.iotdb.influxdb.IoTDBInfluxDBFactory;
+package org.apache.iotdb.influxdb;
import org.influxdb.InfluxDB;
import org.influxdb.dto.Point;
@@ -34,38 +32,56 @@ public class InfluxDBExample {
private static InfluxDB influxDB;
+ private static final String database = "monitor";
+
+ private static final String measurement = "factory";
+
public static void main(String[] args) {
influxDB = IoTDBInfluxDBFactory.connect("http://127.0.0.1:8086", "root", "root");
- influxDB.createDatabase("database");
- influxDB.setDatabase("database");
+ influxDB.createDatabase(database);
+ influxDB.setDatabase(database);
insertData();
queryData();
influxDB.close();
}
private static void insertData() {
- Point.Builder builder = Point.measurement("student");
+ Point.Builder builder = Point.measurement(measurement);
Map<String, String> tags = new HashMap<>();
Map<String, Object> fields = new HashMap<>();
- tags.put("name", "xie");
- tags.put("sex", "m");
- fields.put("score", 87.0);
- fields.put("tel", "110");
- fields.put("country", "china");
+ tags.put("workshop", "A1");
+ tags.put("production", "B1");
+ tags.put("cell", "C1");
+ fields.put("temperature", 16.9);
+ fields.put("pressure", 142);
builder.tag(tags);
builder.fields(fields);
builder.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
Point point = builder.build();
influxDB.write(point);
- builder = Point.measurement("student");
+ builder = Point.measurement(measurement);
tags = new HashMap<>();
fields = new HashMap<>();
- tags.put("name", "xie");
- tags.put("sex", "m");
- tags.put("province", "anhui");
- fields.put("score", 99.0);
- fields.put("country", "china");
+ tags.put("workshop", "A1");
+ tags.put("production", "B1");
+ tags.put("cell", "C2");
+ fields.put("temperature", 16.5);
+ fields.put("pressure", 108);
+ builder.tag(tags);
+ builder.fields(fields);
+ builder.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
+ point = builder.build();
+ influxDB.write(point);
+
+ builder = Point.measurement(measurement);
+ tags = new HashMap<>();
+ fields = new HashMap<>();
+ tags.put("workshop", "A1");
+ tags.put("production", "B2");
+ tags.put("cell", "C2");
+ fields.put("temperature", 13.0);
+ fields.put("pressure", 130);
builder.tag(tags);
builder.fields(fields);
builder.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
@@ -77,32 +93,18 @@ public class InfluxDBExample {
Query query;
QueryResult result;
- // the selector query is parallel to the field value
query =
new Query(
- "select * from student where (name=\"xie\" and sex=\"m\")or time<now()-7d", "database");
+ "select * from factory where (workshop=\"A1\" and production=\"B1\" and cell =\"C1\" and time>now()-7d)",
+ database);
result = influxDB.query(query);
System.out.println("query1 result:" + result.getResults().get(0).getSeries().get(0).toString());
- // the selector query is parallel to the field value
- query = new Query("select * from student ", "database");
- result = influxDB.query(query);
- System.out.println("query2 result:" + result.getResults().get(0).getSeries().get(0).toString());
-
- // use iotdb built-in func
query =
new Query(
- "select max(score),min(score),sum(score),count(score),first(score),last(score) from student ",
- "database");
+ "select count(temperature),first(temperature),last(temperature),max(temperature),mean(temperature),median(temperature),min(temperature),mode(temperature),spread(temperature),stddev(temperature),sum(temperature) from student where ((workshop=\"A1\" and production=\"B1\" and cell =\"C1\" ) or temperature< 15 )",
+ database);
result = influxDB.query(query);
- System.out.println("query3 result:" + result.getResults().get(0).getSeries().get(0).toString());
-
- // aggregate query and selector query are parallel
- query =
- new Query(
- "select count(score),first(score),last(country),max(score),mean(score),median(score),min(score),mode(score),spread(score),stddev(score),sum(score) from student where (name=\"xie\" and sex=\"m\")or score<99",
- "database");
- result = influxDB.query(query);
- System.out.println("query4 result:" + result.getResults().get(0).getSeries().get(0).toString());
+ System.out.println("query2 result:" + result.getResults().get(0).getSeries().get(0).toString());
}
}
diff --git a/server/src/main/java/org/apache/iotdb/db/protocol/influxdb/function/aggregator/InfluxSpreadFunction.java b/server/src/main/java/org/apache/iotdb/db/protocol/influxdb/function/aggregator/InfluxSpreadFunction.java
index a5cfcbde1e..4da7fa3429 100644
--- a/server/src/main/java/org/apache/iotdb/db/protocol/influxdb/function/aggregator/InfluxSpreadFunction.java
+++ b/server/src/main/java/org/apache/iotdb/db/protocol/influxdb/function/aggregator/InfluxSpreadFunction.java
@@ -35,7 +35,11 @@ public class InfluxSpreadFunction extends InfluxAggregator {
@Override
public InfluxFunctionValue calculateBruteForce() {
- return new InfluxFunctionValue(maxNum - minNum, 0L);
+ if (maxNum == null || minNum == null) {
+ return new InfluxFunctionValue(null, 0L);
+ } else {
+ return new InfluxFunctionValue(maxNum - minNum, 0L);
+ }
}
@Override
diff --git a/server/src/main/java/org/apache/iotdb/db/protocol/influxdb/handler/QueryHandler.java b/server/src/main/java/org/apache/iotdb/db/protocol/influxdb/handler/QueryHandler.java
index e7b0881b6d..113da3912f 100644
--- a/server/src/main/java/org/apache/iotdb/db/protocol/influxdb/handler/QueryHandler.java
+++ b/server/src/main/java/org/apache/iotdb/db/protocol/influxdb/handler/QueryHandler.java
@@ -849,7 +849,9 @@ public class QueryHandler {
curQueryPath.append(".").append("*");
}
}
- curQueryPath.append(".**");
+ if (currentQueryMaxTagNum < tagOrders.size()) {
+ curQueryPath.append(".**");
+ }
// construct actual query condition
StringBuilder realIotDBCondition = new StringBuilder();
for (int i = 0; i < fieldExpressions.size(); i++) {
diff --git a/server/src/main/java/org/apache/iotdb/db/protocol/influxdb/meta/TagInfoRecords.java b/server/src/main/java/org/apache/iotdb/db/protocol/influxdb/meta/TagInfoRecords.java
index 47daa82c05..e6cf7b4f41 100644
--- a/server/src/main/java/org/apache/iotdb/db/protocol/influxdb/meta/TagInfoRecords.java
+++ b/server/src/main/java/org/apache/iotdb/db/protocol/influxdb/meta/TagInfoRecords.java
@@ -65,7 +65,11 @@ public class TagInfoRecords {
public void add(String database, String measurement, String tag, int order) {
deviceIds.add(TAG_INFO_DEVICE_ID);
- times.add(System.currentTimeMillis());
+ if (times.size() == 0) {
+ times.add(System.currentTimeMillis());
+ } else {
+ times.add(times.get(times.size() - 1) + 1);
+ }
measurementsList.add(TAG_INFO_MEASUREMENTS);
typesList.add(TAG_INFO_TYPES);