From 8f086918b8ec3ff9d5eebd740a7ed45f72ee4236 Mon Sep 17 00:00:00 2001 From: Leto_b Date: Mon, 27 Apr 2026 14:45:43 +0800 Subject: [PATCH] adjust data insert content in table --- .../Basic-Concept/Write-Updata-Data_apache.md | 175 +++++++++-------- .../Write-Updata-Data_timecho.md | 178 +++++++++--------- .../Basic-Concept/Write-Updata-Data_apache.md | 175 +++++++++-------- .../Write-Updata-Data_timecho.md | 178 +++++++++--------- .../Basic-Concept/Write-Updata-Data_apache.md | 177 ++++++++--------- .../Write-Updata-Data_timecho.md | 178 +++++++++--------- .../Basic-Concept/Write-Updata-Data_apache.md | 177 ++++++++--------- .../Write-Updata-Data_timecho.md | 178 +++++++++--------- 8 files changed, 710 insertions(+), 706 deletions(-) diff --git a/src/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_apache.md b/src/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_apache.md index 51bfa69df..a48678be9 100644 --- a/src/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_apache.md +++ b/src/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_apache.md @@ -21,7 +21,7 @@ # Write & Update Data -## 1. Data Insertion +## 1. SQL Insertion ### 1.1 Syntax @@ -47,86 +47,7 @@ Since attributes generally do not change over time, it is recommended to update - -### 1.2 Automatically Create Tables via Session Insertion - -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. - -**Example:** - -```Java -try (ITableSession session = - new TableSessionBuilder() - .nodeUrls(Collections.singletonList("127.0.0.1:6667")) - .username("root") - .password("root") - .build()) { - - session.executeNonQueryStatement("CREATE DATABASE db1"); - session.executeNonQueryStatement("use db1"); - - // Insert data without manually creating the table - List columnNameList = - Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); - List dataTypeList = - Arrays.asList( - TSDataType.STRING, - TSDataType.STRING, - TSDataType.STRING, - TSDataType.STRING, - TSDataType.FLOAT, - TSDataType.DOUBLE); - List columnTypeList = - new ArrayList<>( - Arrays.asList( - ColumnCategory.TAG, - ColumnCategory.TAG, - ColumnCategory.TAG, - ColumnCategory.ATTRIBUTE, - ColumnCategory.FIELD, - ColumnCategory.FIELD)); - Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); - for (long timestamp = 0; timestamp < 100; timestamp++) { - int rowIndex = tablet.getRowSize(); - tablet.addTimestamp(rowIndex, timestamp); - tablet.addValue("region_id", rowIndex, "1"); - tablet.addValue("plant_id", rowIndex, "5"); - tablet.addValue("device_id", rowIndex, "3"); - tablet.addValue("model", rowIndex, "A"); - tablet.addValue("temperature", rowIndex, 37.6F); - tablet.addValue("humidity", rowIndex, 111.1); - if (tablet.getRowSize() == tablet.getMaxRowNumber()) { - session.insert(tablet); - tablet.reset(); - } - } - if (tablet.getRowSize() != 0) { - session.insert(tablet); - tablet.reset(); - } -} -``` - -After execution, you can verify the table creation using the following command: - -```SQL -desc table1; -``` -```shell -+-----------+---------+-----------+ -| ColumnName| DataType| Category| -+-----------+---------+-----------+ -| time|TIMESTAMP| TIME| -| region_id| STRING| TAG| -| plant_id| STRING| TAG| -| device_id| STRING| TAG| -| model| STRING| ATTRIBUTE| -|temperature| FLOAT| FIELD| -| humidity| DOUBLE| FIELD| -+-----------+---------+-----------+ -``` - -### 1.3 Specified Column Insertion +### 1.2 Specified Column Insertion It is possible to insert data for specific columns. Columns not specified will remain `null`. @@ -138,7 +59,7 @@ INSERT INTO table1(region, plant_id, device_id, time, temperature, humidity) VAL INSERT INTO table1(region, plant_id, device_id, time, temperature) VALUES ('Hamburg', '1001', '100', '2025-11-26 13:38:00', 91.0); ``` -### 1.4 Null Value Insertion +### 1.3 Null Value Insertion You can explicitly set `null` values for tag columns, attribute columns, and field columns. @@ -157,7 +78,7 @@ If no tag columns are included, the system will automatically create a device wi > **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. -### 1.5 Multi-Row Insertion +### 1.4 Multi-Row Insertion IoTDB supports inserting multiple rows of data in a single statement to improve efficiency. @@ -182,13 +103,13 @@ VALUES - Data type mismatches between the insertion data and the column's data type will result in an error code `DATA_TYPE_MISMATCH(614)`. -### 1.6 Query Write-back +### 1.5 Query Write-back 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. > ​**Note**​: This feature is available starting from version V2.0.6. -#### 1.6.1 Syntax Definition +#### 1.5.1 Syntax Definition sql @@ -297,7 +218,7 @@ Total line number = 2 It costs 0.014s ``` -#### 1.6.2 Notes +#### 1.5.2 Notes * 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`. * 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 * For more details about user permissions, refer to [Authority Management](../User-Manual/Authority-Management_apache.md). -## 2. Data Updates +## 2. Schema-less Writing + +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. + +**Example:** + +```Java +try (ITableSession session = + new TableSessionBuilder() + .nodeUrls(Collections.singletonList("127.0.0.1:6667")) + .username("root") + .password("root") + .build()) { + + session.executeNonQueryStatement("CREATE DATABASE db1"); + session.executeNonQueryStatement("use db1"); + + // Insert data without manually creating the table + List columnNameList = + Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); + List dataTypeList = + Arrays.asList( + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.FLOAT, + TSDataType.DOUBLE); + List columnTypeList = + new ArrayList<>( + Arrays.asList( + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.ATTRIBUTE, + ColumnCategory.FIELD, + ColumnCategory.FIELD)); + Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); + for (long timestamp = 0; timestamp < 100; timestamp++) { + int rowIndex = tablet.getRowSize(); + tablet.addTimestamp(rowIndex, timestamp); + tablet.addValue("region_id", rowIndex, "1"); + tablet.addValue("plant_id", rowIndex, "5"); + tablet.addValue("device_id", rowIndex, "3"); + tablet.addValue("model", rowIndex, "A"); + tablet.addValue("temperature", rowIndex, 37.6F); + tablet.addValue("humidity", rowIndex, 111.1); + if (tablet.getRowSize() == tablet.getMaxRowNumber()) { + session.insert(tablet); + tablet.reset(); + } + } + if (tablet.getRowSize() != 0) { + session.insert(tablet); + tablet.reset(); + } +} +``` + +After execution, you can verify the table creation using the following command: + +```SQL +desc table1; +``` +```shell ++-----------+---------+-----------+ +| ColumnName| DataType| Category| ++-----------+---------+-----------+ +| time|TIMESTAMP| TIME| +| region_id| STRING| TAG| +| plant_id| STRING| TAG| +| device_id| STRING| TAG| +| model| STRING| ATTRIBUTE| +|temperature| FLOAT| FIELD| +| humidity| DOUBLE| FIELD| ++-----------+---------+-----------+ +``` + +## 3. Data Updates -### 2.1 Syntax +### 3.1 Syntax ```SQL UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? diff --git a/src/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_timecho.md b/src/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_timecho.md index e50b2d08c..38436f17d 100644 --- a/src/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_timecho.md +++ b/src/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_timecho.md @@ -21,7 +21,7 @@ # Write & Update Data -## 1. Data Insertion +## 1. SQL Insertion ### 1.1 Syntax @@ -48,85 +48,7 @@ Since attributes generally do not change over time, it is recommended to update -### 1.2 Automatically Create Tables via Session Insertion - -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. - -**Example:** - -```Java -try (ITableSession session = - new TableSessionBuilder() - .nodeUrls(Collections.singletonList("127.0.0.1:6667")) - .username("root") - .password("root") - .build()) { - - session.executeNonQueryStatement("CREATE DATABASE db1"); - session.executeNonQueryStatement("use db1"); - - // Insert data without manually creating the table - List columnNameList = - Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); - List dataTypeList = - Arrays.asList( - TSDataType.STRING, - TSDataType.STRING, - TSDataType.STRING, - TSDataType.STRING, - TSDataType.FLOAT, - TSDataType.DOUBLE); - List columnTypeList = - new ArrayList<>( - Arrays.asList( - ColumnCategory.TAG, - ColumnCategory.TAG, - ColumnCategory.TAG, - ColumnCategory.ATTRIBUTE, - ColumnCategory.FIELD, - ColumnCategory.FIELD)); - Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); - for (long timestamp = 0; timestamp < 100; timestamp++) { - int rowIndex = tablet.getRowSize(); - tablet.addTimestamp(rowIndex, timestamp); - tablet.addValue("region_id", rowIndex, "1"); - tablet.addValue("plant_id", rowIndex, "5"); - tablet.addValue("device_id", rowIndex, "3"); - tablet.addValue("model", rowIndex, "A"); - tablet.addValue("temperature", rowIndex, 37.6F); - tablet.addValue("humidity", rowIndex, 111.1); - if (tablet.getRowSize() == tablet.getMaxRowNumber()) { - session.insert(tablet); - tablet.reset(); - } - } - if (tablet.getRowSize() != 0) { - session.insert(tablet); - tablet.reset(); - } -} -``` - -After execution, you can verify the table creation using the following command: - -```SQL -desc table1; -``` -```shell -+-----------+---------+-----------+ -| ColumnName| DataType| Category| -+-----------+---------+-----------+ -| time|TIMESTAMP| TIME| -| region_id| STRING| TAG| -| plant_id| STRING| TAG| -| device_id| STRING| TAG| -| model| STRING| ATTRIBUTE| -|temperature| FLOAT| FIELD| -| humidity| DOUBLE| FIELD| -+-----------+---------+-----------+ -``` - -### 1.3 Specified Column Insertion +### 1.2 Specified Column Insertion It is possible to insert data for specific columns. Columns not specified will remain `null`. @@ -138,7 +60,7 @@ INSERT INTO table1(region, plant_id, device_id, time, temperature, humidity) VAL INSERT INTO table1(region, plant_id, device_id, time, temperature) VALUES ('Hamburg', '1001', '100', '2025-11-26 13:38:00', 91.0); ``` -### 1.4 Null Value Insertion +### 1.3 Null Value Insertion You can explicitly set `null` values for tag columns, attribute columns, and field columns. @@ -157,7 +79,7 @@ If no tag columns are included, the system will automatically create a device wi > **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. -### 1.5 Multi-Row Insertion +### 1.4 Multi-Row Insertion IoTDB supports inserting multiple rows of data in a single statement to improve efficiency. @@ -182,13 +104,13 @@ VALUES - Data type mismatches between the insertion data and the column's data type will result in an error code `DATA_TYPE_MISMATCH(614)`. -### 1.6 Query Write-back +### 1.5 Query Write-back 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. > ​**Note**​: This feature is available starting from version V2.0.6. -#### 1.6.1 Syntax Definition +#### 1.5.1 Syntax Definition sql @@ -297,7 +219,7 @@ Total line number = 2 It costs 0.014s ``` -#### 1.6.2 Notes +#### 1.5.2 Notes * 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`. * The target table ​**must already exist**​; otherwise, the error message `550: Table 'xxx.xxx' does not exist` will be thrown. @@ -316,7 +238,7 @@ It costs 0.014s * For more details about user permissions, refer to [Authority Management](../User-Manual/Authority-Management_timecho.md). -### 1.7 Writing Object Type +### 1.6 Writing Object Type To avoid oversized Object write requests, values of **Object** type can be split into segments and written sequentially. In SQL, the `to_object(isEOF, offset, content)` function must be used for value insertion. @@ -369,9 +291,89 @@ INSERT INTO table1(time, device_id, s1) VALUES(1, 'tag1', TO_OBJECT(TRUE, 4, X'6 2. During segmented writes, if the `offset` of the current write does not match the current size of the Object, the write operation will fail. 3. If `offset=0` is used after partial writes, the existing content will be overwritten with new data. -## 2. Data Updates -### 2.1 Syntax +## 2. Schema-less Writing + +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. + +**Example:** + +```Java +try (ITableSession session = + new TableSessionBuilder() + .nodeUrls(Collections.singletonList("127.0.0.1:6667")) + .username("root") + .password("root") + .build()) { + + session.executeNonQueryStatement("CREATE DATABASE db1"); + session.executeNonQueryStatement("use db1"); + + // Insert data without manually creating the table + List columnNameList = + Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); + List dataTypeList = + Arrays.asList( + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.FLOAT, + TSDataType.DOUBLE); + List columnTypeList = + new ArrayList<>( + Arrays.asList( + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.ATTRIBUTE, + ColumnCategory.FIELD, + ColumnCategory.FIELD)); + Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); + for (long timestamp = 0; timestamp < 100; timestamp++) { + int rowIndex = tablet.getRowSize(); + tablet.addTimestamp(rowIndex, timestamp); + tablet.addValue("region_id", rowIndex, "1"); + tablet.addValue("plant_id", rowIndex, "5"); + tablet.addValue("device_id", rowIndex, "3"); + tablet.addValue("model", rowIndex, "A"); + tablet.addValue("temperature", rowIndex, 37.6F); + tablet.addValue("humidity", rowIndex, 111.1); + if (tablet.getRowSize() == tablet.getMaxRowNumber()) { + session.insert(tablet); + tablet.reset(); + } + } + if (tablet.getRowSize() != 0) { + session.insert(tablet); + tablet.reset(); + } +} +``` + +After execution, you can verify the table creation using the following command: + +```SQL +desc table1; +``` +```shell ++-----------+---------+-----------+ +| ColumnName| DataType| Category| ++-----------+---------+-----------+ +| time|TIMESTAMP| TIME| +| region_id| STRING| TAG| +| plant_id| STRING| TAG| +| device_id| STRING| TAG| +| model| STRING| ATTRIBUTE| +|temperature| FLOAT| FIELD| +| humidity| DOUBLE| FIELD| ++-----------+---------+-----------+ +``` + + +## 3. Data Updates + +### 3.1 Syntax ```SQL UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? diff --git a/src/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_apache.md b/src/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_apache.md index 51bfa69df..a48678be9 100644 --- a/src/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_apache.md +++ b/src/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_apache.md @@ -21,7 +21,7 @@ # Write & Update Data -## 1. Data Insertion +## 1. SQL Insertion ### 1.1 Syntax @@ -47,86 +47,7 @@ Since attributes generally do not change over time, it is recommended to update - -### 1.2 Automatically Create Tables via Session Insertion - -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. - -**Example:** - -```Java -try (ITableSession session = - new TableSessionBuilder() - .nodeUrls(Collections.singletonList("127.0.0.1:6667")) - .username("root") - .password("root") - .build()) { - - session.executeNonQueryStatement("CREATE DATABASE db1"); - session.executeNonQueryStatement("use db1"); - - // Insert data without manually creating the table - List columnNameList = - Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); - List dataTypeList = - Arrays.asList( - TSDataType.STRING, - TSDataType.STRING, - TSDataType.STRING, - TSDataType.STRING, - TSDataType.FLOAT, - TSDataType.DOUBLE); - List columnTypeList = - new ArrayList<>( - Arrays.asList( - ColumnCategory.TAG, - ColumnCategory.TAG, - ColumnCategory.TAG, - ColumnCategory.ATTRIBUTE, - ColumnCategory.FIELD, - ColumnCategory.FIELD)); - Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); - for (long timestamp = 0; timestamp < 100; timestamp++) { - int rowIndex = tablet.getRowSize(); - tablet.addTimestamp(rowIndex, timestamp); - tablet.addValue("region_id", rowIndex, "1"); - tablet.addValue("plant_id", rowIndex, "5"); - tablet.addValue("device_id", rowIndex, "3"); - tablet.addValue("model", rowIndex, "A"); - tablet.addValue("temperature", rowIndex, 37.6F); - tablet.addValue("humidity", rowIndex, 111.1); - if (tablet.getRowSize() == tablet.getMaxRowNumber()) { - session.insert(tablet); - tablet.reset(); - } - } - if (tablet.getRowSize() != 0) { - session.insert(tablet); - tablet.reset(); - } -} -``` - -After execution, you can verify the table creation using the following command: - -```SQL -desc table1; -``` -```shell -+-----------+---------+-----------+ -| ColumnName| DataType| Category| -+-----------+---------+-----------+ -| time|TIMESTAMP| TIME| -| region_id| STRING| TAG| -| plant_id| STRING| TAG| -| device_id| STRING| TAG| -| model| STRING| ATTRIBUTE| -|temperature| FLOAT| FIELD| -| humidity| DOUBLE| FIELD| -+-----------+---------+-----------+ -``` - -### 1.3 Specified Column Insertion +### 1.2 Specified Column Insertion It is possible to insert data for specific columns. Columns not specified will remain `null`. @@ -138,7 +59,7 @@ INSERT INTO table1(region, plant_id, device_id, time, temperature, humidity) VAL INSERT INTO table1(region, plant_id, device_id, time, temperature) VALUES ('Hamburg', '1001', '100', '2025-11-26 13:38:00', 91.0); ``` -### 1.4 Null Value Insertion +### 1.3 Null Value Insertion You can explicitly set `null` values for tag columns, attribute columns, and field columns. @@ -157,7 +78,7 @@ If no tag columns are included, the system will automatically create a device wi > **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. -### 1.5 Multi-Row Insertion +### 1.4 Multi-Row Insertion IoTDB supports inserting multiple rows of data in a single statement to improve efficiency. @@ -182,13 +103,13 @@ VALUES - Data type mismatches between the insertion data and the column's data type will result in an error code `DATA_TYPE_MISMATCH(614)`. -### 1.6 Query Write-back +### 1.5 Query Write-back 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. > ​**Note**​: This feature is available starting from version V2.0.6. -#### 1.6.1 Syntax Definition +#### 1.5.1 Syntax Definition sql @@ -297,7 +218,7 @@ Total line number = 2 It costs 0.014s ``` -#### 1.6.2 Notes +#### 1.5.2 Notes * 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`. * 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 * For more details about user permissions, refer to [Authority Management](../User-Manual/Authority-Management_apache.md). -## 2. Data Updates +## 2. Schema-less Writing + +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. + +**Example:** + +```Java +try (ITableSession session = + new TableSessionBuilder() + .nodeUrls(Collections.singletonList("127.0.0.1:6667")) + .username("root") + .password("root") + .build()) { + + session.executeNonQueryStatement("CREATE DATABASE db1"); + session.executeNonQueryStatement("use db1"); + + // Insert data without manually creating the table + List columnNameList = + Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); + List dataTypeList = + Arrays.asList( + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.FLOAT, + TSDataType.DOUBLE); + List columnTypeList = + new ArrayList<>( + Arrays.asList( + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.ATTRIBUTE, + ColumnCategory.FIELD, + ColumnCategory.FIELD)); + Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); + for (long timestamp = 0; timestamp < 100; timestamp++) { + int rowIndex = tablet.getRowSize(); + tablet.addTimestamp(rowIndex, timestamp); + tablet.addValue("region_id", rowIndex, "1"); + tablet.addValue("plant_id", rowIndex, "5"); + tablet.addValue("device_id", rowIndex, "3"); + tablet.addValue("model", rowIndex, "A"); + tablet.addValue("temperature", rowIndex, 37.6F); + tablet.addValue("humidity", rowIndex, 111.1); + if (tablet.getRowSize() == tablet.getMaxRowNumber()) { + session.insert(tablet); + tablet.reset(); + } + } + if (tablet.getRowSize() != 0) { + session.insert(tablet); + tablet.reset(); + } +} +``` + +After execution, you can verify the table creation using the following command: + +```SQL +desc table1; +``` +```shell ++-----------+---------+-----------+ +| ColumnName| DataType| Category| ++-----------+---------+-----------+ +| time|TIMESTAMP| TIME| +| region_id| STRING| TAG| +| plant_id| STRING| TAG| +| device_id| STRING| TAG| +| model| STRING| ATTRIBUTE| +|temperature| FLOAT| FIELD| +| humidity| DOUBLE| FIELD| ++-----------+---------+-----------+ +``` + +## 3. Data Updates -### 2.1 Syntax +### 3.1 Syntax ```SQL UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? diff --git a/src/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_timecho.md b/src/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_timecho.md index e50b2d08c..38436f17d 100644 --- a/src/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_timecho.md +++ b/src/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_timecho.md @@ -21,7 +21,7 @@ # Write & Update Data -## 1. Data Insertion +## 1. SQL Insertion ### 1.1 Syntax @@ -48,85 +48,7 @@ Since attributes generally do not change over time, it is recommended to update -### 1.2 Automatically Create Tables via Session Insertion - -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. - -**Example:** - -```Java -try (ITableSession session = - new TableSessionBuilder() - .nodeUrls(Collections.singletonList("127.0.0.1:6667")) - .username("root") - .password("root") - .build()) { - - session.executeNonQueryStatement("CREATE DATABASE db1"); - session.executeNonQueryStatement("use db1"); - - // Insert data without manually creating the table - List columnNameList = - Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); - List dataTypeList = - Arrays.asList( - TSDataType.STRING, - TSDataType.STRING, - TSDataType.STRING, - TSDataType.STRING, - TSDataType.FLOAT, - TSDataType.DOUBLE); - List columnTypeList = - new ArrayList<>( - Arrays.asList( - ColumnCategory.TAG, - ColumnCategory.TAG, - ColumnCategory.TAG, - ColumnCategory.ATTRIBUTE, - ColumnCategory.FIELD, - ColumnCategory.FIELD)); - Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); - for (long timestamp = 0; timestamp < 100; timestamp++) { - int rowIndex = tablet.getRowSize(); - tablet.addTimestamp(rowIndex, timestamp); - tablet.addValue("region_id", rowIndex, "1"); - tablet.addValue("plant_id", rowIndex, "5"); - tablet.addValue("device_id", rowIndex, "3"); - tablet.addValue("model", rowIndex, "A"); - tablet.addValue("temperature", rowIndex, 37.6F); - tablet.addValue("humidity", rowIndex, 111.1); - if (tablet.getRowSize() == tablet.getMaxRowNumber()) { - session.insert(tablet); - tablet.reset(); - } - } - if (tablet.getRowSize() != 0) { - session.insert(tablet); - tablet.reset(); - } -} -``` - -After execution, you can verify the table creation using the following command: - -```SQL -desc table1; -``` -```shell -+-----------+---------+-----------+ -| ColumnName| DataType| Category| -+-----------+---------+-----------+ -| time|TIMESTAMP| TIME| -| region_id| STRING| TAG| -| plant_id| STRING| TAG| -| device_id| STRING| TAG| -| model| STRING| ATTRIBUTE| -|temperature| FLOAT| FIELD| -| humidity| DOUBLE| FIELD| -+-----------+---------+-----------+ -``` - -### 1.3 Specified Column Insertion +### 1.2 Specified Column Insertion It is possible to insert data for specific columns. Columns not specified will remain `null`. @@ -138,7 +60,7 @@ INSERT INTO table1(region, plant_id, device_id, time, temperature, humidity) VAL INSERT INTO table1(region, plant_id, device_id, time, temperature) VALUES ('Hamburg', '1001', '100', '2025-11-26 13:38:00', 91.0); ``` -### 1.4 Null Value Insertion +### 1.3 Null Value Insertion You can explicitly set `null` values for tag columns, attribute columns, and field columns. @@ -157,7 +79,7 @@ If no tag columns are included, the system will automatically create a device wi > **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. -### 1.5 Multi-Row Insertion +### 1.4 Multi-Row Insertion IoTDB supports inserting multiple rows of data in a single statement to improve efficiency. @@ -182,13 +104,13 @@ VALUES - Data type mismatches between the insertion data and the column's data type will result in an error code `DATA_TYPE_MISMATCH(614)`. -### 1.6 Query Write-back +### 1.5 Query Write-back 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. > ​**Note**​: This feature is available starting from version V2.0.6. -#### 1.6.1 Syntax Definition +#### 1.5.1 Syntax Definition sql @@ -297,7 +219,7 @@ Total line number = 2 It costs 0.014s ``` -#### 1.6.2 Notes +#### 1.5.2 Notes * 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`. * The target table ​**must already exist**​; otherwise, the error message `550: Table 'xxx.xxx' does not exist` will be thrown. @@ -316,7 +238,7 @@ It costs 0.014s * For more details about user permissions, refer to [Authority Management](../User-Manual/Authority-Management_timecho.md). -### 1.7 Writing Object Type +### 1.6 Writing Object Type To avoid oversized Object write requests, values of **Object** type can be split into segments and written sequentially. In SQL, the `to_object(isEOF, offset, content)` function must be used for value insertion. @@ -369,9 +291,89 @@ INSERT INTO table1(time, device_id, s1) VALUES(1, 'tag1', TO_OBJECT(TRUE, 4, X'6 2. During segmented writes, if the `offset` of the current write does not match the current size of the Object, the write operation will fail. 3. If `offset=0` is used after partial writes, the existing content will be overwritten with new data. -## 2. Data Updates -### 2.1 Syntax +## 2. Schema-less Writing + +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. + +**Example:** + +```Java +try (ITableSession session = + new TableSessionBuilder() + .nodeUrls(Collections.singletonList("127.0.0.1:6667")) + .username("root") + .password("root") + .build()) { + + session.executeNonQueryStatement("CREATE DATABASE db1"); + session.executeNonQueryStatement("use db1"); + + // Insert data without manually creating the table + List columnNameList = + Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); + List dataTypeList = + Arrays.asList( + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.FLOAT, + TSDataType.DOUBLE); + List columnTypeList = + new ArrayList<>( + Arrays.asList( + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.ATTRIBUTE, + ColumnCategory.FIELD, + ColumnCategory.FIELD)); + Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); + for (long timestamp = 0; timestamp < 100; timestamp++) { + int rowIndex = tablet.getRowSize(); + tablet.addTimestamp(rowIndex, timestamp); + tablet.addValue("region_id", rowIndex, "1"); + tablet.addValue("plant_id", rowIndex, "5"); + tablet.addValue("device_id", rowIndex, "3"); + tablet.addValue("model", rowIndex, "A"); + tablet.addValue("temperature", rowIndex, 37.6F); + tablet.addValue("humidity", rowIndex, 111.1); + if (tablet.getRowSize() == tablet.getMaxRowNumber()) { + session.insert(tablet); + tablet.reset(); + } + } + if (tablet.getRowSize() != 0) { + session.insert(tablet); + tablet.reset(); + } +} +``` + +After execution, you can verify the table creation using the following command: + +```SQL +desc table1; +``` +```shell ++-----------+---------+-----------+ +| ColumnName| DataType| Category| ++-----------+---------+-----------+ +| time|TIMESTAMP| TIME| +| region_id| STRING| TAG| +| plant_id| STRING| TAG| +| device_id| STRING| TAG| +| model| STRING| ATTRIBUTE| +|temperature| FLOAT| FIELD| +| humidity| DOUBLE| FIELD| ++-----------+---------+-----------+ +``` + + +## 3. Data Updates + +### 3.1 Syntax ```SQL UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? diff --git a/src/zh/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_apache.md b/src/zh/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_apache.md index 24d2f5dbe..25fe37c70 100644 --- a/src/zh/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_apache.md +++ b/src/zh/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_apache.md @@ -21,7 +21,7 @@ # 写入&更新 -## 1. 数据写入 +## 1. SQL 写入 ### 1.1 语法 @@ -41,92 +41,14 @@ INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, 6. 写入重复时间戳,原时间戳对应列的值会更新。 7. 若 INSERT 语句未指定列名(如 INSERT INTO table VALUES (...)),则 VALUES中的值必须严格按表中列的物理顺序排列(顺序可通过 DESC table命令查看)。 -由于属性一般并不随时间的变化而变化,因此推荐以 update 的方式单独更新属性值,参见下文 [数据更新](#数据更新)。 +由于属性一般并不随时间的变化而变化,因此推荐以 update 的方式单独更新属性值,参见下文 [数据更新](#_3-数据更新)。
-### 1.2 通过 Session 写入自动创建表 - -在通过 Session 进行数据写入时,IoTDB 支持无模式写入:无需事先手动创建表,系统会根据写入请求中的信息自动构建表结构,之后直接执行数据写入操作。 - -**示例:** - -```Java -try (ITableSession session = - new TableSessionBuilder() - .nodeUrls(Collections.singletonList("127.0.0.1:6667")) - .username("root") - .password("root") - .build()) { - - session.executeNonQueryStatement("CREATE DATABASE db1"); - session.executeNonQueryStatement("use db1"); - - // 不创建表直接写入数据 - List columnNameList = - Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); - List dataTypeList = - Arrays.asList( - TSDataType.STRING, - TSDataType.STRING, - TSDataType.STRING, - TSDataType.STRING, - TSDataType.FLOAT, - TSDataType.DOUBLE); - List columnTypeList = - new ArrayList<>( - Arrays.asList( - ColumnCategory.TAG, - ColumnCategory.TAG, - ColumnCategory.TAG, - ColumnCategory.ATTRIBUTE, - ColumnCategory.FIELD, - ColumnCategory.FIELD)); - Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); - for (long timestamp = 0; timestamp < 100; timestamp++) { - int rowIndex = tablet.getRowSize(); - tablet.addTimestamp(rowIndex, timestamp); - tablet.addValue("region_id", rowIndex, "1"); - tablet.addValue("plant_id", rowIndex, "5"); - tablet.addValue("device_id", rowIndex, "3"); - tablet.addValue("model", rowIndex, "A"); - tablet.addValue("temperature", rowIndex, 37.6F); - tablet.addValue("humidity", rowIndex, 111.1); - if (tablet.getRowSize() == tablet.getMaxRowNumber()) { - session.insert(tablet); - tablet.reset(); - } - } - if (tablet.getRowSize() != 0) { - session.insert(tablet); - tablet.reset(); - } -} -``` - -在代码执行完成后,可以通过下述语句确认表已成功创建,其中包含了时间列、标签列、属性列以及测点列等各类信息。 - -```SQL -desc table1; -``` -```shell -+-----------+---------+-----------+ -| ColumnName| DataType| Category| -+-----------+---------+-----------+ -| time|TIMESTAMP| TIME| -| region_id| STRING| TAG| -| plant_id| STRING| TAG| -| device_id| STRING| TAG| -| model| STRING| ATTRIBUTE| -|temperature| FLOAT| FIELD| -| humidity| DOUBLE| FIELD| -+-----------+---------+-----------+ -``` - -### 1.3 指定列写入 +### 1.2 指定列写入 在写入操作中,可以指定部分列,未指定的列将不会被写入任何内容(即设置为 `null`)。 @@ -138,7 +60,7 @@ INSERT INTO table1(region, plant_id, device_id, time, temperature, humidity) VAL INSERT INTO table1(region, plant_id, device_id, time, temperature) VALUES ('北京', '1001', '100', '2025-11-26 13:38:00', 91.0); ``` -### 1.4 空值写入 +### 1.3 空值写入 标签列、属性列和测点列可以指定空值(`null`),表示不写入任何内容。 @@ -155,7 +77,7 @@ INSERT INTO table1(region, plant_id, device_id, model_id, maintenance, time, tem > 注意,该操作不仅会自动为表中已有的标签列填充 null 值,而且对于未来新增的标签列,同样会自动填充 null。 -### 1.5 多行写入 +### 1.4 多行写入 支持同时写入多行数据,提高数据写入效率。 @@ -179,13 +101,13 @@ VALUES - 如果在 SQL 语句中引用了表中不存在的列,IoTDB 将返回错误码 `COLUMN_NOT_EXIST(616)`。 - 如果写入的数据类型与表中列的数据类型不一致,将报错 `DATA_TYPE_MISMATCH(507)`。 -### 1.6 查询写回 +### 1.5 查询写回 IoTDB 表模型支持追加查询写回功能,即`INSERT INTO QUERY` 语句,支持将查询结果写入**已经存在**的表中。 > 注意:该功能从 V 2.0.6 版本开始提供。 -#### 1.6.1 语法定义 +#### 1.5.1 语法定义 ```SQL INSERT INTO table_name [ ( column [, ... ] ) ] query @@ -284,7 +206,7 @@ Total line number = 2 It costs 0.014s ``` -#### 1.6.2 相关说明 +#### 1.5.2 相关说明 * 允许 query 中的源表与目标表 table\_name 是同一个表,例如:`INSERT INTO testtb SELECT * FROM testtb`。 * 目标表必须已存在,否则提示错误信息`550: Table 'xxx.xxx' does not exist`。 @@ -303,9 +225,88 @@ It costs 0.014s * 更多用户权限相关的内容,请参考[权限管理](../User-Manual/Authority-Management_apache.md)。 -## 2. 数据更新 +## 2. 无模式写入 + +在通过 Session 进行数据写入时,IoTDB 支持无模式写入:无需事先手动创建表,系统会根据写入请求中的信息自动构建表结构,之后直接执行数据写入操作。 + +**示例:** + +```Java +try (ITableSession session = + new TableSessionBuilder() + .nodeUrls(Collections.singletonList("127.0.0.1:6667")) + .username("root") + .password("root") + .build()) { + + session.executeNonQueryStatement("CREATE DATABASE db1"); + session.executeNonQueryStatement("use db1"); + + // 不创建表直接写入数据 + List columnNameList = + Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); + List dataTypeList = + Arrays.asList( + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.FLOAT, + TSDataType.DOUBLE); + List columnTypeList = + new ArrayList<>( + Arrays.asList( + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.ATTRIBUTE, + ColumnCategory.FIELD, + ColumnCategory.FIELD)); + Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); + for (long timestamp = 0; timestamp < 100; timestamp++) { + int rowIndex = tablet.getRowSize(); + tablet.addTimestamp(rowIndex, timestamp); + tablet.addValue("region_id", rowIndex, "1"); + tablet.addValue("plant_id", rowIndex, "5"); + tablet.addValue("device_id", rowIndex, "3"); + tablet.addValue("model", rowIndex, "A"); + tablet.addValue("temperature", rowIndex, 37.6F); + tablet.addValue("humidity", rowIndex, 111.1); + if (tablet.getRowSize() == tablet.getMaxRowNumber()) { + session.insert(tablet); + tablet.reset(); + } + } + if (tablet.getRowSize() != 0) { + session.insert(tablet); + tablet.reset(); + } +} +``` + +在代码执行完成后,可以通过下述语句确认表已成功创建,其中包含了时间列、标签列、属性列以及测点列等各类信息。 + +```SQL +desc table1; +``` +```shell ++-----------+---------+-----------+ +| ColumnName| DataType| Category| ++-----------+---------+-----------+ +| time|TIMESTAMP| TIME| +| region_id| STRING| TAG| +| plant_id| STRING| TAG| +| device_id| STRING| TAG| +| model| STRING| ATTRIBUTE| +|temperature| FLOAT| FIELD| +| humidity| DOUBLE| FIELD| ++-----------+---------+-----------+ +``` + + +## 3. 数据更新 -### 2.1 语法 +### 3.1 语法 ```SQL UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? diff --git a/src/zh/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_timecho.md b/src/zh/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_timecho.md index 666e07dfc..58ea9299d 100644 --- a/src/zh/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_timecho.md +++ b/src/zh/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_timecho.md @@ -21,7 +21,7 @@ # 写入&更新 -## 1. 数据写入 +## 1.SQL 写入 ### 1.1 语法 @@ -41,92 +41,14 @@ INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, 6. 写入重复时间戳,原时间戳对应列的值会更新。 7. 若 INSERT 语句未指定列名(如 INSERT INTO table VALUES (...)),则 VALUES中的值必须严格按表中列的物理顺序排列(顺序可通过 DESC table命令查看)。 -由于属性一般并不随时间的变化而变化,因此推荐以 update 的方式单独更新属性值,参见下文 [数据更新](#数据更新)。 +由于属性一般并不随时间的变化而变化,因此推荐以 update 的方式单独更新属性值,参见下文 [数据更新](#_3-数据更新)。
-### 1.2 通过 Session 写入自动创建表 - -在通过 Session 进行数据写入时,IoTDB 支持无模式写入:无需事先手动创建表,系统会根据写入请求中的信息自动构建表结构,之后直接执行数据写入操作。 - -**示例:** - -```Java -try (ITableSession session = - new TableSessionBuilder() - .nodeUrls(Collections.singletonList("127.0.0.1:6667")) - .username("root") - .password("root") - .build()) { - - session.executeNonQueryStatement("CREATE DATABASE db1"); - session.executeNonQueryStatement("use db1"); - - // 不创建表直接写入数据 - List columnNameList = - Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); - List dataTypeList = - Arrays.asList( - TSDataType.STRING, - TSDataType.STRING, - TSDataType.STRING, - TSDataType.STRING, - TSDataType.FLOAT, - TSDataType.DOUBLE); - List columnTypeList = - new ArrayList<>( - Arrays.asList( - ColumnCategory.TAG, - ColumnCategory.TAG, - ColumnCategory.TAG, - ColumnCategory.ATTRIBUTE, - ColumnCategory.FIELD, - ColumnCategory.FIELD)); - Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); - for (long timestamp = 0; timestamp < 100; timestamp++) { - int rowIndex = tablet.getRowSize(); - tablet.addTimestamp(rowIndex, timestamp); - tablet.addValue("region_id", rowIndex, "1"); - tablet.addValue("plant_id", rowIndex, "5"); - tablet.addValue("device_id", rowIndex, "3"); - tablet.addValue("model", rowIndex, "A"); - tablet.addValue("temperature", rowIndex, 37.6F); - tablet.addValue("humidity", rowIndex, 111.1); - if (tablet.getRowSize() == tablet.getMaxRowNumber()) { - session.insert(tablet); - tablet.reset(); - } - } - if (tablet.getRowSize() != 0) { - session.insert(tablet); - tablet.reset(); - } -} -``` - -在代码执行完成后,可以通过下述语句确认表已成功创建,其中包含了时间列、标签列、属性列以及测点列等各类信息。 - -```SQL -desc table1; -``` -```shell -+-----------+---------+-----------+ -| ColumnName| DataType| Category| -+-----------+---------+-----------+ -| time|TIMESTAMP| TIME| -| region_id| STRING| TAG| -| plant_id| STRING| TAG| -| device_id| STRING| TAG| -| model| STRING| ATTRIBUTE| -|temperature| FLOAT| FIELD| -| humidity| DOUBLE| FIELD| -+-----------+---------+-----------+ -``` - -### 1.3 指定列写入 +### 1.2 指定列写入 在写入操作中,可以指定部分列,未指定的列将不会被写入任何内容(即设置为 `null`)。 @@ -138,7 +60,7 @@ INSERT INTO table1(region, plant_id, device_id, time, temperature, humidity) VAL INSERT INTO table1(region, plant_id, device_id, time, temperature) VALUES ('北京', '1001', '100', '2025-11-26 13:38:00', 91.0); ``` -### 1.4 空值写入 +### 1.3 空值写入 标签列、属性列和测点列可以指定空值(`null`),表示不写入任何内容。 @@ -155,7 +77,7 @@ INSERT INTO table1(region, plant_id, device_id, model_id, maintenance, time, tem > 注意,该操作不仅会自动为表中已有的标签列填充 null 值,而且对于未来新增的标签列,同样会自动填充 null。 -### 1.5 多行写入 +### 1.4 多行写入 支持同时写入多行数据,提高数据写入效率。 @@ -179,13 +101,13 @@ VALUES - 如果在 SQL 语句中引用了表中不存在的列,IoTDB 将返回错误码 `COLUMN_NOT_EXIST(616)`。 - 如果写入的数据类型与表中列的数据类型不一致,将报错 `DATA_TYPE_MISMATCH(507)`。 -### 1.6 查询写回 +### 1.5 查询写回 IoTDB 表模型支持追加查询写回功能,即`INSERT INTO QUERY` 语句,支持将查询结果写入**已经存在**的表中。 > 注意:该功能从 V 2.0.6 版本开始提供。 -#### 1.6.1 语法定义 +#### 1.5.1 语法定义 ```SQL INSERT INTO table_name [ ( column [, ... ] ) ] query @@ -284,7 +206,7 @@ Total line number = 2 It costs 0.014s ``` -#### 1.6.2 相关说明 +#### 1.5.2 相关说明 * 允许 query 中的源表与目标表 table\_name 是同一个表,例如:`INSERT INTO testtb SELECT * FROM testtb`。 * 目标表必须已存在,否则提示错误信息`550: Table 'xxx.xxx' does not exist`。 @@ -303,7 +225,7 @@ It costs 0.014s * 更多用户权限相关的内容,请参考[权限管理](../User-Manual/Authority-Management_timecho.md)。 -### 1.7 Object 类型写入 +### 1.6 Object 类型写入 自为了避免单个 Object 过大导致写入请求过大,Object 类型的值支持拆分后按顺序分段写入。SQL 中需要使用 `to_object(isEOF, offset, content)` 函数进行值填充。 @@ -356,10 +278,88 @@ insert into table1(time, device_id, s1) values(1, 'tag1', to_object(true, 4, X'6 3. 如果已写入了部分数据,本次写入的 offset 为 0,本次写入会清除之前已写入的数据部分,重新写入新的数据 +## 2. 无模式写入 + +在通过 Session 进行数据写入时,IoTDB 支持无模式写入:无需事先手动创建表,系统会根据写入请求中的信息自动构建表结构,之后直接执行数据写入操作。 + +**示例:** + +```Java +try (ITableSession session = + new TableSessionBuilder() + .nodeUrls(Collections.singletonList("127.0.0.1:6667")) + .username("root") + .password("root") + .build()) { + + session.executeNonQueryStatement("CREATE DATABASE db1"); + session.executeNonQueryStatement("use db1"); + + // 不创建表直接写入数据 + List columnNameList = + Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); + List dataTypeList = + Arrays.asList( + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.FLOAT, + TSDataType.DOUBLE); + List columnTypeList = + new ArrayList<>( + Arrays.asList( + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.ATTRIBUTE, + ColumnCategory.FIELD, + ColumnCategory.FIELD)); + Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); + for (long timestamp = 0; timestamp < 100; timestamp++) { + int rowIndex = tablet.getRowSize(); + tablet.addTimestamp(rowIndex, timestamp); + tablet.addValue("region_id", rowIndex, "1"); + tablet.addValue("plant_id", rowIndex, "5"); + tablet.addValue("device_id", rowIndex, "3"); + tablet.addValue("model", rowIndex, "A"); + tablet.addValue("temperature", rowIndex, 37.6F); + tablet.addValue("humidity", rowIndex, 111.1); + if (tablet.getRowSize() == tablet.getMaxRowNumber()) { + session.insert(tablet); + tablet.reset(); + } + } + if (tablet.getRowSize() != 0) { + session.insert(tablet); + tablet.reset(); + } +} +``` + +在代码执行完成后,可以通过下述语句确认表已成功创建,其中包含了时间列、标签列、属性列以及测点列等各类信息。 + +```SQL +desc table1; +``` +```shell ++-----------+---------+-----------+ +| ColumnName| DataType| Category| ++-----------+---------+-----------+ +| time|TIMESTAMP| TIME| +| region_id| STRING| TAG| +| plant_id| STRING| TAG| +| device_id| STRING| TAG| +| model| STRING| ATTRIBUTE| +|temperature| FLOAT| FIELD| +| humidity| DOUBLE| FIELD| ++-----------+---------+-----------+ +``` + -## 2. 数据更新 +## 3. 数据更新 -### 2.1 语法 +### 3.1 语法 ```SQL UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? diff --git a/src/zh/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_apache.md b/src/zh/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_apache.md index 24d2f5dbe..25fe37c70 100644 --- a/src/zh/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_apache.md +++ b/src/zh/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_apache.md @@ -21,7 +21,7 @@ # 写入&更新 -## 1. 数据写入 +## 1. SQL 写入 ### 1.1 语法 @@ -41,92 +41,14 @@ INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, 6. 写入重复时间戳,原时间戳对应列的值会更新。 7. 若 INSERT 语句未指定列名(如 INSERT INTO table VALUES (...)),则 VALUES中的值必须严格按表中列的物理顺序排列(顺序可通过 DESC table命令查看)。 -由于属性一般并不随时间的变化而变化,因此推荐以 update 的方式单独更新属性值,参见下文 [数据更新](#数据更新)。 +由于属性一般并不随时间的变化而变化,因此推荐以 update 的方式单独更新属性值,参见下文 [数据更新](#_3-数据更新)。
-### 1.2 通过 Session 写入自动创建表 - -在通过 Session 进行数据写入时,IoTDB 支持无模式写入:无需事先手动创建表,系统会根据写入请求中的信息自动构建表结构,之后直接执行数据写入操作。 - -**示例:** - -```Java -try (ITableSession session = - new TableSessionBuilder() - .nodeUrls(Collections.singletonList("127.0.0.1:6667")) - .username("root") - .password("root") - .build()) { - - session.executeNonQueryStatement("CREATE DATABASE db1"); - session.executeNonQueryStatement("use db1"); - - // 不创建表直接写入数据 - List columnNameList = - Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); - List dataTypeList = - Arrays.asList( - TSDataType.STRING, - TSDataType.STRING, - TSDataType.STRING, - TSDataType.STRING, - TSDataType.FLOAT, - TSDataType.DOUBLE); - List columnTypeList = - new ArrayList<>( - Arrays.asList( - ColumnCategory.TAG, - ColumnCategory.TAG, - ColumnCategory.TAG, - ColumnCategory.ATTRIBUTE, - ColumnCategory.FIELD, - ColumnCategory.FIELD)); - Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); - for (long timestamp = 0; timestamp < 100; timestamp++) { - int rowIndex = tablet.getRowSize(); - tablet.addTimestamp(rowIndex, timestamp); - tablet.addValue("region_id", rowIndex, "1"); - tablet.addValue("plant_id", rowIndex, "5"); - tablet.addValue("device_id", rowIndex, "3"); - tablet.addValue("model", rowIndex, "A"); - tablet.addValue("temperature", rowIndex, 37.6F); - tablet.addValue("humidity", rowIndex, 111.1); - if (tablet.getRowSize() == tablet.getMaxRowNumber()) { - session.insert(tablet); - tablet.reset(); - } - } - if (tablet.getRowSize() != 0) { - session.insert(tablet); - tablet.reset(); - } -} -``` - -在代码执行完成后,可以通过下述语句确认表已成功创建,其中包含了时间列、标签列、属性列以及测点列等各类信息。 - -```SQL -desc table1; -``` -```shell -+-----------+---------+-----------+ -| ColumnName| DataType| Category| -+-----------+---------+-----------+ -| time|TIMESTAMP| TIME| -| region_id| STRING| TAG| -| plant_id| STRING| TAG| -| device_id| STRING| TAG| -| model| STRING| ATTRIBUTE| -|temperature| FLOAT| FIELD| -| humidity| DOUBLE| FIELD| -+-----------+---------+-----------+ -``` - -### 1.3 指定列写入 +### 1.2 指定列写入 在写入操作中,可以指定部分列,未指定的列将不会被写入任何内容(即设置为 `null`)。 @@ -138,7 +60,7 @@ INSERT INTO table1(region, plant_id, device_id, time, temperature, humidity) VAL INSERT INTO table1(region, plant_id, device_id, time, temperature) VALUES ('北京', '1001', '100', '2025-11-26 13:38:00', 91.0); ``` -### 1.4 空值写入 +### 1.3 空值写入 标签列、属性列和测点列可以指定空值(`null`),表示不写入任何内容。 @@ -155,7 +77,7 @@ INSERT INTO table1(region, plant_id, device_id, model_id, maintenance, time, tem > 注意,该操作不仅会自动为表中已有的标签列填充 null 值,而且对于未来新增的标签列,同样会自动填充 null。 -### 1.5 多行写入 +### 1.4 多行写入 支持同时写入多行数据,提高数据写入效率。 @@ -179,13 +101,13 @@ VALUES - 如果在 SQL 语句中引用了表中不存在的列,IoTDB 将返回错误码 `COLUMN_NOT_EXIST(616)`。 - 如果写入的数据类型与表中列的数据类型不一致,将报错 `DATA_TYPE_MISMATCH(507)`。 -### 1.6 查询写回 +### 1.5 查询写回 IoTDB 表模型支持追加查询写回功能,即`INSERT INTO QUERY` 语句,支持将查询结果写入**已经存在**的表中。 > 注意:该功能从 V 2.0.6 版本开始提供。 -#### 1.6.1 语法定义 +#### 1.5.1 语法定义 ```SQL INSERT INTO table_name [ ( column [, ... ] ) ] query @@ -284,7 +206,7 @@ Total line number = 2 It costs 0.014s ``` -#### 1.6.2 相关说明 +#### 1.5.2 相关说明 * 允许 query 中的源表与目标表 table\_name 是同一个表,例如:`INSERT INTO testtb SELECT * FROM testtb`。 * 目标表必须已存在,否则提示错误信息`550: Table 'xxx.xxx' does not exist`。 @@ -303,9 +225,88 @@ It costs 0.014s * 更多用户权限相关的内容,请参考[权限管理](../User-Manual/Authority-Management_apache.md)。 -## 2. 数据更新 +## 2. 无模式写入 + +在通过 Session 进行数据写入时,IoTDB 支持无模式写入:无需事先手动创建表,系统会根据写入请求中的信息自动构建表结构,之后直接执行数据写入操作。 + +**示例:** + +```Java +try (ITableSession session = + new TableSessionBuilder() + .nodeUrls(Collections.singletonList("127.0.0.1:6667")) + .username("root") + .password("root") + .build()) { + + session.executeNonQueryStatement("CREATE DATABASE db1"); + session.executeNonQueryStatement("use db1"); + + // 不创建表直接写入数据 + List columnNameList = + Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); + List dataTypeList = + Arrays.asList( + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.FLOAT, + TSDataType.DOUBLE); + List columnTypeList = + new ArrayList<>( + Arrays.asList( + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.ATTRIBUTE, + ColumnCategory.FIELD, + ColumnCategory.FIELD)); + Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); + for (long timestamp = 0; timestamp < 100; timestamp++) { + int rowIndex = tablet.getRowSize(); + tablet.addTimestamp(rowIndex, timestamp); + tablet.addValue("region_id", rowIndex, "1"); + tablet.addValue("plant_id", rowIndex, "5"); + tablet.addValue("device_id", rowIndex, "3"); + tablet.addValue("model", rowIndex, "A"); + tablet.addValue("temperature", rowIndex, 37.6F); + tablet.addValue("humidity", rowIndex, 111.1); + if (tablet.getRowSize() == tablet.getMaxRowNumber()) { + session.insert(tablet); + tablet.reset(); + } + } + if (tablet.getRowSize() != 0) { + session.insert(tablet); + tablet.reset(); + } +} +``` + +在代码执行完成后,可以通过下述语句确认表已成功创建,其中包含了时间列、标签列、属性列以及测点列等各类信息。 + +```SQL +desc table1; +``` +```shell ++-----------+---------+-----------+ +| ColumnName| DataType| Category| ++-----------+---------+-----------+ +| time|TIMESTAMP| TIME| +| region_id| STRING| TAG| +| plant_id| STRING| TAG| +| device_id| STRING| TAG| +| model| STRING| ATTRIBUTE| +|temperature| FLOAT| FIELD| +| humidity| DOUBLE| FIELD| ++-----------+---------+-----------+ +``` + + +## 3. 数据更新 -### 2.1 语法 +### 3.1 语法 ```SQL UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? diff --git a/src/zh/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_timecho.md b/src/zh/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_timecho.md index 666e07dfc..58ea9299d 100644 --- a/src/zh/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_timecho.md +++ b/src/zh/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_timecho.md @@ -21,7 +21,7 @@ # 写入&更新 -## 1. 数据写入 +## 1.SQL 写入 ### 1.1 语法 @@ -41,92 +41,14 @@ INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, 6. 写入重复时间戳,原时间戳对应列的值会更新。 7. 若 INSERT 语句未指定列名(如 INSERT INTO table VALUES (...)),则 VALUES中的值必须严格按表中列的物理顺序排列(顺序可通过 DESC table命令查看)。 -由于属性一般并不随时间的变化而变化,因此推荐以 update 的方式单独更新属性值,参见下文 [数据更新](#数据更新)。 +由于属性一般并不随时间的变化而变化,因此推荐以 update 的方式单独更新属性值,参见下文 [数据更新](#_3-数据更新)。
-### 1.2 通过 Session 写入自动创建表 - -在通过 Session 进行数据写入时,IoTDB 支持无模式写入:无需事先手动创建表,系统会根据写入请求中的信息自动构建表结构,之后直接执行数据写入操作。 - -**示例:** - -```Java -try (ITableSession session = - new TableSessionBuilder() - .nodeUrls(Collections.singletonList("127.0.0.1:6667")) - .username("root") - .password("root") - .build()) { - - session.executeNonQueryStatement("CREATE DATABASE db1"); - session.executeNonQueryStatement("use db1"); - - // 不创建表直接写入数据 - List columnNameList = - Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); - List dataTypeList = - Arrays.asList( - TSDataType.STRING, - TSDataType.STRING, - TSDataType.STRING, - TSDataType.STRING, - TSDataType.FLOAT, - TSDataType.DOUBLE); - List columnTypeList = - new ArrayList<>( - Arrays.asList( - ColumnCategory.TAG, - ColumnCategory.TAG, - ColumnCategory.TAG, - ColumnCategory.ATTRIBUTE, - ColumnCategory.FIELD, - ColumnCategory.FIELD)); - Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); - for (long timestamp = 0; timestamp < 100; timestamp++) { - int rowIndex = tablet.getRowSize(); - tablet.addTimestamp(rowIndex, timestamp); - tablet.addValue("region_id", rowIndex, "1"); - tablet.addValue("plant_id", rowIndex, "5"); - tablet.addValue("device_id", rowIndex, "3"); - tablet.addValue("model", rowIndex, "A"); - tablet.addValue("temperature", rowIndex, 37.6F); - tablet.addValue("humidity", rowIndex, 111.1); - if (tablet.getRowSize() == tablet.getMaxRowNumber()) { - session.insert(tablet); - tablet.reset(); - } - } - if (tablet.getRowSize() != 0) { - session.insert(tablet); - tablet.reset(); - } -} -``` - -在代码执行完成后,可以通过下述语句确认表已成功创建,其中包含了时间列、标签列、属性列以及测点列等各类信息。 - -```SQL -desc table1; -``` -```shell -+-----------+---------+-----------+ -| ColumnName| DataType| Category| -+-----------+---------+-----------+ -| time|TIMESTAMP| TIME| -| region_id| STRING| TAG| -| plant_id| STRING| TAG| -| device_id| STRING| TAG| -| model| STRING| ATTRIBUTE| -|temperature| FLOAT| FIELD| -| humidity| DOUBLE| FIELD| -+-----------+---------+-----------+ -``` - -### 1.3 指定列写入 +### 1.2 指定列写入 在写入操作中,可以指定部分列,未指定的列将不会被写入任何内容(即设置为 `null`)。 @@ -138,7 +60,7 @@ INSERT INTO table1(region, plant_id, device_id, time, temperature, humidity) VAL INSERT INTO table1(region, plant_id, device_id, time, temperature) VALUES ('北京', '1001', '100', '2025-11-26 13:38:00', 91.0); ``` -### 1.4 空值写入 +### 1.3 空值写入 标签列、属性列和测点列可以指定空值(`null`),表示不写入任何内容。 @@ -155,7 +77,7 @@ INSERT INTO table1(region, plant_id, device_id, model_id, maintenance, time, tem > 注意,该操作不仅会自动为表中已有的标签列填充 null 值,而且对于未来新增的标签列,同样会自动填充 null。 -### 1.5 多行写入 +### 1.4 多行写入 支持同时写入多行数据,提高数据写入效率。 @@ -179,13 +101,13 @@ VALUES - 如果在 SQL 语句中引用了表中不存在的列,IoTDB 将返回错误码 `COLUMN_NOT_EXIST(616)`。 - 如果写入的数据类型与表中列的数据类型不一致,将报错 `DATA_TYPE_MISMATCH(507)`。 -### 1.6 查询写回 +### 1.5 查询写回 IoTDB 表模型支持追加查询写回功能,即`INSERT INTO QUERY` 语句,支持将查询结果写入**已经存在**的表中。 > 注意:该功能从 V 2.0.6 版本开始提供。 -#### 1.6.1 语法定义 +#### 1.5.1 语法定义 ```SQL INSERT INTO table_name [ ( column [, ... ] ) ] query @@ -284,7 +206,7 @@ Total line number = 2 It costs 0.014s ``` -#### 1.6.2 相关说明 +#### 1.5.2 相关说明 * 允许 query 中的源表与目标表 table\_name 是同一个表,例如:`INSERT INTO testtb SELECT * FROM testtb`。 * 目标表必须已存在,否则提示错误信息`550: Table 'xxx.xxx' does not exist`。 @@ -303,7 +225,7 @@ It costs 0.014s * 更多用户权限相关的内容,请参考[权限管理](../User-Manual/Authority-Management_timecho.md)。 -### 1.7 Object 类型写入 +### 1.6 Object 类型写入 自为了避免单个 Object 过大导致写入请求过大,Object 类型的值支持拆分后按顺序分段写入。SQL 中需要使用 `to_object(isEOF, offset, content)` 函数进行值填充。 @@ -356,10 +278,88 @@ insert into table1(time, device_id, s1) values(1, 'tag1', to_object(true, 4, X'6 3. 如果已写入了部分数据,本次写入的 offset 为 0,本次写入会清除之前已写入的数据部分,重新写入新的数据 +## 2. 无模式写入 + +在通过 Session 进行数据写入时,IoTDB 支持无模式写入:无需事先手动创建表,系统会根据写入请求中的信息自动构建表结构,之后直接执行数据写入操作。 + +**示例:** + +```Java +try (ITableSession session = + new TableSessionBuilder() + .nodeUrls(Collections.singletonList("127.0.0.1:6667")) + .username("root") + .password("root") + .build()) { + + session.executeNonQueryStatement("CREATE DATABASE db1"); + session.executeNonQueryStatement("use db1"); + + // 不创建表直接写入数据 + List columnNameList = + Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); + List dataTypeList = + Arrays.asList( + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.FLOAT, + TSDataType.DOUBLE); + List columnTypeList = + new ArrayList<>( + Arrays.asList( + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.ATTRIBUTE, + ColumnCategory.FIELD, + ColumnCategory.FIELD)); + Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); + for (long timestamp = 0; timestamp < 100; timestamp++) { + int rowIndex = tablet.getRowSize(); + tablet.addTimestamp(rowIndex, timestamp); + tablet.addValue("region_id", rowIndex, "1"); + tablet.addValue("plant_id", rowIndex, "5"); + tablet.addValue("device_id", rowIndex, "3"); + tablet.addValue("model", rowIndex, "A"); + tablet.addValue("temperature", rowIndex, 37.6F); + tablet.addValue("humidity", rowIndex, 111.1); + if (tablet.getRowSize() == tablet.getMaxRowNumber()) { + session.insert(tablet); + tablet.reset(); + } + } + if (tablet.getRowSize() != 0) { + session.insert(tablet); + tablet.reset(); + } +} +``` + +在代码执行完成后,可以通过下述语句确认表已成功创建,其中包含了时间列、标签列、属性列以及测点列等各类信息。 + +```SQL +desc table1; +``` +```shell ++-----------+---------+-----------+ +| ColumnName| DataType| Category| ++-----------+---------+-----------+ +| time|TIMESTAMP| TIME| +| region_id| STRING| TAG| +| plant_id| STRING| TAG| +| device_id| STRING| TAG| +| model| STRING| ATTRIBUTE| +|temperature| FLOAT| FIELD| +| humidity| DOUBLE| FIELD| ++-----------+---------+-----------+ +``` + -## 2. 数据更新 +## 3. 数据更新 -### 2.1 语法 +### 3.1 语法 ```SQL UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)?