Skip to content

Commit c5af57d

Browse files
authored
adjust data insert content in table (#1103)
1 parent 5036fca commit c5af57d

8 files changed

Lines changed: 710 additions & 706 deletions

File tree

src/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_apache.md

Lines changed: 87 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
# Write & Update Data
2323

24-
## 1. Data Insertion
24+
## 1. SQL Insertion
2525

2626
### 1.1 Syntax
2727

@@ -47,86 +47,7 @@ Since attributes generally do not change over time, it is recommended to update
4747
<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%;"/>
4848
</div>
4949

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-
desc table1;
114-
```
115-
```shell
116-
+-----------+---------+-----------+
117-
| ColumnName| DataType| Category|
118-
+-----------+---------+-----------+
119-
| time|TIMESTAMP| TIME|
120-
| region_id| STRING| TAG|
121-
| plant_id| STRING| TAG|
122-
| device_id| STRING| TAG|
123-
| model| STRING| ATTRIBUTE|
124-
|temperature| FLOAT| FIELD|
125-
| humidity| DOUBLE| FIELD|
126-
+-----------+---------+-----------+
127-
```
128-
129-
### 1.3 Specified Column Insertion
50+
### 1.2 Specified Column Insertion
13051

13152
It is possible to insert data for specific columns. Columns not specified will remain `null`.
13253

@@ -138,7 +59,7 @@ INSERT INTO table1(region, plant_id, device_id, time, temperature, humidity) VAL
13859
INSERT INTO table1(region, plant_id, device_id, time, temperature) VALUES ('Hamburg', '1001', '100', '2025-11-26 13:38:00', 91.0);
13960
```
14061

141-
### 1.4 Null Value Insertion
62+
### 1.3 Null Value Insertion
14263

14364
You can explicitly set `null` values for tag columns, attribute columns, and field columns.
14465

@@ -157,7 +78,7 @@ If no tag columns are included, the system will automatically create a device wi
15778

15879
> **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.
15980
160-
### 1.5 Multi-Row Insertion
81+
### 1.4 Multi-Row Insertion
16182

16283
IoTDB supports inserting multiple rows of data in a single statement to improve efficiency.
16384

@@ -182,13 +103,13 @@ VALUES
182103
- Data type mismatches between the insertion data and the column's data type will result in an error code `DATA_TYPE_MISMATCH(614)`.
183104

184105

185-
### 1.6 Query Write-back
106+
### 1.5 Query Write-back
186107

187108
The IoTDB table model supports the **append-only query write-back** feature, implemented via the `INSERT INTO QUERY` statement. This feature allows writing the results of a query into an **existing** table.
188109

189110
> **Note**​: This feature is available starting from version V2.0.6.
190111
191-
#### 1.6.1 Syntax Definition
112+
#### 1.5.1 Syntax Definition
192113

193114
sql
194115

@@ -297,7 +218,7 @@ Total line number = 2
297218
It costs 0.014s
298219
```
299220

300-
#### 1.6.2 Notes
221+
#### 1.5.2 Notes
301222

302223
* The source table in the `query` and the target table `table_name` are allowed to be the same table, e.g., `INSERT INTO testtb SELECT * FROM testtb`.
303224
* The target table ​**must already exist**​; otherwise, the error message `550: Table 'xxx.xxx' does not exist` will be thrown.
@@ -316,9 +237,87 @@ It costs 0.014s
316237
* For more details about user permissions, refer to [Authority Management](../User-Manual/Authority-Management_apache.md).
317238

318239

319-
## 2. Data Updates
240+
## 2. Schema-less Writing
241+
242+
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.
243+
244+
**Example:**
245+
246+
```Java
247+
try (ITableSession session =
248+
new TableSessionBuilder()
249+
.nodeUrls(Collections.singletonList("127.0.0.1:6667"))
250+
.username("root")
251+
.password("root")
252+
.build()) {
253+
254+
session.executeNonQueryStatement("CREATE DATABASE db1");
255+
session.executeNonQueryStatement("use db1");
256+
257+
// Insert data without manually creating the table
258+
List<String> columnNameList =
259+
Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity");
260+
List<TSDataType> dataTypeList =
261+
Arrays.asList(
262+
TSDataType.STRING,
263+
TSDataType.STRING,
264+
TSDataType.STRING,
265+
TSDataType.STRING,
266+
TSDataType.FLOAT,
267+
TSDataType.DOUBLE);
268+
List<ColumnCategory> columnTypeList =
269+
new ArrayList<>(
270+
Arrays.asList(
271+
ColumnCategory.TAG,
272+
ColumnCategory.TAG,
273+
ColumnCategory.TAG,
274+
ColumnCategory.ATTRIBUTE,
275+
ColumnCategory.FIELD,
276+
ColumnCategory.FIELD));
277+
Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100);
278+
for (long timestamp = 0; timestamp < 100; timestamp++) {
279+
int rowIndex = tablet.getRowSize();
280+
tablet.addTimestamp(rowIndex, timestamp);
281+
tablet.addValue("region_id", rowIndex, "1");
282+
tablet.addValue("plant_id", rowIndex, "5");
283+
tablet.addValue("device_id", rowIndex, "3");
284+
tablet.addValue("model", rowIndex, "A");
285+
tablet.addValue("temperature", rowIndex, 37.6F);
286+
tablet.addValue("humidity", rowIndex, 111.1);
287+
if (tablet.getRowSize() == tablet.getMaxRowNumber()) {
288+
session.insert(tablet);
289+
tablet.reset();
290+
}
291+
}
292+
if (tablet.getRowSize() != 0) {
293+
session.insert(tablet);
294+
tablet.reset();
295+
}
296+
}
297+
```
298+
299+
After execution, you can verify the table creation using the following command:
300+
301+
```SQL
302+
desc table1;
303+
```
304+
```shell
305+
+-----------+---------+-----------+
306+
| ColumnName| DataType| Category|
307+
+-----------+---------+-----------+
308+
| time|TIMESTAMP| TIME|
309+
| region_id| STRING| TAG|
310+
| plant_id| STRING| TAG|
311+
| device_id| STRING| TAG|
312+
| model| STRING| ATTRIBUTE|
313+
|temperature| FLOAT| FIELD|
314+
| humidity| DOUBLE| FIELD|
315+
+-----------+---------+-----------+
316+
```
317+
318+
## 3. Data Updates
320319

321-
### 2.1 Syntax
320+
### 3.1 Syntax
322321

323322
```SQL
324323
UPDATE <TABLE_NAME> SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)?

0 commit comments

Comments
 (0)