Skip to content

Commit

Permalink
fix review
Browse files Browse the repository at this point in the history
  • Loading branch information
qiaojialin committed Feb 27, 2020
1 parent 2ea5601 commit c6cfe88
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,23 @@
-->

# 第5章 IoTDB操作指南

## DML (数据操作语言)

## 数据接入

IoTDB为用户提供多种插入实时数据的方式,例如在[Cli/Shell工具](/#/Documents/progress/chap4/sec1)中直接输入插入数据的INSERT语句,或使用Java API(标准[Java JDBC](/#/Documents/progress/chap4/sec2)接口)单条或批量执行插入数据的INSERT语句。

本节主要为您介绍实时数据接入的INSERT语句在场景中的实际使用示例,有关INSERT SQL语句的详细语法请参见本文[INSERT语句](/#/Documents/progress/chap5/sec4)节。

### 使用INSERT语句

使用INSERT语句可以向指定的已经创建的一条或多条时间序列中插入数据。对于每一条数据,均由一个时间戳类型的时间戳和一个数值或布尔值、字符串类型的传感器采集值组成。

在本节的场景实例下,以其中的两个时间序列`root.ln.wf02.wt02.status``root.ln.wf02.wt02.hardware`为例 ,它们的数据类型分别为BOOLEAN和TEXT。

单列数据插入示例代码如下:

```
IoTDB > insert into root.ln.wf02.wt02(timestamp,status) values(1,true)
IoTDB > insert into root.ln.wf02.wt02(timestamp,hardware) values(1, "v1")
Expand Down Expand Up @@ -70,16 +74,21 @@ IoTDB > insert into root.ln.wf02.wt02(timestamp, temperature) values(1,"v1")
```
Msg: The resultDataType or encoding or compression of the last node temperature is conflicting in the storage group root.ln
```

若用户插入的数据类型与该Timeseries对应的数据类型不一致,例如执行以下命令:

```
IoTDB > insert into root.ln.wf02.wt02(timestamp,hardware) values(1,100)
```

系统将会返回以下ERROR告知数据类型有误:

```
error: The TEXT data type should be covered by " or '
```

## 数据查询

### 时间切片查询

本节主要介绍时间切片查询的相关示例,主要使用的是[IoTDB SELECT语句](/#/Documents/progress/chap5/sec4)。同时,您也可以使用[Java JDBC](/#/Documents/progress/chap4/sec2)标准接口来执行相关的查询语句。
Expand All @@ -91,6 +100,7 @@ SQL语句为:
```
select temperature from root.ln.wf01.wt01 where time < 2017-11-01T00:08:00.000
```

其含义为:

被选择的设备为ln集团wf01子站wt01设备;被选择的时间序列为温度传感器(temperature);该语句要求选择出该设备在“2017-11-01T00:08:00.000”(此处可以使用多种时间格式,详情可参看[2.1节](/#/Documents/progress/chap2/sec1))时间点以前的所有温度传感器的值。
Expand All @@ -106,6 +116,7 @@ SQL语句为:
```
select status, temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000;
```

其含义为:

被选择的设备为ln集团wf01子站wt01设备;被选择的时间序列为供电状态(status)和温度传感器(temperature);该语句要求选择出“2017-11-01T00:05:00.000”至“2017-11-01T00:12:00.000”之间的所选时间序列的值。
Expand All @@ -123,6 +134,7 @@ SQL语句为:
```
select status,temperature from root.ln.wf01.wt01 where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000);
```

其含义为:

被选择的设备为ln集团wf01子站wt01设备;被选择的时间序列为“供电状态(status)”和“温度传感器(temperature)”;该语句指定了两个不同的时间区间,分别为“2017-11-01T00:05:00.000至2017-11-01T00:12:00.000”和“2017-11-01T16:35:00.000至2017-11-01T16:37:00.000”;该语句要求选择出满足任一时间区间的被选时间序列的值。
Expand All @@ -138,6 +150,7 @@ select status,temperature from root.ln.wf01.wt01 where (time > 2017-11-01T00:05:
```
select wf01.wt01.status,wf02.wt02.hardware from root.ln where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000);
```

其含义为:

被选择的时间序列为“ln集团wf01子站wt01设备的供电状态”以及“ln集团wf02子站wt02设备的硬件版本”;该语句指定了两个时间区间,分别为“2017-11-01T00:05:00.000至2017-11-01T00:12:00.000”和“2017-11-01T16:35:00.000至2017-11-01T16:37:00.000”;该语句要求选择出满足任意时间区间的被选时间序列的值。
Expand All @@ -149,7 +162,7 @@ select wf01.wt01.status,wf02.wt02.hardware from root.ln where (time > 2017-11-01

IoTDB支持另外两种结果返回形式: 按设备时间对齐 'align by device' 和 时序不对齐 'disable align'.

'align by device' 对齐方式下,设备ID会单独作为一列出现。在select 子句中写了多少列,最终结果就会有该列数+2 (时间列和设备名字列)。SQL形如:
'align by device' 对齐方式下,设备ID会单独作为一列出现。在 select 子句中写了多少列,最终结果就会有该列数+2 (时间列和设备名字列)。SQL形如:

```
select s1,s2 from root.sg1.* GROUP BY DEVICE
Expand All @@ -168,6 +181,8 @@ select s1,s2 from root.sg1.* GROUP BY DEVICE
IoTDB支持根据时间间隔和自定义的滑动步长(默认值与时间间隔相同,自定义的值必须大于等于时间间隔)对结果集进行划分,默认结果按照时间升序排列。
同时,您也可以使用Java JDBC标准接口来执行相关的查询语句。

