This is an automated email from the ASF dual-hosted git repository.
jackietien pushed a commit to branch tsbs/iot
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/tsbs/iot by this push:
new 2acf31a7ddb query done
2acf31a7ddb is described below
commit 2acf31a7ddb3ae3ba6ffa8e32978e890a5cc7719
Author: JackieTien97 <[email protected]>
AuthorDate: Mon Apr 29 20:17:11 2024 +0800
query done
---
example/session/pom.xml | 45 ++++++++
.../main/java/org/apache/iotdb/TSBSExample.java | 117 +++++++++++++++++++++
.../java/org/apache/iotdb/session/Session.java | 41 ++++++++
.../apache/iotdb/session/SessionConnection.java | 81 ++++++++++++++
.../protocol/thrift/impl/ClientRPCServiceImpl.java | 17 +--
.../apache/iotdb/db/utils/QueryDataSetUtils.java | 59 ++++++++---
6 files changed, 340 insertions(+), 20 deletions(-)
diff --git a/example/session/pom.xml b/example/session/pom.xml
index ac5d936f997..0f3f27a23d6 100644
--- a/example/session/pom.xml
+++ b/example/session/pom.xml
@@ -40,4 +40,49 @@
<version>${project.version}</version>
</dependency>
</dependencies>
+ <profiles>
+ <profile>
+ <id>get-jar-with-dependencies</id>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-assembly-plugin</artifactId>
+ <configuration>
+ <descriptorRefs>
+
<descriptorRef>jar-with-dependencies</descriptorRef>
+ </descriptorRefs>
+ <archive>
+ <manifest>
+
<mainClass>org.apache.iotdb.TSBSExample</mainClass>
+ </manifest>
+ </archive>
+ </configuration>
+ <executions>
+ <execution>
+ <id>make-assembly</id>
+ <!-- bind to the packaging phase -->
+ <goals>
+ <goal>single</goal>
+ </goals>
+ <!-- this is used for inheritance merges -->
+ <phase>package</phase>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jar-plugin</artifactId>
+ <configuration>
+ <archive>
+ <manifest>
+
<mainClass>org.apache.iotdb.TSBSExample</mainClass>
+ </manifest>
+ </archive>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ </profile>
+ </profiles>
</project>
diff --git a/example/session/src/main/java/org/apache/iotdb/TSBSExample.java
b/example/session/src/main/java/org/apache/iotdb/TSBSExample.java
new file mode 100644
index 00000000000..8832c63b1b6
--- /dev/null
+++ b/example/session/src/main/java/org/apache/iotdb/TSBSExample.java
@@ -0,0 +1,117 @@
+/*
+ * 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;
+
+import org.apache.iotdb.isession.SessionDataSet;
+import org.apache.iotdb.isession.util.Version;
+import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.rpc.StatementExecutionException;
+import org.apache.iotdb.session.Session;
+
+public class TSBSExample {
+
+ public static void main(String[] args)
+ throws IoTDBConnectionException, StatementExecutionException {
+ String host = args[0];
+ Session session =
+ new Session.Builder()
+ .host(host)
+ .port(6667)
+ .username("root")
+ .password("root")
+ .version(Version.V_1_0)
+ .fetchSize(10_000)
+ .timeOut(6_000_000)
+ .build();
+ session.open(false);
+
+ String queryType = args[1];
+
+ switch (queryType) {
+ case "last-loc":
+ try (SessionDataSet dataSet =
+ session.executeQueryStatement(
+ String.format(
+ "SELECT max_time(latitude), last_value(latitude),
last_value(longitude) FROM root.readings.%s.** align by device",
+ args[2]))) {
+ SessionDataSet.DataIterator iterator = dataSet.iterator();
+ long count = 0;
+ while (iterator.next()) {
+ count++;
+ }
+ System.out.println("total lines: " + count);
+ }
+ break;
+ case "low-fuel":
+ try (SessionDataSet dataSet =
+ session.executeQueryStatement(
+ String.format(
+ "SELECT last_value(fuel_state) FROM root.diagnostics.%s.**
WHERE fuel_state <= 0.1 align by device;",
+ args[2]))) {
+ SessionDataSet.DataIterator iterator = dataSet.iterator();
+ long count = 0;
+ while (iterator.next()) {
+ count++;
+ }
+ System.out.println("total lines: " + count);
+ }
+ break;
+ case "high-load":
+ try (SessionDataSet dataSet = session.tsbsIoTHighLoad(args[2])) {
+ SessionDataSet.DataIterator iterator = dataSet.iterator();
+ long count = 0;
+ while (iterator.next()) {
+ count++;
+ }
+ System.out.println("total lines: " + count);
+ }
+ break;
+ case "stationary-trucks":
+ try (SessionDataSet dataSet =
+ session.executeQueryStatement(
+ String.format(
+ "select avg(velocity) from root.readings.%s.** group
by([startTime, endTime), 10m) having avg(velocity) < 1 align by device",
+ args[2]))) {
+ SessionDataSet.DataIterator iterator = dataSet.iterator();
+ long count = 0;
+ while (iterator.next()) {
+ count++;
+ }
+ System.out.println("total lines: " + count);
+ }
+ break;
+ case "breakdown-frequency":
+ try (SessionDataSet dataSet =
+ session.tsbsIoTBreakdownFrequency(Long.parseLong(args[2]),
Long.parseLong(args[3]))) {
+ SessionDataSet.DataIterator iterator = dataSet.iterator();
+ long count = 0;
+ while (iterator.next()) {
+ count++;
+ }
+ System.out.println("total lines: " + count);
+ }
+ break;
+ default:
+ throw new UnsupportedOperationException(queryType);
+ }
+
+ session.close();
+ }
+}
diff --git
a/iotdb-client/session/src/main/java/org/apache/iotdb/session/Session.java
b/iotdb-client/session/src/main/java/org/apache/iotdb/session/Session.java
index 02be2198a0f..d4867b70819 100644
--- a/iotdb-client/session/src/main/java/org/apache/iotdb/session/Session.java
+++ b/iotdb-client/session/src/main/java/org/apache/iotdb/session/Session.java
@@ -861,6 +861,47 @@ public class Session implements ISession {
return executeStatementMayRedirect(sql, queryTimeoutInMs);
}
+ public SessionDataSet tsbsIoTHighLoad(String fleet)
+ throws StatementExecutionException, IoTDBConnectionException {
+ try {
+ return defaultSessionConnection.executeTSBSHighLoad(fleet,
queryTimeoutInMs);
+ } catch (RedirectException e) {
+ handleQueryRedirection(e.getEndPoint());
+ if (enableQueryRedirection) {
+ // retry
+ try {
+ return defaultSessionConnection.executeTSBSHighLoad(fleet,
queryTimeoutInMs);
+ } catch (RedirectException redirectException) {
+ logger.error("{} redirect twice", fleet, redirectException);
+ throw new StatementExecutionException(fleet + " redirect twice,
please try again.");
+ }
+ } else {
+ throw new StatementExecutionException(MSG_DONOT_ENABLE_REDIRECT);
+ }
+ }
+ }
+
+ public SessionDataSet tsbsIoTBreakdownFrequency(long startTime, long endTime)
+ throws StatementExecutionException, IoTDBConnectionException {
+ try {
+ return defaultSessionConnection.executeTSBSBreakDownFrequency(
+ startTime, endTime, queryTimeoutInMs);
+ } catch (RedirectException e) {
+ handleQueryRedirection(e.getEndPoint());
+ if (enableQueryRedirection) {
+ // retry
+ try {
+ return defaultSessionConnection.executeTSBSBreakDownFrequency(
+ startTime, endTime, queryTimeoutInMs);
+ } catch (RedirectException redirectException) {
+ throw new StatementExecutionException("redirect twice, please try
again.");
+ }
+ } else {
+ throw new StatementExecutionException(MSG_DONOT_ENABLE_REDIRECT);
+ }
+ }
+ }
+
/**
* execute query sql with explicit timeout
*
diff --git
a/iotdb-client/session/src/main/java/org/apache/iotdb/session/SessionConnection.java
b/iotdb-client/session/src/main/java/org/apache/iotdb/session/SessionConnection.java
index 731172a5bd2..e801a143609 100644
---
a/iotdb-client/session/src/main/java/org/apache/iotdb/session/SessionConnection.java
+++
b/iotdb-client/session/src/main/java/org/apache/iotdb/session/SessionConnection.java
@@ -34,6 +34,7 @@ import
org.apache.iotdb.service.rpc.thrift.TCreateTimeseriesUsingSchemaTemplateR
import org.apache.iotdb.service.rpc.thrift.TSAggregationQueryReq;
import org.apache.iotdb.service.rpc.thrift.TSAppendSchemaTemplateReq;
import org.apache.iotdb.service.rpc.thrift.TSBackupConfigurationResp;
+import org.apache.iotdb.service.rpc.thrift.TSBreakdownFrequencyReq;
import org.apache.iotdb.service.rpc.thrift.TSCloseSessionReq;
import org.apache.iotdb.service.rpc.thrift.TSConnectionInfoResp;
import org.apache.iotdb.service.rpc.thrift.TSCreateAlignedTimeseriesReq;
@@ -45,6 +46,7 @@ import
org.apache.iotdb.service.rpc.thrift.TSDropSchemaTemplateReq;
import org.apache.iotdb.service.rpc.thrift.TSExecuteStatementReq;
import org.apache.iotdb.service.rpc.thrift.TSExecuteStatementResp;
import org.apache.iotdb.service.rpc.thrift.TSFastLastDataQueryForOneDeviceReq;
+import org.apache.iotdb.service.rpc.thrift.TSHighLoadReq;
import org.apache.iotdb.service.rpc.thrift.TSInsertRecordReq;
import org.apache.iotdb.service.rpc.thrift.TSInsertRecordsOfOneDeviceReq;
import org.apache.iotdb.service.rpc.thrift.TSInsertRecordsReq;
@@ -441,6 +443,85 @@ public class SessionConnection {
session.fetchSize);
}
+ protected SessionDataSet executeTSBSHighLoad(String fleet, long timeout)
+ throws StatementExecutionException, IoTDBConnectionException,
RedirectException {
+ TSHighLoadReq execReq =
+ new TSHighLoadReq(sessionId, statementId, fleet, session.fetchSize,
timeout);
+ TSExecuteStatementResp execResp;
+ try {
+ execResp = client.highLoad(execReq);
+ RpcUtils.verifySuccessWithRedirection(execResp.getStatus());
+ } catch (TException e) {
+ if (reconnect()) {
+ try {
+ execReq.setSessionId(sessionId);
+ execReq.setStatementId(statementId);
+ execResp = client.highLoad(execReq);
+ } catch (TException tException) {
+ throw new IoTDBConnectionException(tException);
+ }
+ } else {
+ throw new IoTDBConnectionException(logForReconnectionFailure());
+ }
+ }
+
+ RpcUtils.verifySuccess(execResp.getStatus());
+ return new SessionDataSet(
+ "high-load",
+ execResp.getColumns(),
+ execResp.getDataTypeList(),
+ execResp.columnNameIndexMap,
+ execResp.getQueryId(),
+ statementId,
+ client,
+ sessionId,
+ execResp.queryResult,
+ execResp.isIgnoreTimeStamp(),
+ timeout,
+ execResp.moreData,
+ session.fetchSize);
+ }
+
+ protected SessionDataSet executeTSBSBreakDownFrequency(long startTime, long
endTime, long timeout)
+ throws StatementExecutionException, IoTDBConnectionException,
RedirectException {
+ TSBreakdownFrequencyReq execReq =
+ new TSBreakdownFrequencyReq(
+ sessionId, statementId, startTime, endTime, session.fetchSize,
timeout);
+ TSExecuteStatementResp execResp;
+ try {
+ execResp = client.breakdownFrequency(execReq);
+ RpcUtils.verifySuccessWithRedirection(execResp.getStatus());
+ } catch (TException e) {
+ if (reconnect()) {
+ try {
+ execReq.setSessionId(sessionId);
+ execReq.setStatementId(statementId);
+ execResp = client.breakdownFrequency(execReq);
+ } catch (TException tException) {
+ throw new IoTDBConnectionException(tException);
+ }
+ } else {
+ throw new IoTDBConnectionException(logForReconnectionFailure());
+ }
+ }
+
+ RpcUtils.verifySuccess(execResp.getStatus());
+ return new SessionDataSet(
+ "breakdown-frequency",
+ execResp.getColumns(),
+ execResp.getDataTypeList(),
+ execResp.columnNameIndexMap,
+ execResp.getQueryId(),
+ statementId,
+ client,
+ sessionId,
+ execResp.queryResult,
+ execResp.isIgnoreTimeStamp(),
+ timeout,
+ execResp.moreData,
+ session.fetchSize);
+ }
+
protected void executeNonQueryStatement(String sql)
throws IoTDBConnectionException, StatementExecutionException {
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java
index 4c1e2fec76a..ec3515b30c0 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java
@@ -274,6 +274,8 @@ public class ClientRPCServiceImpl implements
IClientRPCServiceWithHandler {
private static final String NOMINAL_FUEL_CONSUMPTION_COLUMN_NAME =
"nominal_fuel_consumption";
+ private static final String FUEL_CAPACITY_COLUMN_NAME = "fuel_capacity";
+
private static final String FLEET_COLUMN_NAME = "fleet";
private static final String MODEL_COLUMN_NAME = "model";
@@ -2497,7 +2499,8 @@ public class ClientRPCServiceImpl implements
IClientRPCServiceWithHandler {
if (deviceAttributesHashMap == null) {
ShowTimeSeriesStatement statement =
new ShowTimeSeriesStatement(
- new PartialPath(new String[] {"root", "**",
ATTRIBUTE_COLUMN_NAME}), false);
+ new PartialPath(new String[] {"root", "attributes", "*",
ATTRIBUTE_COLUMN_NAME}),
+ false);
long queryId = SessionManager.getInstance().requestQueryId();
Throwable t = null;
try {
@@ -2506,7 +2509,7 @@ public class ClientRPCServiceImpl implements
IClientRPCServiceWithHandler {
statement,
queryId,
null,
- "show timeseries root.**." + ATTRIBUTE_COLUMN_NAME,
+ "show timeseries root.attributes.*" +
ATTRIBUTE_COLUMN_NAME,
partitionFetcher,
schemaFetcher,
Long.MAX_VALUE);
@@ -2553,19 +2556,19 @@ public class ClientRPCServiceImpl implements
IClientRPCServiceWithHandler {
String value = tmpArray[1].substring(1,
tmpArray[1].length() - 1);
if (NOMINAL_FUEL_CONSUMPTION_COLUMN_NAME.equals(key)
|| LOAD_CAPACITY_COLUMN_NAME.equals(key)
- || FUEL_CONSUMPTION_COLUMN_NAME.equals(key)) {
+ || FUEL_CAPACITY_COLUMN_NAME.equals(key)) {
parsedMap.put(key, Double.parseDouble(value));
}
}
}
- // remove .attr
+ // remove root.attributes. and .attr
tmp.put(
- timeSeries.substring(0, timeSeries.length() - 5),
+ timeSeries.substring(16, timeSeries.length() - 5),
new DeviceAttributes(
parsedMap.getOrDefault(NOMINAL_FUEL_CONSUMPTION_COLUMN_NAME, 1.0d),
parsedMap.getOrDefault(LOAD_CAPACITY_COLUMN_NAME,
1.0d),
-
parsedMap.getOrDefault(FUEL_CONSUMPTION_COLUMN_NAME, 1.0d)));
+ parsedMap.getOrDefault(FUEL_CAPACITY_COLUMN_NAME,
1.0d)));
}
}
}
@@ -2649,7 +2652,7 @@ public class ClientRPCServiceImpl implements
IClientRPCServiceWithHandler {
resp.setColumns(HIGH_LOAD_HEADERS);
resp.setDataTypeList(HIGH_LOAD_HEADER_DATA_TYPES);
resp.setAliasColumns(HIGH_LOAD_ALIAS_COLUMNS);
- resp.setIgnoreTimeStamp(false);
+ resp.setIgnoreTimeStamp(true);
resp.setQueryId(queryId);
resp.setStatus(result.status);
Pair<List<ByteBuffer>, Boolean> pair =
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/QueryDataSetUtils.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/QueryDataSetUtils.java
index 4b084af334b..0b395a2744e 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/QueryDataSetUtils.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/QueryDataSetUtils.java
@@ -909,9 +909,7 @@ public class QueryDataSetUtils {
String driver = seriesArray[DRIVER_LEVEL];
double value =
Double.parseDouble(valueColumn.getBinary(i).getStringValue(StandardCharsets.UTF_8));
- double loadCapacity =
- deviceAttributesMap.get(timeSeries.substring(0,
timeSeries.length() - 13))
- .loadCapacity;
+ double loadCapacity = deviceAttributesMap.get(name).loadCapacity;
if (value >= 0.9 * loadCapacity) {
builder.declarePosition();
@@ -934,7 +932,11 @@ public class QueryDataSetUtils {
}
}
- return new
Pair<>(Collections.singletonList(serde.serialize(builder.build())), finished);
+ return new Pair<>(
+ builder.isEmpty()
+ ? Collections.emptyList()
+ : Collections.singletonList(serde.serialize(builder.build())),
+ finished);
}
public static Pair<List<ByteBuffer>, Boolean>
constructLongDrivingSessionsResult(
@@ -999,7 +1001,11 @@ public class QueryDataSetUtils {
driverColumnBuilder.writeBinary(new Binary(driver,
StandardCharsets.UTF_8));
}
- return new
Pair<>(Collections.singletonList(serde.serialize(builder.build())), finished);
+ return new Pair<>(
+ builder.isEmpty()
+ ? Collections.emptyList()
+ : Collections.singletonList(serde.serialize(builder.build())),
+ finished);
}
public static Pair<List<ByteBuffer>, Boolean>
constructAvgVsProjectedFuelConsumptionResult(
@@ -1026,9 +1032,11 @@ public class QueryDataSetUtils {
int currentCount = tsBlock.getPositionCount();
for (int i = 0; i < currentCount; i++) {
String deviceId =
deviceColumn.getBinary(i).getStringValue(StandardCharsets.UTF_8);
- double nominalFuelConsumption =
deviceAttributesMap.get(deviceId).nominalFuelConsumption;
String[] seriesArray = deviceId.split("\\.");
String model = seriesArray[MODEL_LEVEL];
+ String name = seriesArray[NAME_LEVEL];
+ double nominalFuelConsumption =
deviceAttributesMap.get(name).nominalFuelConsumption;
+
AvgIntermediateResult result =
map.computeIfAbsent(model, k -> new AvgIntermediateResult());
long count = countFuelColumn.getLong(i);
@@ -1056,7 +1064,11 @@ public class QueryDataSetUtils {
builder.declarePositions(size);
- return new
Pair<>(Collections.singletonList(serde.serialize(builder.build())), finished);
+ return new Pair<>(
+ builder.isEmpty()
+ ? Collections.emptyList()
+ : Collections.singletonList(serde.serialize(builder.build())),
+ finished);
}
private static class AvgIntermediateResult {
@@ -1186,7 +1198,11 @@ public class QueryDataSetUtils {
} while (timeRangeIterator.hasNextTimeRange());
}
- return new
Pair<>(Collections.singletonList(serde.serialize(builder.build())), finished);
+ return new Pair<>(
+ builder.isEmpty()
+ ? Collections.emptyList()
+ : Collections.singletonList(serde.serialize(builder.build())),
+ finished);
}
public static Pair<List<ByteBuffer>, Boolean>
constructAvgDailyDrivingSessionResult(
@@ -1274,7 +1290,11 @@ public class QueryDataSetUtils {
}
}
- return new
Pair<>(Collections.singletonList(serde.serialize(builder.build())), finished);
+ return new Pair<>(
+ builder.isEmpty()
+ ? Collections.emptyList()
+ : Collections.singletonList(serde.serialize(builder.build())),
+ finished);
}
public static Pair<List<ByteBuffer>, Boolean> constructAvgLoadResult(
@@ -1306,8 +1326,9 @@ public class QueryDataSetUtils {
String[] deviceArray = deviceId.split("\\.");
String fleet = deviceArray[FLEET_LEVEL];
String model = deviceArray[MODEL_LEVEL];
+ String name = deviceArray[NAME_LEVEL];
double avgLoad = avgLoadColumn.getDouble(i);
- double loadCapacity = deviceAttributesMap.get(deviceId).loadCapacity;
+ double loadCapacity = deviceAttributesMap.get(name).loadCapacity;
AvgLoadKey key = new AvgLoadKey(fleet, model, loadCapacity);
AvgLoadIntermediateResult intermediateResult =
map.computeIfAbsent(key, k -> new AvgLoadIntermediateResult());
@@ -1337,7 +1358,11 @@ public class QueryDataSetUtils {
builder.declarePositions(size);
- return new
Pair<>(Collections.singletonList(serde.serialize(builder.build())), finished);
+ return new Pair<>(
+ builder.isEmpty()
+ ? Collections.emptyList()
+ : Collections.singletonList(serde.serialize(builder.build())),
+ finished);
}
private static class AvgLoadIntermediateResult {
@@ -1430,7 +1455,11 @@ public class QueryDataSetUtils {
builder.declarePositions(size);
- return new
Pair<>(Collections.singletonList(serde.serialize(builder.build())), finished);
+ return new Pair<>(
+ builder.isEmpty()
+ ? Collections.emptyList()
+ : Collections.singletonList(serde.serialize(builder.build())),
+ finished);
}
private static class DailyActivityKey {
@@ -1572,6 +1601,10 @@ public class QueryDataSetUtils {
builder.declarePositions(size);
- return new
Pair<>(Collections.singletonList(serde.serialize(builder.build())), true);
+ return new Pair<>(
+ builder.isEmpty()
+ ? Collections.emptyList()
+ : Collections.singletonList(serde.serialize(builder.build())),
+ true);
}
}