Skip to content

Commit 0405f9d

Browse files
authored
add insert into query to table write 206 (#947)
* add insert into query to table write 206 * adjust RestAPI-V1
1 parent b306e85 commit 0405f9d

36 files changed

+3992
-1320
lines changed

src/.vuepress/sidebar/V2.0.x/en-Table.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export const enSidebar = {
8787
children: [
8888
{ text: 'Database Management', link: 'Database-Management' },
8989
{ text: 'Table Management', link: 'Table-Management' },
90-
{ text: 'Write&Updata Data', link: 'Write-Updata-Data' },
90+
{ text: 'Write&Updata Data', link: 'Write-Updata-Data_apache' },
9191
{ text: 'Query Data', link: 'Query-Data_apache' },
9292
{
9393
text: 'Delete Data',
@@ -213,7 +213,7 @@ export const enSidebar = {
213213
prefix: 'SQL-Manual/',
214214
children: [
215215
{ text: 'Metadata Operations', link: 'SQL-Metadata-Operations' },
216-
{ text: 'Data Addition&Deletion', link: 'SQL-Data-Addition-Deletion' },
216+
{ text: 'Data Addition&Deletion', link: 'SQL-Data-Addition-Deletion_apache' },
217217
{
218218
text: 'Data Query',
219219
collapsible: true,

src/.vuepress/sidebar/V2.0.x/zh-Table.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export const zhSidebar = {
8787
children: [
8888
{ text: '数据库管理', link: 'Database-Management' },
8989
{ text: '表管理', link: 'Table-Management' },
90-
{ text: '写入&更新', link: 'Write-Updata-Data' },
90+
{ text: '写入&更新', link: 'Write-Updata-Data_apache' },
9191
{ text: '数据查询', link: 'Query-Data_apache' },
9292
{
9393
text: '数据删除',
@@ -210,7 +210,7 @@ export const zhSidebar = {
210210
prefix: 'SQL-Manual/',
211211
children: [
212212
{ text: '元数据操作', link: 'SQL-Metadata-Operations' },
213-
{ text: '数据增删', link: 'SQL-Data-Addition-Deletion' },
213+
{ text: '数据增删', link: 'SQL-Data-Addition-Deletion_apache' },
214214
{
215215
text: '数据查询',
216216
collapsible: true,

src/.vuepress/sidebar_timecho/V2.0.x/en-Table.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export const enSidebar = {
104104
children: [
105105
{ text: 'Database Management', link: 'Database-Management' },
106106
{ text: 'Table Management', link: 'Table-Management' },
107-
{ text: 'Write&Updata Data', link: 'Write-Updata-Data' },
107+
{ text: 'Write&Updata Data', link: 'Write-Updata-Data_timecho' },
108108
{ text: 'Query Data', link: 'Query-Data_timecho' },
109109
{
110110
text: 'Delete Data',
@@ -232,7 +232,7 @@ export const enSidebar = {
232232
prefix: 'SQL-Manual/',
233233
children: [
234234
{ text: 'Metadata Operations', link: 'SQL-Metadata-Operations' },
235-
{ text: 'Data Addition&Deletion', link: 'SQL-Data-Addition-Deletion' },
235+
{ text: 'Data Addition&Deletion', link: 'SQL-Data-Addition-Deletion_timecho' },
236236
{
237237
text: 'Data Query',
238238
collapsible: true,

src/.vuepress/sidebar_timecho/V2.0.x/zh-Table.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export const zhSidebar = {
9595
children: [
9696
{ text: '数据库管理', link: 'Database-Management' },
9797
{ text: '表管理', link: 'Table-Management' },
98-
{ text: '写入&更新', link: 'Write-Updata-Data' },
98+
{ text: '写入&更新', link: 'Write-Updata-Data_timecho' },
9999
{ text: '数据查询', link: 'Query-Data_timecho' },
100100
{
101101
text: '数据删除',
@@ -223,7 +223,7 @@ export const zhSidebar = {
223223
prefix: 'SQL-Manual/',
224224
children: [
225225
{ text: '元数据操作', link: 'SQL-Metadata-Operations' },
226-
{ text: '数据增删', link: 'SQL-Data-Addition-Deletion' },
226+
{ text: '数据增删', link: 'SQL-Data-Addition-Deletion_timecho' },
227227
{
228228
text: '数据查询',
229229
collapsible: true,
Lines changed: 3 additions & 189 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
---
2+
redirectTo: Write-Updata-Data_apache.html
3+
---
14
<!--
25
36
Licensed to the Apache Software Foundation (ASF) under one
@@ -18,192 +21,3 @@
1821
under the License.
1922
2023
-->
21-
22-
# Write & Update Data
23-
24-
## 1. Data Insertion
25-
26-
### 1.1 Syntax
27-
28-
In IoTDB, data insertion follows the general syntax:
29-
30-
```SQL
31-
INSERT INTO <TABLE_NAME> [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*)
32-
```
33-
34-
**Basic Constraints**:
35-
36-
1. Tables cannot be automatically created using `INSERT` statements.
37-
2. Columns not specified in the `INSERT` statement will automatically be filled with `null`.
38-
3. If no timestamp is provided, the system will use the current time (`now()`).
39-
4. If a column value does not exist for the identified device, the insertion will overwrite any existing `null` values with the new data.
40-
5. If a column value already exists for the identified device, a new insertion will update the column with the new value.
41-
6. Writing duplicate timestamps will update the values in the columns corresponding to the original timestamps.
42-
7. When an INSERT statement does not specify column names (e.g., INSERT INTO table VALUES (...)), the values in VALUES must strictly follow the physical order of columns in the table (this order can be checked via the DESC table command).
43-
44-
Since attributes generally do not change over time, it is recommended to update attribute values using the `UPDATE` statement described below,Please refer to the following [Data Update](#2-data-updates).
45-
46-
<div style="text-align: center;">
47-
<img src="/img/%E6%95%B0%E6%8D%AE%E5%86%99%E5%85%A5%E8%8B%B1%E6%96%87.png" alt="" style="width: 70%;"/>
48-
</div>
49-
50-
51-
### 1.2 Automatically Create Tables via Session Insertion
52-
53-
When performing data writing through Session, IoTDB supports schema-less writing: there is no need to manually create tables beforehand. The system automatically constructs the table structure based on the information in the write request, and then directly executes the data writing operation.
54-
55-
**Example:**
56-
57-
```Java
58-
try (ITableSession session =
59-
new TableSessionBuilder()
60-
.nodeUrls(Collections.singletonList("127.0.0.1:6667"))
61-
.username("root")
62-
.password("root")
63-
.build()) {
64-
65-
session.executeNonQueryStatement("CREATE DATABASE db1");
66-
session.executeNonQueryStatement("use db1");
67-
68-
// Insert data without manually creating the table
69-
List<String> columnNameList =
70-
Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity");
71-
List<TSDataType> dataTypeList =
72-
Arrays.asList(
73-
TSDataType.STRING,
74-
TSDataType.STRING,
75-
TSDataType.STRING,
76-
TSDataType.STRING,
77-
TSDataType.FLOAT,
78-
TSDataType.DOUBLE);
79-
List<ColumnCategory> columnTypeList =
80-
new ArrayList<>(
81-
Arrays.asList(
82-
ColumnCategory.TAG,
83-
ColumnCategory.TAG,
84-
ColumnCategory.TAG,
85-
ColumnCategory.ATTRIBUTE,
86-
ColumnCategory.FIELD,
87-
ColumnCategory.FIELD));
88-
Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100);
89-
for (long timestamp = 0; timestamp < 100; timestamp++) {
90-
int rowIndex = tablet.getRowSize();
91-
tablet.addTimestamp(rowIndex, timestamp);
92-
tablet.addValue("region_id", rowIndex, "1");
93-
tablet.addValue("plant_id", rowIndex, "5");
94-
tablet.addValue("device_id", rowIndex, "3");
95-
tablet.addValue("model", rowIndex, "A");
96-
tablet.addValue("temperature", rowIndex, 37.6F);
97-
tablet.addValue("humidity", rowIndex, 111.1);
98-
if (tablet.getRowSize() == tablet.getMaxRowNumber()) {
99-
session.insert(tablet);
100-
tablet.reset();
101-
}
102-
}
103-
if (tablet.getRowSize() != 0) {
104-
session.insert(tablet);
105-
tablet.reset();
106-
}
107-
}
108-
```
109-
110-
After execution, you can verify the table creation using the following command:
111-
112-
```SQL
113-
IoTDB> desc table1
114-
+-----------+---------+-----------+
115-
| ColumnName| DataType| Category|
116-
+-----------+---------+-----------+
117-
| time|TIMESTAMP| TIME|
118-
| region_id| STRING| TAG|
119-
| plant_id| STRING| TAG|
120-
| device_id| STRING| TAG|
121-
| model| STRING| ATTRIBUTE|
122-
|temperature| FLOAT| FIELD|
123-
| humidity| DOUBLE| FIELD|
124-
+-----------+---------+-----------+
125-
```
126-
127-
### 1.3 Specified Column Insertion
128-
129-
It is possible to insert data for specific columns. Columns not specified will remain `null`.
130-
131-
**Example:**
132-
133-
```SQL
134-
insert into table1("region", "plant_id", "device_id", Time, "temperature", "displacement") values ('Hamburg', '3001', '3', 4, 90.0, 1200.0)
135-
136-
137-
insert into table1("region", "plant_id", "device_id", Time, "temperature") values ('Hamburg, '3001', '3', 5, 90.0)
138-
```
139-
140-
### 1.4 Null Value Insertion
141-
142-
You can explicitly set `null` values for tag columns, attribute columns, and field columns.
143-
144-
**Example**:
145-
146-
Equivalent to the above partial column insertion.
147-
148-
```SQL
149-
# Equivalent to the example above
150-
insert into table1("region", "plant_id", "device_id", "model", "maintenance_cycle", Time, "temperature", "displacement") values ('Hamburg', '3001', '3', null, null, 4, 90.0, 1200.0)
151-
152-
insert into table1("region", "plant_id", "device_id", "model", "maintenance_cycle", Time, "temperature", "displacement") values ('Hamburg', '3001', '3', null, null, 5, 90.0, null)
153-
```
154-
155-
If no tag columns are included, the system will automatically create a device with all tag column values set to `null`.
156-
157-
> **Note:** This operation will not only automatically populate existing tag columns in the table with `null` values but will also populate any newly added tag columns with `null` values in the future.
158-
159-
### 1.5 Multi-Row Insertion
160-
161-
IoTDB supports inserting multiple rows of data in a single statement to improve efficiency.
162-
163-
**Example**:
164-
165-
```SQL
166-
insert into table1
167-
values
168-
(4, 'Frankfurt', '3001', '3', '1', '10', 90.0, 1200.0)
169-
(5, 'Frankfurt', '3001', '3', '1', '10', 90.0, 1200.0)
170-
171-
172-
insert into table1
173-
("region", "plant_id", "device_id", Time, "temperature", "displacement")
174-
values
175-
('Frankfurt', '3001', '3', 4, 90.0, 1200.0)
176-
('Frankfurt', '3001', '3', 5, 90.0, 1200.0)
177-
```
178-
179-
#### Notes
180-
181-
- Referencing non-existent columns in SQL will result in an error code `COLUMN_NOT_EXIST(616)`.
182-
- Data type mismatches between the insertion data and the column's data type will result in an error code `DATA_TYPE_MISMATCH(614)`.
183-
184-
185-
## 2. Data Updates
186-
187-
### 2.1 Syntax
188-
189-
```SQL
190-
UPDATE <TABLE_NAME> SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)?
191-
192-
updateAssignment
193-
: identifier EQ expression
194-
;
195-
```
196-
197-
**Note:**
198-
199-
- Updates are allowed only on `ATTRIBUTE` columns.
200-
- `WHERE` conditions:
201-
- Can only include `TAG` and `ATTRIBUTE` columns; `FIELD` and `TIME` columns are not allowed.
202-
- Aggregation functions are not supported.
203-
- The result of the `SET` assignment expression must be a `string` type and follow the same constraints as the `WHERE` clause.
204-
205-
**Example**:
206-
207-
```SQL
208-
update table1 set b = a where substring(a, 1, 1) like '%'
209-
```

0 commit comments

Comments
 (0)