Group By 语句不支持 limit 和 offset。

GROUP BY语句为用户提供三类指定参数:

* 参数1:时间轴显示时间窗参数
Expand All @@ -185,6 +200,7 @@ GROUP BY语句为用户提供三类指定参数:
**图 5.2 三类参数的实际含义**</center>

#### 未指定滑动步长的降频聚合查询

对应的SQL语句是:

```
Expand All @@ -205,6 +221,7 @@ select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2
<center><img style="width:100%; max-width:800px; max-height:600px; margin-left:auto; margin-right:auto; display:block;" src="https://user-images.githubusercontent.com/16079446/69116068-eed51b00-0ac5-11ea-9731-b5a45c5cd224.png"></center>

#### 指定滑动步长的降频聚合查询

对应的SQL语句是:

```
Expand All @@ -230,11 +247,13 @@ select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2
<center><img style="width:100%; max-width:800px; max-height:600px; margin-left:auto; margin-right:auto; display:block;" src="https://user-images.githubusercontent.com/16079446/69116083-f85e8300-0ac5-11ea-84f1-59d934eee96e.png"></center>

#### 带值过滤条件的降频聚合查询

对应的SQL语句是:

```
select count(status), max_value(temperature) from root.ln.wf01.wt01 where time > 2017-11-01T01:00:00 and temperature > 20 group by([2017-11-01T00:00:00, 2017-11-07T23:00:00], 3h, 1d);
```

这条查询的含义是:

由于用户指定了滑动步长为`1d`,GROUP BY语句执行时将会每次把时间间隔往后移动一天的步长,而不是默认的3小时。
Expand Down Expand Up @@ -283,13 +302,15 @@ delete from root.ln.wf02.wt02.status where time<=2017-11-01T16:26:00;
```
delete from root.ln.wf02.wt02 where time <= 2017-11-01T16:26:00;
```


```
delete from root.ln.wf02.wt02.* where time <= 2017-11-01T16:26:00;
```

需要注意的是,当删除的路径不存在时,IoTDB会提示路径不存在,无法删除数据,如下所示。

```
IoTDB> delete from root.ln.wf03.wt02.status where time < now()
Msg: TimeSeries does not exist and its data cannot be deleted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ Note: In Version 0.7.0, if <WhereClause> includes `OR`, time filter can not be u
Note: There must be a space on both sides of the plus and minus operator appearing in the time expression
```

* Group By语句
* Group By 语句

```
SELECT <SelectClause> FROM <FromClause> WHERE <WhereClause> GROUP BY <GroupByClause>
Expand Down Expand Up @@ -276,7 +276,7 @@ Note: <TimeUnit> needs to be greater than 0
Note: Third <TimeUnit> if set shouldn't be smaller than second <TimeUnit>
```

* Fill语句
* Fill 语句

```
SELECT <SelectClause> FROM <FromClause> WHERE <WhereClause> FILL <FillClause>
Expand Down Expand Up @@ -305,7 +305,7 @@ Note: the statement needs to satisfy this constraint: <PrefixPath>(FromClause) +
Note: Integer in <TimeUnit> needs to be greater than 0
```

* Limit语句
* Limit & SLimit 语句

```
SELECT <SelectClause> FROM <FromClause> [WHERE <WhereClause>] [<LIMITClause>] [<SLIMITClause>]
Expand Down Expand Up @@ -334,14 +334,15 @@ Note: The order of <LIMITClause> and <SLIMITClause> does not affect the grammati
Note: <FillClause> can not use <LIMITClause> but not <SLIMITClause>.
```

* Group by device语句
* Align by device语句

```
GroupbyDeviceClause : GROUP BY DEVICE
AlignbyDeviceClause : ALIGN BY DEVICE
规则:
1. 大小写不敏感.
正例: select * from root.sg1 align by device
正例: select * from root.sg1 GROUP BY DEVICE
正例: select * from root.sg1 align by device
正例: select * from root.sg1 ALIGN BY DEVICE
2. AlignbyDeviceClause 只能放在末尾.
正例: select * from root.sg1 where time > 10 align by device
Expand Down Expand Up @@ -405,7 +406,8 @@ root.sg1.d0.s0 is INT32 while root.sg2.d3.s0 is FLOAT.
- select * from root.vehicle where time = 3 Fill(int32[previous, 5ms]) align by device
```

* Disable align语句
* Disable align 语句

```
规则:
1. 大小写均可.
Expand Down Expand Up @@ -440,7 +442,6 @@ root.sg1.d0.s0 is INT32 while root.sg2.d3.s0 is FLOAT.
- select * from root.vehicle slimit 10 soffset 2 disable align
- select * from root.vehicle where time > 10 disable align
```

### 数据库管理语句
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ error: The TEXT data type should be covered by " or '
```

## SELECT

### Time Slice Query

This chapter mainly introduces the relevant examples of time slice query using IoTDB SELECT statements. Detailed SQL syntax and usage specifications can be found in [SQL Documentation](/#/Documents/progress/chap5/sec4). You can also use the [Java JDBC](/#/Documents/progress/chap4/sec2) standard interface to execute related queries.
Expand Down Expand Up @@ -117,6 +118,7 @@ The execution result of this SQL statement is as follows:
<center><img style="width:100%; max-width:800px; max-height:600px; margin-left:auto; margin-right:auto; display:block;" src="https://user-images.githubusercontent.com/23614968/61280328-40a12800-a7ea-11e9-85b9-3b8db67673a3.png"></center>

#### Select Multiple Columns of Data for the Same Device According to Multiple Time Intervals

IoTDB supports specifying multiple time interval conditions in a query. Users can combine time interval conditions at will according to their needs. For example, the SQL statement is:

```
Expand All @@ -131,6 +133,7 @@ The execution result of this SQL statement is as follows:


#### Choose Multiple Columns of Data for Different Devices According to Multiple Time Intervals

The system supports the selection of data in any column in a query, i.e., the selected columns can come from different devices. For example, the SQL statement is:

```
Expand All @@ -144,6 +147,7 @@ The execution result of this SQL statement is as follows:
<center><img style="width:100%; max-width:800px; max-height:600px; margin-left:auto; margin-right:auto; display:block;" src="https://user-images.githubusercontent.com/13203019/51577450-dcfe0800-1ef4-11e9-9399-4ba2b2b7fb73.jpg"></center>

### Down-Frequency Aggregate Query

This section mainly introduces the related examples of down-frequency aggregation query,
using the [GROUP BY clause](/#/Documents/progress/chap5/sec4),
which is used to partition the result set according to the user's given partitioning conditions and aggregate the partitioned result set.
Expand All @@ -168,6 +172,7 @@ and value filtering conditions specified.
**Figure 5.2 The actual meanings of the three types of parameters**</center>

#### Down-Frequency Aggregate Query without Specifying the Sliding Step Length

The SQL statement is:

```
Expand All @@ -188,6 +193,7 @@ Since there is data for each time period in the result range to be displayed, th
<center><img style="width:100%; max-width:800px; max-height:600px; margin-left:auto; margin-right:auto; display:block;" src="https://user-images.githubusercontent.com/16079446/69116068-eed51b00-0ac5-11ea-9731-b5a45c5cd224.png"></center>

#### Down-Frequency Aggregate Query Specifying the Sliding Step Length

The SQL statement is:

```
Expand All @@ -213,6 +219,7 @@ Since there is data for each time period in the result range to be displayed, th
<center><img style="width:100%; max-width:800px; max-height:600px; margin-left:auto; margin-right:auto; display:block;" src="https://user-images.githubusercontent.com/16079446/69116083-f85e8300-0ac5-11ea-84f1-59d934eee96e.png"></center>

#### Down-Frequency Aggregate Query Specifying the value Filtering Conditions

The SQL statement is:

```
Expand All @@ -237,13 +244,15 @@ The path after SELECT in GROUP BY statement must be aggregate function, otherwis
<center><img style="width:100%; max-width:800px; max-height:600px; margin-left:auto; margin-right:auto; display:block;" src="https://user-images.githubusercontent.com/16079446/69116099-0b715300-0ac6-11ea-8074-84e04797b8c7.png"></center>

### Automated Fill

In the actual use of IoTDB, when doing the query operation of timeseries, situations where the value is null at some time points may appear, which will obstruct the further analysis by users. In order to better reflect the degree of data change, users expect missing values to be automatically filled. Therefore, the IoTDB system introduces the function of Automated Fill.

Automated fill function refers to filling empty values according to the user's specified method and effective time range when performing timeseries queries for single or multiple columns. If the queried point's value is not null, the fill function will not work.

> Note: In the current version, IoTDB provides users with two methods: Previous and Linear. The previous method fills blanks with previous value. The linear method fills blanks through linear fitting. And the fill function can only be used when performing point-in-time queries.
#### Fill Function

* Previous Function

When the value of the queried timestamp is null, the value of the previous timestamp is used to fill the blank. The formalized previous method is as follows (see Section 7.1.3.6 for detailed syntax):
Expand Down Expand Up @@ -311,6 +320,7 @@ On the [sample data](https://raw.githubusercontent.com/apache/incubator-iotdb/ma
<center><img style="width:100%; max-width:800px; max-height:600px; margin-left:auto; margin-right:auto; display:block;" src="https://user-images.githubusercontent.com/13203019/51577727-d4f29800-1ef5-11e9-8ff3-3bb519da3993.jpg"></center>

#### Correspondence between Data Type and Fill Method

Data types and the supported fill methods are shown in Table 3-6.

<center>**Table 3-6 Data types and the supported fill methods**
Expand Down Expand Up @@ -347,11 +357,17 @@ When the fill method is not specified, each data type bears its own default fill
### Row and Column Control over Query Results

IoTDB provides [LIMIT/SLIMIT](/#/Documents/progress/chap5/sec4) clause and [OFFSET/SOFFSET](/#/Documents/progress/chap5/sec4) clause in order to make users have more control over query results. The use of LIMIT and SLIMIT clauses allows users to control the number of rows and columns of query results, and the use of OFFSET and SOFSET clauses allows users to set the starting position of the results for display.
IoTDB provides [LIMIT/SLIMIT](/#/Documents/progress/chap5/sec4) clause and [OFFSET/SOFFSET](/#/Documents/progress/chap5/sec4)
clause in order to make users have more control over query results.
The use of LIMIT and SLIMIT clauses allows users to control the number of rows and columns of query results,
and the use of OFFSET and SOFSET clauses allows users to set the starting position of the results for display.

Note that the LIMIT and OFFSET are not supported in group by query.

This chapter mainly introduces related examples of row and column control of query results. You can also use the [Java JDBC](/#/Documents/progress/chap4/sec2) standard interface to execute queries.

#### Row Control over Query Results

By using LIMIT and OFFSET clauses, users can control the query results in a row-related manner. We will demonstrate how to use LIMIT and OFFSET clauses through the following examples.

* Example 1: basic LIMIT clause
Expand All @@ -369,7 +385,6 @@ The result is shown below:

<center><img style="width:100%; max-width:800px; max-height:600px; margin-left:auto; margin-right:auto; display:block;" src="https://user-images.githubusercontent.com/13203019/51577752-efc50c80-1ef5-11e9-9071-da2bbd8b9bdd.jpg"></center>


* Example 2: LIMIT clause with OFFSET

The SQL statement is:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,10 @@ Note: The order of <LIMITClause> and <SLIMITClause> does not affect the grammati
Note: <FillClause> can not use <LIMITClause> but not <SLIMITClause>.
```

* Group By Device Statement
* Align By Device Statement

```
GroupbyDeviceClause : GROUP BY DEVICE
AlignbyDeviceClause : ALIGN BY DEVICE
Rules:
1. Both uppercase and lowercase are ok.
Expand Down Expand Up @@ -415,6 +416,7 @@ For example, "select s0,s0,s1 from root.sg.* align by device" is not equal to "s
- select * from root.vehicle where time = 3 Fill(int32[previous, 5ms]) align by device
```
* Disable Align Statement

```
Disable Align Clause: DISABLE ALIGN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public String getColumnClassName(int arg0) throws SQLException {

@Override
public int getColumnCount() {
return columnInfoList.size();
return columnInfoList == null ? 0 : columnInfoList.size();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void testGetColumnCount() {
} catch (Exception e) {
flag = true;
}
assertTrue(flag);
assertFalse(flag);

flag = false;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ private QueryDataSet processDataQuery(QueryPlan queryPlan, QueryContext context)
return new EmptyDataSet();
} else if (queryPlan instanceof GroupByPlan) {
GroupByPlan groupByPlan = (GroupByPlan) queryPlan;
queryDataSet = queryRouter.groupBy(groupByPlan, context);
return queryRouter.groupBy(groupByPlan, context);
} else if (queryPlan instanceof AggregationPlan) {
AggregationPlan aggregationPlan = (AggregationPlan) queryPlan;
queryDataSet = queryRouter.aggregate(aggregationPlan, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void testEmptyDataSet() throws SQLException, ClassNotFoundException {
resultSet = statement.executeQuery(
"select count(*) from root where time >= 1 and time <= 100 "
+ "group by ([0, 100), 20ms, 20ms) align by device");
// has an empty time column
// has time and device columns
Assert.assertEquals(2, resultSet.getMetaData().getColumnCount());
while (resultSet.next()) {
fail();
Expand Down

0 comments on commit c6cfe88

Please sign in to comment.