You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by zh...@apache.org on 2025/08/15 10:44:36 UTC
(iotdb-docs) branch main updated: update java native api examples (#851)
This is an automated email from the ASF dual-hosted git repository.
zhaoxinyi pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iotdb-docs.git
The following commit(s) were added to refs/heads/main by this push:
new 75705cc7 update java native api examples (#851)
75705cc7 is described below
commit 75705cc7b87538c008589e3f1cc37e47af84a18f
Author: leto-b <bi...@timecho.com>
AuthorDate: Fri Aug 15 18:44:31 2025 +0800
update java native api examples (#851)
---
.../API/Programming-Java-Native-API_apache.md | 327 ++++++++++-----------
.../API/Programming-Java-Native-API_timecho.md | 327 ++++++++++-----------
.../Master/Tree/API/Programming-Java-Native-API.md | 56 ++--
.../V1.3.x/API/Programming-Java-Native-API.md | 56 ++--
.../dev-1.3/API/Programming-Java-Native-API.md | 56 ++--
.../API/Programming-Java-Native-API_apache.md | 327 ++++++++++-----------
.../API/Programming-Java-Native-API_timecho.md | 327 ++++++++++-----------
.../latest/API/Programming-Java-Native-API.md | 56 ++--
.../API/Programming-Java-Native-API_apache.md | 327 ++++++++++-----------
.../API/Programming-Java-Native-API_timecho.md | 327 ++++++++++-----------
.../Master/Tree/API/Programming-Java-Native-API.md | 56 ++--
.../V1.3.x/API/Programming-Java-Native-API.md | 36 ++-
.../dev-1.3/API/Programming-Java-Native-API.md | 58 ++--
.../API/Programming-Java-Native-API_apache.md | 327 ++++++++++-----------
.../API/Programming-Java-Native-API_timecho.md | 327 ++++++++++-----------
.../latest/API/Programming-Java-Native-API.md | 56 ++--
16 files changed, 1479 insertions(+), 1567 deletions(-)
diff --git a/src/UserGuide/Master/Table/API/Programming-Java-Native-API_apache.md b/src/UserGuide/Master/Table/API/Programming-Java-Native-API_apache.md
index 80a2c0f9..497e0295 100644
--- a/src/UserGuide/Master/Table/API/Programming-Java-Native-API_apache.md
+++ b/src/UserGuide/Master/Table/API/Programming-Java-Native-API_apache.md
@@ -622,9 +622,9 @@ public class TableSessionPoolBuilder {
## 5. Example Code
-Session: [src/main/java/org/apache/iotdb/TableModelSessionExample.java](https://github.com/apache/iotdb/blob/rc/2.0.1/example/session/src/main/java/org/apache/iotdb/TableModelSessionExample.java)
+Session: [src/main/java/org/apache/iotdb/TableModelSessionExample.java](https://github.com/apache/iotdb/blob/master/example/session/src/main/java/org/apache/iotdb/TableModelSessionExample.java)
-SessionPool: [src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java](https://github.com/apache/iotdb/blob/rc/2.0.1/example/session/src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java)
+SessionPool: [src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java](https://github.com/apache/iotdb/blob/master/example/session/src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java)
```Java
/*
@@ -655,199 +655,170 @@ import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.pool.TableSessionPoolBuilder;
+import org.apache.tsfile.enums.ColumnCategory;
import org.apache.tsfile.enums.TSDataType;
import org.apache.tsfile.write.record.Tablet;
-import org.apache.tsfile.write.record.Tablet.ColumnCategory;
-import org.apache.tsfile.write.schema.IMeasurementSchema;
-import org.apache.tsfile.write.schema.MeasurementSchema;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
+import static org.apache.iotdb.SessionExample.printDataSet;
+
public class TableModelSessionPoolExample {
- private static final String LOCAL_URL = "127.0.0.1:6667";
-
- public static void main(String[] args) {
-
- // don't specify database in constructor
- ITableSessionPool tableSessionPool =
- new TableSessionPoolBuilder()
- .nodeUrls(Collections.singletonList(LOCAL_URL))
- .user("root")
- .password("root")
- .maxSize(1)
- .build();
-
- try (ITableSession session = tableSessionPool.getSession()) {
-
- session.executeNonQueryStatement("CREATE DATABASE test1");
- session.executeNonQueryStatement("CREATE DATABASE test2");
-
- session.executeNonQueryStatement("use test2");
-
- // or use full qualified table name
- session.executeNonQueryStatement(
- "create table test1.table1("
- + "region_id STRING TAG, "
- + "plant_id STRING TAG, "
- + "device_id STRING TAG, "
- + "model STRING ATTRIBUTE, "
- + "temperature FLOAT FIELD, "
- + "humidity DOUBLE FIELD) with (TTL=3600000)");
-
- session.executeNonQueryStatement(
- "create table table2("
- + "region_id STRING TAG, "
- + "plant_id STRING TAG, "
- + "color STRING ATTRIBUTE, "
- + "temperature FLOAT FIELD, "
- + "speed DOUBLE FIELD) with (TTL=6600000)");
-
- // show tables from current database
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
- }
-
- // show tables by specifying another database
- // using SHOW tables FROM
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES FROM test1")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
- }
-
- // insert table data by tablet
- List<IMeasurementSchema> measurementSchemaList =
- new ArrayList<>(
- Arrays.asList(
- new MeasurementSchema("region_id", TSDataType.STRING),
- new MeasurementSchema("plant_id", TSDataType.STRING),
- new MeasurementSchema("device_id", TSDataType.STRING),
- new MeasurementSchema("model", TSDataType.STRING),
- new MeasurementSchema("temperature", TSDataType.FLOAT),
- new MeasurementSchema("humidity", TSDataType.DOUBLE)));
- List<ColumnCategory> columnTypeList =
- new ArrayList<>(
- Arrays.asList(
- ColumnCategory.TAG,
- ColumnCategory.TAG,
- ColumnCategory.TAG,
- ColumnCategory.ATTRIBUTE,
- ColumnCategory.FIELD,
- ColumnCategory.FIELD));
- Tablet tablet =
- new Tablet(
- "test1",
- IMeasurementSchema.getMeasurementNameList(measurementSchemaList),
- IMeasurementSchema.getDataTypeList(measurementSchemaList),
- 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();
- }
-
- // query table data
- try (SessionDataSet dataSet =
- session.executeQueryStatement(
- "select * from test1 "
- + "where region_id = '1' and plant_id in ('3', '5') and device_id = '3'")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ private static final String LOCAL_URL = "127.0.0.1:6667";
+
+ public static void main(String[] args) {
+
+ // don't specify database in constructor
+ ITableSessionPool tableSessionPool =
+ new TableSessionPoolBuilder()
+ .nodeUrls(Collections.singletonList(LOCAL_URL))
+ .user("root")
+ .password("root")
+ .maxSize(1)
+ .build();
+
+ try (ITableSession session = tableSessionPool.getSession()) {
+
+ session.executeNonQueryStatement("CREATE DATABASE test1");
+ session.executeNonQueryStatement("CREATE DATABASE test2");
+
+ session.executeNonQueryStatement("use test2");
+
+ // or use full qualified table name
+ session.executeNonQueryStatement(
+ "create table test1.table1("
+ + "region_id STRING TAG, "
+ + "plant_id STRING TAG, "
+ + "device_id STRING TAG, "
+ + "model STRING ATTRIBUTE, "
+ + "temperature FLOAT FIELD, "
+ + "humidity DOUBLE FIELD) with (TTL=3600000)");
+
+ session.executeNonQueryStatement(
+ "create table table2("
+ + "region_id STRING TAG, "
+ + "plant_id STRING TAG, "
+ + "color STRING ATTRIBUTE, "
+ + "temperature FLOAT FIELD, "
+ + "speed DOUBLE FIELD) with (TTL=6600000)");
+
+ // show tables from current database
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
+
+ // show tables by specifying another database
+ // using SHOW tables FROM
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES FROM test1")) {
+ printDataSet(dataSet);
+ }
+
+ // insert table data by tablet
+ List<String> columnNameList =
+ Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity");
+ List<TSDataType> dataTypeList =
+ Arrays.asList(
+ TSDataType.STRING,
+ TSDataType.STRING,
+ TSDataType.STRING,
+ TSDataType.STRING,
+ TSDataType.FLOAT,
+ TSDataType.DOUBLE);
+ List<ColumnCategory> columnTypeList =
+ new ArrayList<>(
+ Arrays.asList(
+ ColumnCategory.TAG,
+ ColumnCategory.TAG,
+ ColumnCategory.TAG,
+ ColumnCategory.ATTRIBUTE,
+ ColumnCategory.FIELD,
+ ColumnCategory.FIELD));
+ Tablet tablet = new Tablet("test1", 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();
+ }
+
+ // query table data
+ try (SessionDataSet dataSet =
+ session.executeQueryStatement(
+ "select * from test1 "
+ + "where region_id = '1' and plant_id in ('3', '5') and device_id = '3'")) {
+ printDataSet(dataSet);
+ }
+
+ } catch (IoTDBConnectionException e) {
+ e.printStackTrace();
+ } catch (StatementExecutionException e) {
+ e.printStackTrace();
+ } finally {
+ tableSessionPool.close();
}
- }
-
- } catch (IoTDBConnectionException e) {
- e.printStackTrace();
- } catch (StatementExecutionException e) {
- e.printStackTrace();
- } finally {
- tableSessionPool.close();
- }
- // specify database in constructor
- tableSessionPool =
- new TableSessionPoolBuilder()
- .nodeUrls(Collections.singletonList(LOCAL_URL))
- .user("root")
- .password("root")
- .maxSize(1)
- .database("test1")
- .build();
-
- try (ITableSession session = tableSessionPool.getSession()) {
-
- // show tables from current database
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
- }
-
- // change database to test2
- session.executeNonQueryStatement("use test2");
-
- // show tables by specifying another database
- // using SHOW tables FROM
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ // specify database in constructor
+ tableSessionPool =
+ new TableSessionPoolBuilder()
+ .nodeUrls(Collections.singletonList(LOCAL_URL))
+ .user("root")
+ .password("root")
+ .maxSize(1)
+ .database("test1")
+ .build();
+
+ try (ITableSession session = tableSessionPool.getSession()) {
+
+ // show tables from current database
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
+
+ // change database to test2
+ session.executeNonQueryStatement("use test2");
+
+ // show tables by specifying another database
+ // using SHOW tables FROM
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
+
+ } catch (IoTDBConnectionException e) {
+ e.printStackTrace();
+ } catch (StatementExecutionException e) {
+ e.printStackTrace();
}
- }
- } catch (IoTDBConnectionException e) {
- e.printStackTrace();
- } catch (StatementExecutionException e) {
- e.printStackTrace();
- }
+ try (ITableSession session = tableSessionPool.getSession()) {
- try (ITableSession session = tableSessionPool.getSession()) {
+ // show tables from default database test1
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
- // show tables from default database test1
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ } catch (IoTDBConnectionException e) {
+ e.printStackTrace();
+ } catch (StatementExecutionException e) {
+ e.printStackTrace();
+ } finally {
+ tableSessionPool.close();
}
- }
-
- } catch (IoTDBConnectionException e) {
- e.printStackTrace();
- } catch (StatementExecutionException e) {
- e.printStackTrace();
- } finally {
- tableSessionPool.close();
}
- }
}
```
\ No newline at end of file
diff --git a/src/UserGuide/Master/Table/API/Programming-Java-Native-API_timecho.md b/src/UserGuide/Master/Table/API/Programming-Java-Native-API_timecho.md
index fba9f72c..945a55e3 100644
--- a/src/UserGuide/Master/Table/API/Programming-Java-Native-API_timecho.md
+++ b/src/UserGuide/Master/Table/API/Programming-Java-Native-API_timecho.md
@@ -622,9 +622,9 @@ public class TableSessionPoolBuilder {
## 5. Example Code
-Session: [src/main/java/org/apache/iotdb/TableModelSessionExample.java](https://github.com/apache/iotdb/blob/rc/2.0.1/example/session/src/main/java/org/apache/iotdb/TableModelSessionExample.java)
+Session: [src/main/java/org/apache/iotdb/TableModelSessionExample.java](https://github.com/apache/iotdb/blob/master/example/session/src/main/java/org/apache/iotdb/TableModelSessionExample.java)
-SessionPool: [src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java](https://github.com/apache/iotdb/blob/rc/2.0.1/example/session/src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java)
+SessionPool: [src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java](https://github.com/apache/iotdb/blob/master/example/session/src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java)
```Java
/*
@@ -655,199 +655,170 @@ import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.pool.TableSessionPoolBuilder;
+import org.apache.tsfile.enums.ColumnCategory;
import org.apache.tsfile.enums.TSDataType;
import org.apache.tsfile.write.record.Tablet;
-import org.apache.tsfile.write.record.Tablet.ColumnCategory;
-import org.apache.tsfile.write.schema.IMeasurementSchema;
-import org.apache.tsfile.write.schema.MeasurementSchema;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
+import static org.apache.iotdb.SessionExample.printDataSet;
+
public class TableModelSessionPoolExample {
- private static final String LOCAL_URL = "127.0.0.1:6667";
-
- public static void main(String[] args) {
-
- // don't specify database in constructor
- ITableSessionPool tableSessionPool =
- new TableSessionPoolBuilder()
- .nodeUrls(Collections.singletonList(LOCAL_URL))
- .user("root")
- .password("root")
- .maxSize(1)
- .build();
-
- try (ITableSession session = tableSessionPool.getSession()) {
-
- session.executeNonQueryStatement("CREATE DATABASE test1");
- session.executeNonQueryStatement("CREATE DATABASE test2");
-
- session.executeNonQueryStatement("use test2");
-
- // or use full qualified table name
- session.executeNonQueryStatement(
- "create table test1.table1("
- + "region_id STRING TAG, "
- + "plant_id STRING TAG, "
- + "device_id STRING TAG, "
- + "model STRING ATTRIBUTE, "
- + "temperature FLOAT FIELD, "
- + "humidity DOUBLE FIELD) with (TTL=3600000)");
-
- session.executeNonQueryStatement(
- "create table table2("
- + "region_id STRING TAG, "
- + "plant_id STRING TAG, "
- + "color STRING ATTRIBUTE, "
- + "temperature FLOAT FIELD, "
- + "speed DOUBLE FIELD) with (TTL=6600000)");
-
- // show tables from current database
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
- }
-
- // show tables by specifying another database
- // using SHOW tables FROM
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES FROM test1")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
- }
-
- // insert table data by tablet
- List<IMeasurementSchema> measurementSchemaList =
- new ArrayList<>(
- Arrays.asList(
- new MeasurementSchema("region_id", TSDataType.STRING),
- new MeasurementSchema("plant_id", TSDataType.STRING),
- new MeasurementSchema("device_id", TSDataType.STRING),
- new MeasurementSchema("model", TSDataType.STRING),
- new MeasurementSchema("temperature", TSDataType.FLOAT),
- new MeasurementSchema("humidity", TSDataType.DOUBLE)));
- List<ColumnCategory> columnTypeList =
- new ArrayList<>(
- Arrays.asList(
- ColumnCategory.TAG,
- ColumnCategory.TAG,
- ColumnCategory.TAG,
- ColumnCategory.ATTRIBUTE,
- ColumnCategory.FIELD,
- ColumnCategory.FIELD));
- Tablet tablet =
- new Tablet(
- "test1",
- IMeasurementSchema.getMeasurementNameList(measurementSchemaList),
- IMeasurementSchema.getDataTypeList(measurementSchemaList),
- 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();
- }
-
- // query table data
- try (SessionDataSet dataSet =
- session.executeQueryStatement(
- "select * from test1 "
- + "where region_id = '1' and plant_id in ('3', '5') and device_id = '3'")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ private static final String LOCAL_URL = "127.0.0.1:6667";
+
+ public static void main(String[] args) {
+
+ // don't specify database in constructor
+ ITableSessionPool tableSessionPool =
+ new TableSessionPoolBuilder()
+ .nodeUrls(Collections.singletonList(LOCAL_URL))
+ .user("root")
+ .password("root")
+ .maxSize(1)
+ .build();
+
+ try (ITableSession session = tableSessionPool.getSession()) {
+
+ session.executeNonQueryStatement("CREATE DATABASE test1");
+ session.executeNonQueryStatement("CREATE DATABASE test2");
+
+ session.executeNonQueryStatement("use test2");
+
+ // or use full qualified table name
+ session.executeNonQueryStatement(
+ "create table test1.table1("
+ + "region_id STRING TAG, "
+ + "plant_id STRING TAG, "
+ + "device_id STRING TAG, "
+ + "model STRING ATTRIBUTE, "
+ + "temperature FLOAT FIELD, "
+ + "humidity DOUBLE FIELD) with (TTL=3600000)");
+
+ session.executeNonQueryStatement(
+ "create table table2("
+ + "region_id STRING TAG, "
+ + "plant_id STRING TAG, "
+ + "color STRING ATTRIBUTE, "
+ + "temperature FLOAT FIELD, "
+ + "speed DOUBLE FIELD) with (TTL=6600000)");
+
+ // show tables from current database
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
+
+ // show tables by specifying another database
+ // using SHOW tables FROM
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES FROM test1")) {
+ printDataSet(dataSet);
+ }
+
+ // insert table data by tablet
+ List<String> columnNameList =
+ Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity");
+ List<TSDataType> dataTypeList =
+ Arrays.asList(
+ TSDataType.STRING,
+ TSDataType.STRING,
+ TSDataType.STRING,
+ TSDataType.STRING,
+ TSDataType.FLOAT,
+ TSDataType.DOUBLE);
+ List<ColumnCategory> columnTypeList =
+ new ArrayList<>(
+ Arrays.asList(
+ ColumnCategory.TAG,
+ ColumnCategory.TAG,
+ ColumnCategory.TAG,
+ ColumnCategory.ATTRIBUTE,
+ ColumnCategory.FIELD,
+ ColumnCategory.FIELD));
+ Tablet tablet = new Tablet("test1", 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();
+ }
+
+ // query table data
+ try (SessionDataSet dataSet =
+ session.executeQueryStatement(
+ "select * from test1 "
+ + "where region_id = '1' and plant_id in ('3', '5') and device_id = '3'")) {
+ printDataSet(dataSet);
+ }
+
+ } catch (IoTDBConnectionException e) {
+ e.printStackTrace();
+ } catch (StatementExecutionException e) {
+ e.printStackTrace();
+ } finally {
+ tableSessionPool.close();
}
- }
-
- } catch (IoTDBConnectionException e) {
- e.printStackTrace();
- } catch (StatementExecutionException e) {
- e.printStackTrace();
- } finally {
- tableSessionPool.close();
- }
- // specify database in constructor
- tableSessionPool =
- new TableSessionPoolBuilder()
- .nodeUrls(Collections.singletonList(LOCAL_URL))
- .user("root")
- .password("root")
- .maxSize(1)
- .database("test1")
- .build();
-
- try (ITableSession session = tableSessionPool.getSession()) {
-
- // show tables from current database
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
- }
-
- // change database to test2
- session.executeNonQueryStatement("use test2");
-
- // show tables by specifying another database
- // using SHOW tables FROM
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ // specify database in constructor
+ tableSessionPool =
+ new TableSessionPoolBuilder()
+ .nodeUrls(Collections.singletonList(LOCAL_URL))
+ .user("root")
+ .password("root")
+ .maxSize(1)
+ .database("test1")
+ .build();
+
+ try (ITableSession session = tableSessionPool.getSession()) {
+
+ // show tables from current database
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
+
+ // change database to test2
+ session.executeNonQueryStatement("use test2");
+
+ // show tables by specifying another database
+ // using SHOW tables FROM
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
+
+ } catch (IoTDBConnectionException e) {
+ e.printStackTrace();
+ } catch (StatementExecutionException e) {
+ e.printStackTrace();
}
- }
- } catch (IoTDBConnectionException e) {
- e.printStackTrace();
- } catch (StatementExecutionException e) {
- e.printStackTrace();
- }
+ try (ITableSession session = tableSessionPool.getSession()) {
- try (ITableSession session = tableSessionPool.getSession()) {
+ // show tables from default database test1
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
- // show tables from default database test1
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ } catch (IoTDBConnectionException e) {
+ e.printStackTrace();
+ } catch (StatementExecutionException e) {
+ e.printStackTrace();
+ } finally {
+ tableSessionPool.close();
}
- }
-
- } catch (IoTDBConnectionException e) {
- e.printStackTrace();
- } catch (StatementExecutionException e) {
- e.printStackTrace();
- } finally {
- tableSessionPool.close();
}
- }
}
```
\ No newline at end of file
diff --git a/src/UserGuide/Master/Tree/API/Programming-Java-Native-API.md b/src/UserGuide/Master/Tree/API/Programming-Java-Native-API.md
index 159ab303..347493e0 100644
--- a/src/UserGuide/Master/Tree/API/Programming-Java-Native-API.md
+++ b/src/UserGuide/Master/Tree/API/Programming-Java-Native-API.md
@@ -308,33 +308,51 @@ public class SessionPoolExample {
sessionPool.executeNonQueryStatement("set TTL to root.test.** 10000");
// 3. delete time series
sessionPool.executeNonQueryStatement("delete timeseries root.test.d1.s1");
- private static void executeQueryExample() throws IoTDBConnectionException, StatementExecutionException {
+ }
+
+ private static void executeQueryExample() throws IoTDBConnectionException, StatementExecutionException {
// 1. execute normal query
try(SessionDataSetWrapper wrapper = sessionPool.executeQueryStatement("select s1 from root.sg1.d1 limit 10")) {
- while (wrapper.hasNext()) {
- System.out.println(wrapper.next());
- }
+ // get DataIterator like JDBC
+ DataIterator dataIterator = wrapper.iterator();
+ System.out.println(wrapper.getColumnNames());
+ System.out.println(wrapper.getColumnTypes());
+ while (dataIterator.next()) {
+ StringBuilder builder = new StringBuilder();
+ for (String columnName : wrapper.getColumnNames()) {
+ builder.append(dataIterator.getString(columnName) + " ");
}
+ System.out.println(builder);
+ }
+ }
// 2. execute aggregate query
try(SessionDataSetWrapper wrapper = sessionPool.executeQueryStatement("select count(s1) from root.sg1.d1 group by ([0, 40), 5ms) ")) {
- while (wrapper.hasNext()) {
- System.out.println(wrapper.next());
+ // get DataIterator like JDBC
+ DataIterator dataIterator = wrapper.iterator();
+ System.out.println(wrapper.getColumnNames());
+ System.out.println(wrapper.getColumnTypes());
+ while (dataIterator.next()) {
+ StringBuilder builder = new StringBuilder();
+ for (String columnName : wrapper.getColumnNames()) {
+ builder.append(dataIterator.getString(columnName) + " ");
+ }
+ System.out.println(builder);
}
}
- }
+ }
- private static void constructSessionPool() {
- // Using nodeUrls ensures that when one node goes down, other nodes are automatically connected to retry
- List<String> nodeUrls = new ArrayList<>();
- nodeUrls.add("127.0.0.1:6667");
- nodeUrls.add("127.0.0.1:6668");
- sessionPool =
- new SessionPool.Builder()
- .nodeUrls(nodeUrls)
- .user("root")
- .password("root")
- .maxSize(3)
- .build();
+ private static void constructSessionPool() {
+ // Using nodeUrls ensures that when one node goes down, other nodes are automatically connected to retry
+ List<String> nodeUrls = new ArrayList<>();
+ nodeUrls.add("127.0.0.1:6667");
+ nodeUrls.add("127.0.0.1:6668");
+ sessionPool =
+ new SessionPool.Builder()
+ .nodeUrls(nodeUrls)
+ .user("root")
+ .password("root")
+ .maxSize(3)
+ .build();
}
public static void closeSessionPool(){
diff --git a/src/UserGuide/V1.3.x/API/Programming-Java-Native-API.md b/src/UserGuide/V1.3.x/API/Programming-Java-Native-API.md
index 0644e606..7a7b922b 100644
--- a/src/UserGuide/V1.3.x/API/Programming-Java-Native-API.md
+++ b/src/UserGuide/V1.3.x/API/Programming-Java-Native-API.md
@@ -308,33 +308,51 @@ public class SessionPoolExample {
sessionPool.executeNonQueryStatement("set TTL to root.test.** 10000");
// 3. delete time series
sessionPool.executeNonQueryStatement("delete timeseries root.test.d1.s1");
- private static void executeQueryExample() throws IoTDBConnectionException, StatementExecutionException {
+ }
+
+ private static void executeQueryExample() throws IoTDBConnectionException, StatementExecutionException {
// 1. execute normal query
try(SessionDataSetWrapper wrapper = sessionPool.executeQueryStatement("select s1 from root.sg1.d1 limit 10")) {
- while (wrapper.hasNext()) {
- System.out.println(wrapper.next());
- }
+ // get DataIterator like JDBC
+ DataIterator dataIterator = wrapper.iterator();
+ System.out.println(wrapper.getColumnNames());
+ System.out.println(wrapper.getColumnTypes());
+ while (dataIterator.next()) {
+ StringBuilder builder = new StringBuilder();
+ for (String columnName : wrapper.getColumnNames()) {
+ builder.append(dataIterator.getString(columnName) + " ");
}
+ System.out.println(builder);
+ }
+ }
// 2. execute aggregate query
try(SessionDataSetWrapper wrapper = sessionPool.executeQueryStatement("select count(s1) from root.sg1.d1 group by ([0, 40), 5ms) ")) {
- while (wrapper.hasNext()) {
- System.out.println(wrapper.next());
+ // get DataIterator like JDBC
+ DataIterator dataIterator = wrapper.iterator();
+ System.out.println(wrapper.getColumnNames());
+ System.out.println(wrapper.getColumnTypes());
+ while (dataIterator.next()) {
+ StringBuilder builder = new StringBuilder();
+ for (String columnName : wrapper.getColumnNames()) {
+ builder.append(dataIterator.getString(columnName) + " ");
+ }
+ System.out.println(builder);
}
}
- }
+ }
- private static void constructSessionPool() {
- // Using nodeUrls ensures that when one node goes down, other nodes are automatically connected to retry
- List<String> nodeUrls = new ArrayList<>();
- nodeUrls.add("127.0.0.1:6667");
- nodeUrls.add("127.0.0.1:6668");
- sessionPool =
- new SessionPool.Builder()
- .nodeUrls(nodeUrls)
- .user("root")
- .password("root")
- .maxSize(3)
- .build();
+ private static void constructSessionPool() {
+ // Using nodeUrls ensures that when one node goes down, other nodes are automatically connected to retry
+ List<String> nodeUrls = new ArrayList<>();
+ nodeUrls.add("127.0.0.1:6667");
+ nodeUrls.add("127.0.0.1:6668");
+ sessionPool =
+ new SessionPool.Builder()
+ .nodeUrls(nodeUrls)
+ .user("root")
+ .password("root")
+ .maxSize(3)
+ .build();
}
public static void closeSessionPool(){
diff --git a/src/UserGuide/dev-1.3/API/Programming-Java-Native-API.md b/src/UserGuide/dev-1.3/API/Programming-Java-Native-API.md
index c9f20a26..66f27bba 100644
--- a/src/UserGuide/dev-1.3/API/Programming-Java-Native-API.md
+++ b/src/UserGuide/dev-1.3/API/Programming-Java-Native-API.md
@@ -308,33 +308,51 @@ public class SessionPoolExample {
sessionPool.executeNonQueryStatement("set TTL to root.test.** 10000");
// 3. delete time series
sessionPool.executeNonQueryStatement("delete timeseries root.test.d1.s1");
- private static void executeQueryExample() throws IoTDBConnectionException, StatementExecutionException {
+ }
+
+ private static void executeQueryExample() throws IoTDBConnectionException, StatementExecutionException {
// 1. execute normal query
try(SessionDataSetWrapper wrapper = sessionPool.executeQueryStatement("select s1 from root.sg1.d1 limit 10")) {
- while (wrapper.hasNext()) {
- System.out.println(wrapper.next());
- }
+ // get DataIterator like JDBC
+ DataIterator dataIterator = wrapper.iterator();
+ System.out.println(wrapper.getColumnNames());
+ System.out.println(wrapper.getColumnTypes());
+ while (dataIterator.next()) {
+ StringBuilder builder = new StringBuilder();
+ for (String columnName : wrapper.getColumnNames()) {
+ builder.append(dataIterator.getString(columnName) + " ");
}
+ System.out.println(builder);
+ }
+ }
// 2. execute aggregate query
try(SessionDataSetWrapper wrapper = sessionPool.executeQueryStatement("select count(s1) from root.sg1.d1 group by ([0, 40), 5ms) ")) {
- while (wrapper.hasNext()) {
- System.out.println(wrapper.next());
+ // get DataIterator like JDBC
+ DataIterator dataIterator = wrapper.iterator();
+ System.out.println(wrapper.getColumnNames());
+ System.out.println(wrapper.getColumnTypes());
+ while (dataIterator.next()) {
+ StringBuilder builder = new StringBuilder();
+ for (String columnName : wrapper.getColumnNames()) {
+ builder.append(dataIterator.getString(columnName) + " ");
+ }
+ System.out.println(builder);
}
}
- }
+ }
- private static void constructSessionPool() {
- // Using nodeUrls ensures that when one node goes down, other nodes are automatically connected to retry
- List<String> nodeUrls = new ArrayList<>();
- nodeUrls.add("127.0.0.1:6667");
- nodeUrls.add("127.0.0.1:6668");
- sessionPool =
- new SessionPool.Builder()
- .nodeUrls(nodeUrls)
- .user("root")
- .password("root")
- .maxSize(3)
- .build();
+ private static void constructSessionPool() {
+ // Using nodeUrls ensures that when one node goes down, other nodes are automatically connected to retry
+ List<String> nodeUrls = new ArrayList<>();
+ nodeUrls.add("127.0.0.1:6667");
+ nodeUrls.add("127.0.0.1:6668");
+ sessionPool =
+ new SessionPool.Builder()
+ .nodeUrls(nodeUrls)
+ .user("root")
+ .password("root")
+ .maxSize(3)
+ .build();
}
public static void closeSessionPool(){
diff --git a/src/UserGuide/latest-Table/API/Programming-Java-Native-API_apache.md b/src/UserGuide/latest-Table/API/Programming-Java-Native-API_apache.md
index 80a2c0f9..497e0295 100644
--- a/src/UserGuide/latest-Table/API/Programming-Java-Native-API_apache.md
+++ b/src/UserGuide/latest-Table/API/Programming-Java-Native-API_apache.md
@@ -622,9 +622,9 @@ public class TableSessionPoolBuilder {
## 5. Example Code
-Session: [src/main/java/org/apache/iotdb/TableModelSessionExample.java](https://github.com/apache/iotdb/blob/rc/2.0.1/example/session/src/main/java/org/apache/iotdb/TableModelSessionExample.java)
+Session: [src/main/java/org/apache/iotdb/TableModelSessionExample.java](https://github.com/apache/iotdb/blob/master/example/session/src/main/java/org/apache/iotdb/TableModelSessionExample.java)
-SessionPool: [src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java](https://github.com/apache/iotdb/blob/rc/2.0.1/example/session/src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java)
+SessionPool: [src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java](https://github.com/apache/iotdb/blob/master/example/session/src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java)
```Java
/*
@@ -655,199 +655,170 @@ import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.pool.TableSessionPoolBuilder;
+import org.apache.tsfile.enums.ColumnCategory;
import org.apache.tsfile.enums.TSDataType;
import org.apache.tsfile.write.record.Tablet;
-import org.apache.tsfile.write.record.Tablet.ColumnCategory;
-import org.apache.tsfile.write.schema.IMeasurementSchema;
-import org.apache.tsfile.write.schema.MeasurementSchema;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
+import static org.apache.iotdb.SessionExample.printDataSet;
+
public class TableModelSessionPoolExample {
- private static final String LOCAL_URL = "127.0.0.1:6667";
-
- public static void main(String[] args) {
-
- // don't specify database in constructor
- ITableSessionPool tableSessionPool =
- new TableSessionPoolBuilder()
- .nodeUrls(Collections.singletonList(LOCAL_URL))
- .user("root")
- .password("root")
- .maxSize(1)
- .build();
-
- try (ITableSession session = tableSessionPool.getSession()) {
-
- session.executeNonQueryStatement("CREATE DATABASE test1");
- session.executeNonQueryStatement("CREATE DATABASE test2");
-
- session.executeNonQueryStatement("use test2");
-
- // or use full qualified table name
- session.executeNonQueryStatement(
- "create table test1.table1("
- + "region_id STRING TAG, "
- + "plant_id STRING TAG, "
- + "device_id STRING TAG, "
- + "model STRING ATTRIBUTE, "
- + "temperature FLOAT FIELD, "
- + "humidity DOUBLE FIELD) with (TTL=3600000)");
-
- session.executeNonQueryStatement(
- "create table table2("
- + "region_id STRING TAG, "
- + "plant_id STRING TAG, "
- + "color STRING ATTRIBUTE, "
- + "temperature FLOAT FIELD, "
- + "speed DOUBLE FIELD) with (TTL=6600000)");
-
- // show tables from current database
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
- }
-
- // show tables by specifying another database
- // using SHOW tables FROM
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES FROM test1")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
- }
-
- // insert table data by tablet
- List<IMeasurementSchema> measurementSchemaList =
- new ArrayList<>(
- Arrays.asList(
- new MeasurementSchema("region_id", TSDataType.STRING),
- new MeasurementSchema("plant_id", TSDataType.STRING),
- new MeasurementSchema("device_id", TSDataType.STRING),
- new MeasurementSchema("model", TSDataType.STRING),
- new MeasurementSchema("temperature", TSDataType.FLOAT),
- new MeasurementSchema("humidity", TSDataType.DOUBLE)));
- List<ColumnCategory> columnTypeList =
- new ArrayList<>(
- Arrays.asList(
- ColumnCategory.TAG,
- ColumnCategory.TAG,
- ColumnCategory.TAG,
- ColumnCategory.ATTRIBUTE,
- ColumnCategory.FIELD,
- ColumnCategory.FIELD));
- Tablet tablet =
- new Tablet(
- "test1",
- IMeasurementSchema.getMeasurementNameList(measurementSchemaList),
- IMeasurementSchema.getDataTypeList(measurementSchemaList),
- 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();
- }
-
- // query table data
- try (SessionDataSet dataSet =
- session.executeQueryStatement(
- "select * from test1 "
- + "where region_id = '1' and plant_id in ('3', '5') and device_id = '3'")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ private static final String LOCAL_URL = "127.0.0.1:6667";
+
+ public static void main(String[] args) {
+
+ // don't specify database in constructor
+ ITableSessionPool tableSessionPool =
+ new TableSessionPoolBuilder()
+ .nodeUrls(Collections.singletonList(LOCAL_URL))
+ .user("root")
+ .password("root")
+ .maxSize(1)
+ .build();
+
+ try (ITableSession session = tableSessionPool.getSession()) {
+
+ session.executeNonQueryStatement("CREATE DATABASE test1");
+ session.executeNonQueryStatement("CREATE DATABASE test2");
+
+ session.executeNonQueryStatement("use test2");
+
+ // or use full qualified table name
+ session.executeNonQueryStatement(
+ "create table test1.table1("
+ + "region_id STRING TAG, "
+ + "plant_id STRING TAG, "
+ + "device_id STRING TAG, "
+ + "model STRING ATTRIBUTE, "
+ + "temperature FLOAT FIELD, "
+ + "humidity DOUBLE FIELD) with (TTL=3600000)");
+
+ session.executeNonQueryStatement(
+ "create table table2("
+ + "region_id STRING TAG, "
+ + "plant_id STRING TAG, "
+ + "color STRING ATTRIBUTE, "
+ + "temperature FLOAT FIELD, "
+ + "speed DOUBLE FIELD) with (TTL=6600000)");
+
+ // show tables from current database
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
+
+ // show tables by specifying another database
+ // using SHOW tables FROM
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES FROM test1")) {
+ printDataSet(dataSet);
+ }
+
+ // insert table data by tablet
+ List<String> columnNameList =
+ Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity");
+ List<TSDataType> dataTypeList =
+ Arrays.asList(
+ TSDataType.STRING,
+ TSDataType.STRING,
+ TSDataType.STRING,
+ TSDataType.STRING,
+ TSDataType.FLOAT,
+ TSDataType.DOUBLE);
+ List<ColumnCategory> columnTypeList =
+ new ArrayList<>(
+ Arrays.asList(
+ ColumnCategory.TAG,
+ ColumnCategory.TAG,
+ ColumnCategory.TAG,
+ ColumnCategory.ATTRIBUTE,
+ ColumnCategory.FIELD,
+ ColumnCategory.FIELD));
+ Tablet tablet = new Tablet("test1", 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();
+ }
+
+ // query table data
+ try (SessionDataSet dataSet =
+ session.executeQueryStatement(
+ "select * from test1 "
+ + "where region_id = '1' and plant_id in ('3', '5') and device_id = '3'")) {
+ printDataSet(dataSet);
+ }
+
+ } catch (IoTDBConnectionException e) {
+ e.printStackTrace();
+ } catch (StatementExecutionException e) {
+ e.printStackTrace();
+ } finally {
+ tableSessionPool.close();
}
- }
-
- } catch (IoTDBConnectionException e) {
- e.printStackTrace();
- } catch (StatementExecutionException e) {
- e.printStackTrace();
- } finally {
- tableSessionPool.close();
- }
- // specify database in constructor
- tableSessionPool =
- new TableSessionPoolBuilder()
- .nodeUrls(Collections.singletonList(LOCAL_URL))
- .user("root")
- .password("root")
- .maxSize(1)
- .database("test1")
- .build();
-
- try (ITableSession session = tableSessionPool.getSession()) {
-
- // show tables from current database
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
- }
-
- // change database to test2
- session.executeNonQueryStatement("use test2");
-
- // show tables by specifying another database
- // using SHOW tables FROM
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ // specify database in constructor
+ tableSessionPool =
+ new TableSessionPoolBuilder()
+ .nodeUrls(Collections.singletonList(LOCAL_URL))
+ .user("root")
+ .password("root")
+ .maxSize(1)
+ .database("test1")
+ .build();
+
+ try (ITableSession session = tableSessionPool.getSession()) {
+
+ // show tables from current database
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
+
+ // change database to test2
+ session.executeNonQueryStatement("use test2");
+
+ // show tables by specifying another database
+ // using SHOW tables FROM
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
+
+ } catch (IoTDBConnectionException e) {
+ e.printStackTrace();
+ } catch (StatementExecutionException e) {
+ e.printStackTrace();
}
- }
- } catch (IoTDBConnectionException e) {
- e.printStackTrace();
- } catch (StatementExecutionException e) {
- e.printStackTrace();
- }
+ try (ITableSession session = tableSessionPool.getSession()) {
- try (ITableSession session = tableSessionPool.getSession()) {
+ // show tables from default database test1
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
- // show tables from default database test1
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ } catch (IoTDBConnectionException e) {
+ e.printStackTrace();
+ } catch (StatementExecutionException e) {
+ e.printStackTrace();
+ } finally {
+ tableSessionPool.close();
}
- }
-
- } catch (IoTDBConnectionException e) {
- e.printStackTrace();
- } catch (StatementExecutionException e) {
- e.printStackTrace();
- } finally {
- tableSessionPool.close();
}
- }
}
```
\ No newline at end of file
diff --git a/src/UserGuide/latest-Table/API/Programming-Java-Native-API_timecho.md b/src/UserGuide/latest-Table/API/Programming-Java-Native-API_timecho.md
index fba9f72c..945a55e3 100644
--- a/src/UserGuide/latest-Table/API/Programming-Java-Native-API_timecho.md
+++ b/src/UserGuide/latest-Table/API/Programming-Java-Native-API_timecho.md
@@ -622,9 +622,9 @@ public class TableSessionPoolBuilder {
## 5. Example Code
-Session: [src/main/java/org/apache/iotdb/TableModelSessionExample.java](https://github.com/apache/iotdb/blob/rc/2.0.1/example/session/src/main/java/org/apache/iotdb/TableModelSessionExample.java)
+Session: [src/main/java/org/apache/iotdb/TableModelSessionExample.java](https://github.com/apache/iotdb/blob/master/example/session/src/main/java/org/apache/iotdb/TableModelSessionExample.java)
-SessionPool: [src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java](https://github.com/apache/iotdb/blob/rc/2.0.1/example/session/src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java)
+SessionPool: [src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java](https://github.com/apache/iotdb/blob/master/example/session/src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java)
```Java
/*
@@ -655,199 +655,170 @@ import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.pool.TableSessionPoolBuilder;
+import org.apache.tsfile.enums.ColumnCategory;
import org.apache.tsfile.enums.TSDataType;
import org.apache.tsfile.write.record.Tablet;
-import org.apache.tsfile.write.record.Tablet.ColumnCategory;
-import org.apache.tsfile.write.schema.IMeasurementSchema;
-import org.apache.tsfile.write.schema.MeasurementSchema;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
+import static org.apache.iotdb.SessionExample.printDataSet;
+
public class TableModelSessionPoolExample {
- private static final String LOCAL_URL = "127.0.0.1:6667";
-
- public static void main(String[] args) {
-
- // don't specify database in constructor
- ITableSessionPool tableSessionPool =
- new TableSessionPoolBuilder()
- .nodeUrls(Collections.singletonList(LOCAL_URL))
- .user("root")
- .password("root")
- .maxSize(1)
- .build();
-
- try (ITableSession session = tableSessionPool.getSession()) {
-
- session.executeNonQueryStatement("CREATE DATABASE test1");
- session.executeNonQueryStatement("CREATE DATABASE test2");
-
- session.executeNonQueryStatement("use test2");
-
- // or use full qualified table name
- session.executeNonQueryStatement(
- "create table test1.table1("
- + "region_id STRING TAG, "
- + "plant_id STRING TAG, "
- + "device_id STRING TAG, "
- + "model STRING ATTRIBUTE, "
- + "temperature FLOAT FIELD, "
- + "humidity DOUBLE FIELD) with (TTL=3600000)");
-
- session.executeNonQueryStatement(
- "create table table2("
- + "region_id STRING TAG, "
- + "plant_id STRING TAG, "
- + "color STRING ATTRIBUTE, "
- + "temperature FLOAT FIELD, "
- + "speed DOUBLE FIELD) with (TTL=6600000)");
-
- // show tables from current database
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
- }
-
- // show tables by specifying another database
- // using SHOW tables FROM
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES FROM test1")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
- }
-
- // insert table data by tablet
- List<IMeasurementSchema> measurementSchemaList =
- new ArrayList<>(
- Arrays.asList(
- new MeasurementSchema("region_id", TSDataType.STRING),
- new MeasurementSchema("plant_id", TSDataType.STRING),
- new MeasurementSchema("device_id", TSDataType.STRING),
- new MeasurementSchema("model", TSDataType.STRING),
- new MeasurementSchema("temperature", TSDataType.FLOAT),
- new MeasurementSchema("humidity", TSDataType.DOUBLE)));
- List<ColumnCategory> columnTypeList =
- new ArrayList<>(
- Arrays.asList(
- ColumnCategory.TAG,
- ColumnCategory.TAG,
- ColumnCategory.TAG,
- ColumnCategory.ATTRIBUTE,
- ColumnCategory.FIELD,
- ColumnCategory.FIELD));
- Tablet tablet =
- new Tablet(
- "test1",
- IMeasurementSchema.getMeasurementNameList(measurementSchemaList),
- IMeasurementSchema.getDataTypeList(measurementSchemaList),
- 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();
- }
-
- // query table data
- try (SessionDataSet dataSet =
- session.executeQueryStatement(
- "select * from test1 "
- + "where region_id = '1' and plant_id in ('3', '5') and device_id = '3'")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ private static final String LOCAL_URL = "127.0.0.1:6667";
+
+ public static void main(String[] args) {
+
+ // don't specify database in constructor
+ ITableSessionPool tableSessionPool =
+ new TableSessionPoolBuilder()
+ .nodeUrls(Collections.singletonList(LOCAL_URL))
+ .user("root")
+ .password("root")
+ .maxSize(1)
+ .build();
+
+ try (ITableSession session = tableSessionPool.getSession()) {
+
+ session.executeNonQueryStatement("CREATE DATABASE test1");
+ session.executeNonQueryStatement("CREATE DATABASE test2");
+
+ session.executeNonQueryStatement("use test2");
+
+ // or use full qualified table name
+ session.executeNonQueryStatement(
+ "create table test1.table1("
+ + "region_id STRING TAG, "
+ + "plant_id STRING TAG, "
+ + "device_id STRING TAG, "
+ + "model STRING ATTRIBUTE, "
+ + "temperature FLOAT FIELD, "
+ + "humidity DOUBLE FIELD) with (TTL=3600000)");
+
+ session.executeNonQueryStatement(
+ "create table table2("
+ + "region_id STRING TAG, "
+ + "plant_id STRING TAG, "
+ + "color STRING ATTRIBUTE, "
+ + "temperature FLOAT FIELD, "
+ + "speed DOUBLE FIELD) with (TTL=6600000)");
+
+ // show tables from current database
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
+
+ // show tables by specifying another database
+ // using SHOW tables FROM
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES FROM test1")) {
+ printDataSet(dataSet);
+ }
+
+ // insert table data by tablet
+ List<String> columnNameList =
+ Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity");
+ List<TSDataType> dataTypeList =
+ Arrays.asList(
+ TSDataType.STRING,
+ TSDataType.STRING,
+ TSDataType.STRING,
+ TSDataType.STRING,
+ TSDataType.FLOAT,
+ TSDataType.DOUBLE);
+ List<ColumnCategory> columnTypeList =
+ new ArrayList<>(
+ Arrays.asList(
+ ColumnCategory.TAG,
+ ColumnCategory.TAG,
+ ColumnCategory.TAG,
+ ColumnCategory.ATTRIBUTE,
+ ColumnCategory.FIELD,
+ ColumnCategory.FIELD));
+ Tablet tablet = new Tablet("test1", 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();
+ }
+
+ // query table data
+ try (SessionDataSet dataSet =
+ session.executeQueryStatement(
+ "select * from test1 "
+ + "where region_id = '1' and plant_id in ('3', '5') and device_id = '3'")) {
+ printDataSet(dataSet);
+ }
+
+ } catch (IoTDBConnectionException e) {
+ e.printStackTrace();
+ } catch (StatementExecutionException e) {
+ e.printStackTrace();
+ } finally {
+ tableSessionPool.close();
}
- }
-
- } catch (IoTDBConnectionException e) {
- e.printStackTrace();
- } catch (StatementExecutionException e) {
- e.printStackTrace();
- } finally {
- tableSessionPool.close();
- }
- // specify database in constructor
- tableSessionPool =
- new TableSessionPoolBuilder()
- .nodeUrls(Collections.singletonList(LOCAL_URL))
- .user("root")
- .password("root")
- .maxSize(1)
- .database("test1")
- .build();
-
- try (ITableSession session = tableSessionPool.getSession()) {
-
- // show tables from current database
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
- }
-
- // change database to test2
- session.executeNonQueryStatement("use test2");
-
- // show tables by specifying another database
- // using SHOW tables FROM
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ // specify database in constructor
+ tableSessionPool =
+ new TableSessionPoolBuilder()
+ .nodeUrls(Collections.singletonList(LOCAL_URL))
+ .user("root")
+ .password("root")
+ .maxSize(1)
+ .database("test1")
+ .build();
+
+ try (ITableSession session = tableSessionPool.getSession()) {
+
+ // show tables from current database
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
+
+ // change database to test2
+ session.executeNonQueryStatement("use test2");
+
+ // show tables by specifying another database
+ // using SHOW tables FROM
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
+
+ } catch (IoTDBConnectionException e) {
+ e.printStackTrace();
+ } catch (StatementExecutionException e) {
+ e.printStackTrace();
}
- }
- } catch (IoTDBConnectionException e) {
- e.printStackTrace();
- } catch (StatementExecutionException e) {
- e.printStackTrace();
- }
+ try (ITableSession session = tableSessionPool.getSession()) {
- try (ITableSession session = tableSessionPool.getSession()) {
+ // show tables from default database test1
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
- // show tables from default database test1
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ } catch (IoTDBConnectionException e) {
+ e.printStackTrace();
+ } catch (StatementExecutionException e) {
+ e.printStackTrace();
+ } finally {
+ tableSessionPool.close();
}
- }
-
- } catch (IoTDBConnectionException e) {
- e.printStackTrace();
- } catch (StatementExecutionException e) {
- e.printStackTrace();
- } finally {
- tableSessionPool.close();
}
- }
}
```
\ No newline at end of file
diff --git a/src/UserGuide/latest/API/Programming-Java-Native-API.md b/src/UserGuide/latest/API/Programming-Java-Native-API.md
index a1f25e8f..7ef45e59 100644
--- a/src/UserGuide/latest/API/Programming-Java-Native-API.md
+++ b/src/UserGuide/latest/API/Programming-Java-Native-API.md
@@ -308,33 +308,51 @@ public class SessionPoolExample {
sessionPool.executeNonQueryStatement("set TTL to root.test.** 10000");
// 3. delete time series
sessionPool.executeNonQueryStatement("delete timeseries root.test.d1.s1");
- private static void executeQueryExample() throws IoTDBConnectionException, StatementExecutionException {
+ }
+
+ private static void executeQueryExample() throws IoTDBConnectionException, StatementExecutionException {
// 1. execute normal query
try(SessionDataSetWrapper wrapper = sessionPool.executeQueryStatement("select s1 from root.sg1.d1 limit 10")) {
- while (wrapper.hasNext()) {
- System.out.println(wrapper.next());
- }
+ // get DataIterator like JDBC
+ DataIterator dataIterator = wrapper.iterator();
+ System.out.println(wrapper.getColumnNames());
+ System.out.println(wrapper.getColumnTypes());
+ while (dataIterator.next()) {
+ StringBuilder builder = new StringBuilder();
+ for (String columnName : wrapper.getColumnNames()) {
+ builder.append(dataIterator.getString(columnName) + " ");
}
+ System.out.println(builder);
+ }
+ }
// 2. execute aggregate query
try(SessionDataSetWrapper wrapper = sessionPool.executeQueryStatement("select count(s1) from root.sg1.d1 group by ([0, 40), 5ms) ")) {
- while (wrapper.hasNext()) {
- System.out.println(wrapper.next());
+ // get DataIterator like JDBC
+ DataIterator dataIterator = wrapper.iterator();
+ System.out.println(wrapper.getColumnNames());
+ System.out.println(wrapper.getColumnTypes());
+ while (dataIterator.next()) {
+ StringBuilder builder = new StringBuilder();
+ for (String columnName : wrapper.getColumnNames()) {
+ builder.append(dataIterator.getString(columnName) + " ");
+ }
+ System.out.println(builder);
}
}
- }
+ }
- private static void constructSessionPool() {
- // Using nodeUrls ensures that when one node goes down, other nodes are automatically connected to retry
- List<String> nodeUrls = new ArrayList<>();
- nodeUrls.add("127.0.0.1:6667");
- nodeUrls.add("127.0.0.1:6668");
- sessionPool =
- new SessionPool.Builder()
- .nodeUrls(nodeUrls)
- .user("root")
- .password("root")
- .maxSize(3)
- .build();
+ private static void constructSessionPool() {
+ // Using nodeUrls ensures that when one node goes down, other nodes are automatically connected to retry
+ List<String> nodeUrls = new ArrayList<>();
+ nodeUrls.add("127.0.0.1:6667");
+ nodeUrls.add("127.0.0.1:6668");
+ sessionPool =
+ new SessionPool.Builder()
+ .nodeUrls(nodeUrls)
+ .user("root")
+ .password("root")
+ .maxSize(3)
+ .build();
}
public static void closeSessionPool(){
diff --git a/src/zh/UserGuide/Master/Table/API/Programming-Java-Native-API_apache.md b/src/zh/UserGuide/Master/Table/API/Programming-Java-Native-API_apache.md
index ca2005c9..d2b82af5 100644
--- a/src/zh/UserGuide/Master/Table/API/Programming-Java-Native-API_apache.md
+++ b/src/zh/UserGuide/Master/Table/API/Programming-Java-Native-API_apache.md
@@ -629,9 +629,9 @@ public class TableSessionPoolBuilder {
## 5. 示例代码
-Session 示例代码:[src/main/java/org/apache/iotdb/TableModelSessionExample.java](https://github.com/apache/iotdb/blob/rc/2.0.1/example/session/src/main/java/org/apache/iotdb/TableModelSessionExample.java)
+Session 示例代码:[src/main/java/org/apache/iotdb/TableModelSessionExample.java](https://github.com/apache/iotdb/blob/master/example/session/src/main/java/org/apache/iotdb/TableModelSessionExample.java)
-SessionPool 示例代码:[src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java](https://github.com/apache/iotdb/blob/rc/2.0.1/example/session/src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java)
+SessionPool 示例代码:[src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java](https://github.com/apache/iotdb/blob/master/example/session/src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java)
```Java
/*
@@ -662,199 +662,170 @@ import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.pool.TableSessionPoolBuilder;
+import org.apache.tsfile.enums.ColumnCategory;
import org.apache.tsfile.enums.TSDataType;
import org.apache.tsfile.write.record.Tablet;
-import org.apache.tsfile.write.record.Tablet.ColumnCategory;
-import org.apache.tsfile.write.schema.IMeasurementSchema;
-import org.apache.tsfile.write.schema.MeasurementSchema;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
+import static org.apache.iotdb.SessionExample.printDataSet;
+
public class TableModelSessionPoolExample {
- private static final String LOCAL_URL = "127.0.0.1:6667";
-
- public static void main(String[] args) {
-
- // don't specify database in constructor
- ITableSessionPool tableSessionPool =
- new TableSessionPoolBuilder()
- .nodeUrls(Collections.singletonList(LOCAL_URL))
- .user("root")
- .password("root")
- .maxSize(1)
- .build();
-
- try (ITableSession session = tableSessionPool.getSession()) {
-
- session.executeNonQueryStatement("CREATE DATABASE test1");
- session.executeNonQueryStatement("CREATE DATABASE test2");
-
- session.executeNonQueryStatement("use test2");
-
- // or use full qualified table name
- session.executeNonQueryStatement(
- "create table test1.table1("
- + "region_id STRING TAG, "
- + "plant_id STRING TAG, "
- + "device_id STRING TAG, "
- + "model STRING ATTRIBUTE, "
- + "temperature FLOAT FIELD, "
- + "humidity DOUBLE FIELD) with (TTL=3600000)");
-
- session.executeNonQueryStatement(
- "create table table2("
- + "region_id STRING TAG, "
- + "plant_id STRING TAG, "
- + "color STRING ATTRIBUTE, "
- + "temperature FLOAT FIELD, "
- + "speed DOUBLE FIELD) with (TTL=6600000)");
-
- // show tables from current database
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
- }
-
- // show tables by specifying another database
- // using SHOW tables FROM
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES FROM test1")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
- }
-
- // insert table data by tablet
- List<IMeasurementSchema> measurementSchemaList =
- new ArrayList<>(
- Arrays.asList(
- new MeasurementSchema("region_id", TSDataType.STRING),
- new MeasurementSchema("plant_id", TSDataType.STRING),
- new MeasurementSchema("device_id", TSDataType.STRING),
- new MeasurementSchema("model", TSDataType.STRING),
- new MeasurementSchema("temperature", TSDataType.FLOAT),
- new MeasurementSchema("humidity", TSDataType.DOUBLE)));
- List<ColumnCategory> columnTypeList =
- new ArrayList<>(
- Arrays.asList(
- ColumnCategory.TAG,
- ColumnCategory.TAG,
- ColumnCategory.TAG,
- ColumnCategory.ATTRIBUTE,
- ColumnCategory.FIELD,
- ColumnCategory.FIELD));
- Tablet tablet =
- new Tablet(
- "test1",
- IMeasurementSchema.getMeasurementNameList(measurementSchemaList),
- IMeasurementSchema.getDataTypeList(measurementSchemaList),
- 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();
- }
-
- // query table data
- try (SessionDataSet dataSet =
- session.executeQueryStatement(
- "select * from test1 "
- + "where region_id = '1' and plant_id in ('3', '5') and device_id = '3'")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ private static final String LOCAL_URL = "127.0.0.1:6667";
+
+ public static void main(String[] args) {
+
+ // don't specify database in constructor
+ ITableSessionPool tableSessionPool =
+ new TableSessionPoolBuilder()
+ .nodeUrls(Collections.singletonList(LOCAL_URL))
+ .user("root")
+ .password("root")
+ .maxSize(1)
+ .build();
+
+ try (ITableSession session = tableSessionPool.getSession()) {
+
+ session.executeNonQueryStatement("CREATE DATABASE test1");
+ session.executeNonQueryStatement("CREATE DATABASE test2");
+
+ session.executeNonQueryStatement("use test2");
+
+ // or use full qualified table name
+ session.executeNonQueryStatement(
+ "create table test1.table1("
+ + "region_id STRING TAG, "
+ + "plant_id STRING TAG, "
+ + "device_id STRING TAG, "
+ + "model STRING ATTRIBUTE, "
+ + "temperature FLOAT FIELD, "
+ + "humidity DOUBLE FIELD) with (TTL=3600000)");
+
+ session.executeNonQueryStatement(
+ "create table table2("
+ + "region_id STRING TAG, "
+ + "plant_id STRING TAG, "
+ + "color STRING ATTRIBUTE, "
+ + "temperature FLOAT FIELD, "
+ + "speed DOUBLE FIELD) with (TTL=6600000)");
+
+ // show tables from current database
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
+
+ // show tables by specifying another database
+ // using SHOW tables FROM
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES FROM test1")) {
+ printDataSet(dataSet);
+ }
+
+ // insert table data by tablet
+ List<String> columnNameList =
+ Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity");
+ List<TSDataType> dataTypeList =
+ Arrays.asList(
+ TSDataType.STRING,
+ TSDataType.STRING,
+ TSDataType.STRING,
+ TSDataType.STRING,
+ TSDataType.FLOAT,
+ TSDataType.DOUBLE);
+ List<ColumnCategory> columnTypeList =
+ new ArrayList<>(
+ Arrays.asList(
+ ColumnCategory.TAG,
+ ColumnCategory.TAG,
+ ColumnCategory.TAG,
+ ColumnCategory.ATTRIBUTE,
+ ColumnCategory.FIELD,
+ ColumnCategory.FIELD));
+ Tablet tablet = new Tablet("test1", 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();
+ }
+
+ // query table data
+ try (SessionDataSet dataSet =
+ session.executeQueryStatement(
+ "select * from test1 "
+ + "where region_id = '1' and plant_id in ('3', '5') and device_id = '3'")) {
+ printDataSet(dataSet);
+ }
+
+ } catch (IoTDBConnectionException e) {
+ e.printStackTrace();
+ } catch (StatementExecutionException e) {
+ e.printStackTrace();
+ } finally {
+ tableSessionPool.close();
}
- }
-
- } catch (IoTDBConnectionException e) {
- e.printStackTrace();
- } catch (StatementExecutionException e) {
- e.printStackTrace();
- } finally {
- tableSessionPool.close();
- }
- // specify database in constructor
- tableSessionPool =
- new TableSessionPoolBuilder()
- .nodeUrls(Collections.singletonList(LOCAL_URL))
- .user("root")
- .password("root")
- .maxSize(1)
- .database("test1")
- .build();
-
- try (ITableSession session = tableSessionPool.getSession()) {
-
- // show tables from current database
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
- }
-
- // change database to test2
- session.executeNonQueryStatement("use test2");
-
- // show tables by specifying another database
- // using SHOW tables FROM
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ // specify database in constructor
+ tableSessionPool =
+ new TableSessionPoolBuilder()
+ .nodeUrls(Collections.singletonList(LOCAL_URL))
+ .user("root")
+ .password("root")
+ .maxSize(1)
+ .database("test1")
+ .build();
+
+ try (ITableSession session = tableSessionPool.getSession()) {
+
+ // show tables from current database
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
+
+ // change database to test2
+ session.executeNonQueryStatement("use test2");
+
+ // show tables by specifying another database
+ // using SHOW tables FROM
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
+
+ } catch (IoTDBConnectionException e) {
+ e.printStackTrace();
+ } catch (StatementExecutionException e) {
+ e.printStackTrace();
}
- }
- } catch (IoTDBConnectionException e) {
- e.printStackTrace();
- } catch (StatementExecutionException e) {
- e.printStackTrace();
- }
+ try (ITableSession session = tableSessionPool.getSession()) {
- try (ITableSession session = tableSessionPool.getSession()) {
+ // show tables from default database test1
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
- // show tables from default database test1
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ } catch (IoTDBConnectionException e) {
+ e.printStackTrace();
+ } catch (StatementExecutionException e) {
+ e.printStackTrace();
+ } finally {
+ tableSessionPool.close();
}
- }
-
- } catch (IoTDBConnectionException e) {
- e.printStackTrace();
- } catch (StatementExecutionException e) {
- e.printStackTrace();
- } finally {
- tableSessionPool.close();
}
- }
}
```
\ No newline at end of file
diff --git a/src/zh/UserGuide/Master/Table/API/Programming-Java-Native-API_timecho.md b/src/zh/UserGuide/Master/Table/API/Programming-Java-Native-API_timecho.md
index ae979081..49f63cdb 100644
--- a/src/zh/UserGuide/Master/Table/API/Programming-Java-Native-API_timecho.md
+++ b/src/zh/UserGuide/Master/Table/API/Programming-Java-Native-API_timecho.md
@@ -629,9 +629,9 @@ public class TableSessionPoolBuilder {
## 5. 示例代码
-Session 示例代码:[src/main/java/org/apache/iotdb/TableModelSessionExample.java](https://github.com/apache/iotdb/blob/rc/2.0.1/example/session/src/main/java/org/apache/iotdb/TableModelSessionExample.java)
+Session 示例代码:[src/main/java/org/apache/iotdb/TableModelSessionExample.java](https://github.com/apache/iotdb/blob/master/example/session/src/main/java/org/apache/iotdb/TableModelSessionExample.java)
-SessionPool 示例代码:[src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java](https://github.com/apache/iotdb/blob/rc/2.0.1/example/session/src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java)
+SessionPool 示例代码:[src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java](https://github.com/apache/iotdb/blob/master/example/session/src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java)
```Java
/*
@@ -662,199 +662,170 @@ import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.pool.TableSessionPoolBuilder;
+import org.apache.tsfile.enums.ColumnCategory;
import org.apache.tsfile.enums.TSDataType;
import org.apache.tsfile.write.record.Tablet;
-import org.apache.tsfile.write.record.Tablet.ColumnCategory;
-import org.apache.tsfile.write.schema.IMeasurementSchema;
-import org.apache.tsfile.write.schema.MeasurementSchema;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
+import static org.apache.iotdb.SessionExample.printDataSet;
+
public class TableModelSessionPoolExample {
- private static final String LOCAL_URL = "127.0.0.1:6667";
-
- public static void main(String[] args) {
-
- // don't specify database in constructor
- ITableSessionPool tableSessionPool =
- new TableSessionPoolBuilder()
- .nodeUrls(Collections.singletonList(LOCAL_URL))
- .user("root")
- .password("root")
- .maxSize(1)
- .build();
-
- try (ITableSession session = tableSessionPool.getSession()) {
-
- session.executeNonQueryStatement("CREATE DATABASE test1");
- session.executeNonQueryStatement("CREATE DATABASE test2");
-
- session.executeNonQueryStatement("use test2");
-
- // or use full qualified table name
- session.executeNonQueryStatement(
- "create table test1.table1("
- + "region_id STRING TAG, "
- + "plant_id STRING TAG, "
- + "device_id STRING TAG, "
- + "model STRING ATTRIBUTE, "
- + "temperature FLOAT FIELD, "
- + "humidity DOUBLE FIELD) with (TTL=3600000)");
-
- session.executeNonQueryStatement(
- "create table table2("
- + "region_id STRING TAG, "
- + "plant_id STRING TAG, "
- + "color STRING ATTRIBUTE, "
- + "temperature FLOAT FIELD, "
- + "speed DOUBLE FIELD) with (TTL=6600000)");
-
- // show tables from current database
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
- }
-
- // show tables by specifying another database
- // using SHOW tables FROM
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES FROM test1")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
- }
-
- // insert table data by tablet
- List<IMeasurementSchema> measurementSchemaList =
- new ArrayList<>(
- Arrays.asList(
- new MeasurementSchema("region_id", TSDataType.STRING),
- new MeasurementSchema("plant_id", TSDataType.STRING),
- new MeasurementSchema("device_id", TSDataType.STRING),
- new MeasurementSchema("model", TSDataType.STRING),
- new MeasurementSchema("temperature", TSDataType.FLOAT),
- new MeasurementSchema("humidity", TSDataType.DOUBLE)));
- List<ColumnCategory> columnTypeList =
- new ArrayList<>(
- Arrays.asList(
- ColumnCategory.TAG,
- ColumnCategory.TAG,
- ColumnCategory.TAG,
- ColumnCategory.ATTRIBUTE,
- ColumnCategory.FIELD,
- ColumnCategory.FIELD));
- Tablet tablet =
- new Tablet(
- "test1",
- IMeasurementSchema.getMeasurementNameList(measurementSchemaList),
- IMeasurementSchema.getDataTypeList(measurementSchemaList),
- 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();
- }
-
- // query table data
- try (SessionDataSet dataSet =
- session.executeQueryStatement(
- "select * from test1 "
- + "where region_id = '1' and plant_id in ('3', '5') and device_id = '3'")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ private static final String LOCAL_URL = "127.0.0.1:6667";
+
+ public static void main(String[] args) {
+
+ // don't specify database in constructor
+ ITableSessionPool tableSessionPool =
+ new TableSessionPoolBuilder()
+ .nodeUrls(Collections.singletonList(LOCAL_URL))
+ .user("root")
+ .password("root")
+ .maxSize(1)
+ .build();
+
+ try (ITableSession session = tableSessionPool.getSession()) {
+
+ session.executeNonQueryStatement("CREATE DATABASE test1");
+ session.executeNonQueryStatement("CREATE DATABASE test2");
+
+ session.executeNonQueryStatement("use test2");
+
+ // or use full qualified table name
+ session.executeNonQueryStatement(
+ "create table test1.table1("
+ + "region_id STRING TAG, "
+ + "plant_id STRING TAG, "
+ + "device_id STRING TAG, "
+ + "model STRING ATTRIBUTE, "
+ + "temperature FLOAT FIELD, "
+ + "humidity DOUBLE FIELD) with (TTL=3600000)");
+
+ session.executeNonQueryStatement(
+ "create table table2("
+ + "region_id STRING TAG, "
+ + "plant_id STRING TAG, "
+ + "color STRING ATTRIBUTE, "
+ + "temperature FLOAT FIELD, "
+ + "speed DOUBLE FIELD) with (TTL=6600000)");
+
+ // show tables from current database
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
+
+ // show tables by specifying another database
+ // using SHOW tables FROM
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES FROM test1")) {
+ printDataSet(dataSet);
+ }
+
+ // insert table data by tablet
+ List<String> columnNameList =
+ Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity");
+ List<TSDataType> dataTypeList =
+ Arrays.asList(
+ TSDataType.STRING,
+ TSDataType.STRING,
+ TSDataType.STRING,
+ TSDataType.STRING,
+ TSDataType.FLOAT,
+ TSDataType.DOUBLE);
+ List<ColumnCategory> columnTypeList =
+ new ArrayList<>(
+ Arrays.asList(
+ ColumnCategory.TAG,
+ ColumnCategory.TAG,
+ ColumnCategory.TAG,
+ ColumnCategory.ATTRIBUTE,
+ ColumnCategory.FIELD,
+ ColumnCategory.FIELD));
+ Tablet tablet = new Tablet("test1", 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();
+ }
+
+ // query table data
+ try (SessionDataSet dataSet =
+ session.executeQueryStatement(
+ "select * from test1 "
+ + "where region_id = '1' and plant_id in ('3', '5') and device_id = '3'")) {
+ printDataSet(dataSet);
+ }
+
+ } catch (IoTDBConnectionException e) {
+ e.printStackTrace();
+ } catch (StatementExecutionException e) {
+ e.printStackTrace();
+ } finally {
+ tableSessionPool.close();
}
- }
-
- } catch (IoTDBConnectionException e) {
- e.printStackTrace();
- } catch (StatementExecutionException e) {
- e.printStackTrace();
- } finally {
- tableSessionPool.close();
- }
- // specify database in constructor
- tableSessionPool =
- new TableSessionPoolBuilder()
- .nodeUrls(Collections.singletonList(LOCAL_URL))
- .user("root")
- .password("root")
- .maxSize(1)
- .database("test1")
- .build();
-
- try (ITableSession session = tableSessionPool.getSession()) {
-
- // show tables from current database
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
- }
-
- // change database to test2
- session.executeNonQueryStatement("use test2");
-
- // show tables by specifying another database
- // using SHOW tables FROM
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ // specify database in constructor
+ tableSessionPool =
+ new TableSessionPoolBuilder()
+ .nodeUrls(Collections.singletonList(LOCAL_URL))
+ .user("root")
+ .password("root")
+ .maxSize(1)
+ .database("test1")
+ .build();
+
+ try (ITableSession session = tableSessionPool.getSession()) {
+
+ // show tables from current database
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
+
+ // change database to test2
+ session.executeNonQueryStatement("use test2");
+
+ // show tables by specifying another database
+ // using SHOW tables FROM
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
+
+ } catch (IoTDBConnectionException e) {
+ e.printStackTrace();
+ } catch (StatementExecutionException e) {
+ e.printStackTrace();
}
- }
- } catch (IoTDBConnectionException e) {
- e.printStackTrace();
- } catch (StatementExecutionException e) {
- e.printStackTrace();
- }
+ try (ITableSession session = tableSessionPool.getSession()) {
- try (ITableSession session = tableSessionPool.getSession()) {
+ // show tables from default database test1
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
- // show tables from default database test1
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ } catch (IoTDBConnectionException e) {
+ e.printStackTrace();
+ } catch (StatementExecutionException e) {
+ e.printStackTrace();
+ } finally {
+ tableSessionPool.close();
}
- }
-
- } catch (IoTDBConnectionException e) {
- e.printStackTrace();
- } catch (StatementExecutionException e) {
- e.printStackTrace();
- } finally {
- tableSessionPool.close();
}
- }
}
```
\ No newline at end of file
diff --git a/src/zh/UserGuide/Master/Tree/API/Programming-Java-Native-API.md b/src/zh/UserGuide/Master/Tree/API/Programming-Java-Native-API.md
index 8a9f27d0..e0e65cf2 100644
--- a/src/zh/UserGuide/Master/Tree/API/Programming-Java-Native-API.md
+++ b/src/zh/UserGuide/Master/Tree/API/Programming-Java-Native-API.md
@@ -306,33 +306,51 @@ public class SessionPoolExample {
sessionPool.executeNonQueryStatement("set TTL to root.test.** 10000");
// 3. delete time series
sessionPool.executeNonQueryStatement("delete timeseries root.test.d1.s1");
- private static void executeQueryExample() throws IoTDBConnectionException, StatementExecutionException {
+ }
+
+ private static void executeQueryExample() throws IoTDBConnectionException, StatementExecutionException {
// 1. execute normal query
try(SessionDataSetWrapper wrapper = sessionPool.executeQueryStatement("select s1 from root.sg1.d1 limit 10")) {
- while (wrapper.hasNext()) {
- System.out.println(wrapper.next());
- }
+ // get DataIterator like JDBC
+ DataIterator dataIterator = wrapper.iterator();
+ System.out.println(wrapper.getColumnNames());
+ System.out.println(wrapper.getColumnTypes());
+ while (dataIterator.next()) {
+ StringBuilder builder = new StringBuilder();
+ for (String columnName : wrapper.getColumnNames()) {
+ builder.append(dataIterator.getString(columnName) + " ");
}
+ System.out.println(builder);
+ }
+ }
// 2. execute aggregate query
try(SessionDataSetWrapper wrapper = sessionPool.executeQueryStatement("select count(s1) from root.sg1.d1 group by ([0, 40), 5ms) ")) {
- while (wrapper.hasNext()) {
- System.out.println(wrapper.next());
+ // get DataIterator like JDBC
+ DataIterator dataIterator = wrapper.iterator();
+ System.out.println(wrapper.getColumnNames());
+ System.out.println(wrapper.getColumnTypes());
+ while (dataIterator.next()) {
+ StringBuilder builder = new StringBuilder();
+ for (String columnName : wrapper.getColumnNames()) {
+ builder.append(dataIterator.getString(columnName) + " ");
+ }
+ System.out.println(builder);
}
}
- }
+ }
- private static void constructSessionPool() {
- // Using nodeUrls ensures that when one node goes down, other nodes are automatically connected to retry
- List<String> nodeUrls = new ArrayList<>();
- nodeUrls.add("127.0.0.1:6667");
- nodeUrls.add("127.0.0.1:6668");
- sessionPool =
- new SessionPool.Builder()
- .nodeUrls(nodeUrls)
- .user("root")
- .password("root")
- .maxSize(3)
- .build();
+ private static void constructSessionPool() {
+ // Using nodeUrls ensures that when one node goes down, other nodes are automatically connected to retry
+ List<String> nodeUrls = new ArrayList<>();
+ nodeUrls.add("127.0.0.1:6667");
+ nodeUrls.add("127.0.0.1:6668");
+ sessionPool =
+ new SessionPool.Builder()
+ .nodeUrls(nodeUrls)
+ .user("root")
+ .password("root")
+ .maxSize(3)
+ .build();
}
public static void closeSessionPool(){
diff --git a/src/zh/UserGuide/V1.3.x/API/Programming-Java-Native-API.md b/src/zh/UserGuide/V1.3.x/API/Programming-Java-Native-API.md
index 628c47ea..5459c77f 100644
--- a/src/zh/UserGuide/V1.3.x/API/Programming-Java-Native-API.md
+++ b/src/zh/UserGuide/V1.3.x/API/Programming-Java-Native-API.md
@@ -306,22 +306,40 @@ public class SessionPoolExample {
sessionPool.executeNonQueryStatement("set TTL to root.test.** 10000");
// 3. delete time series
sessionPool.executeNonQueryStatement("delete timeseries root.test.d1.s1");
- private static void executeQueryExample() throws IoTDBConnectionException, StatementExecutionException {
+ }
+
+ private static void executeQueryExample() throws IoTDBConnectionException, StatementExecutionException {
// 1. execute normal query
try(SessionDataSetWrapper wrapper = sessionPool.executeQueryStatement("select s1 from root.sg1.d1 limit 10")) {
- while (wrapper.hasNext()) {
- System.out.println(wrapper.next());
- }
+ // get DataIterator like JDBC
+ DataIterator dataIterator = wrapper.iterator();
+ System.out.println(wrapper.getColumnNames());
+ System.out.println(wrapper.getColumnTypes());
+ while (dataIterator.next()) {
+ StringBuilder builder = new StringBuilder();
+ for (String columnName : wrapper.getColumnNames()) {
+ builder.append(dataIterator.getString(columnName) + " ");
}
+ System.out.println(builder);
+ }
+ }
// 2. execute aggregate query
try(SessionDataSetWrapper wrapper = sessionPool.executeQueryStatement("select count(s1) from root.sg1.d1 group by ([0, 40), 5ms) ")) {
- while (wrapper.hasNext()) {
- System.out.println(wrapper.next());
+ // get DataIterator like JDBC
+ DataIterator dataIterator = wrapper.iterator();
+ System.out.println(wrapper.getColumnNames());
+ System.out.println(wrapper.getColumnTypes());
+ while (dataIterator.next()) {
+ StringBuilder builder = new StringBuilder();
+ for (String columnName : wrapper.getColumnNames()) {
+ builder.append(dataIterator.getString(columnName) + " ");
+ }
+ System.out.println(builder);
}
}
- }
-
- private static void constructSessionPool() {
+ }
+
+ private static void constructSessionPool() {
// Using nodeUrls ensures that when one node goes down, other nodes are automatically connected to retry
List<String> nodeUrls = new ArrayList<>();
nodeUrls.add("127.0.0.1:6667");
diff --git a/src/zh/UserGuide/dev-1.3/API/Programming-Java-Native-API.md b/src/zh/UserGuide/dev-1.3/API/Programming-Java-Native-API.md
index 668fc0c7..53a23250 100644
--- a/src/zh/UserGuide/dev-1.3/API/Programming-Java-Native-API.md
+++ b/src/zh/UserGuide/dev-1.3/API/Programming-Java-Native-API.md
@@ -306,33 +306,51 @@ public class SessionPoolExample {
sessionPool.executeNonQueryStatement("set TTL to root.test.** 10000");
// 3. delete time series
sessionPool.executeNonQueryStatement("delete timeseries root.test.d1.s1");
- private static void executeQueryExample() throws IoTDBConnectionException, StatementExecutionException {
+ }
+
+ private static void executeQueryExample() throws IoTDBConnectionException, StatementExecutionException {
// 1. execute normal query
try(SessionDataSetWrapper wrapper = sessionPool.executeQueryStatement("select s1 from root.sg1.d1 limit 10")) {
- while (wrapper.hasNext()) {
- System.out.println(wrapper.next());
- }
+ // get DataIterator like JDBC
+ DataIterator dataIterator = wrapper.iterator();
+ System.out.println(wrapper.getColumnNames());
+ System.out.println(wrapper.getColumnTypes());
+ while (dataIterator.next()) {
+ StringBuilder builder = new StringBuilder();
+ for (String columnName : wrapper.getColumnNames()) {
+ builder.append(dataIterator.getString(columnName) + " ");
}
+ System.out.println(builder);
+ }
+ }
// 2. execute aggregate query
try(SessionDataSetWrapper wrapper = sessionPool.executeQueryStatement("select count(s1) from root.sg1.d1 group by ([0, 40), 5ms) ")) {
- while (wrapper.hasNext()) {
- System.out.println(wrapper.next());
+ // get DataIterator like JDBC
+ DataIterator dataIterator = wrapper.iterator();
+ System.out.println(wrapper.getColumnNames());
+ System.out.println(wrapper.getColumnTypes());
+ while (dataIterator.next()) {
+ StringBuilder builder = new StringBuilder();
+ for (String columnName : wrapper.getColumnNames()) {
+ builder.append(dataIterator.getString(columnName) + " ");
+ }
+ System.out.println(builder);
}
}
- }
-
- private static void constructSessionPool() {
- // Using nodeUrls ensures that when one node goes down, other nodes are automatically connected to retry
- List<String> nodeUrls = new ArrayList<>();
- nodeUrls.add("127.0.0.1:6667");
- nodeUrls.add("127.0.0.1:6668");
- sessionPool =
- new SessionPool.Builder()
- .nodeUrls(nodeUrls)
- .user("root")
- .password("root")
- .maxSize(3)
- .build();
+ }
+
+ private static void constructSessionPool() {
+ // Using nodeUrls ensures that when one node goes down, other nodes are automatically connected to retry
+ List<String> nodeUrls = new ArrayList<>();
+ nodeUrls.add("127.0.0.1:6667");
+ nodeUrls.add("127.0.0.1:6668");
+ sessionPool =
+ new SessionPool.Builder()
+ .nodeUrls(nodeUrls)
+ .user("root")
+ .password("root")
+ .maxSize(3)
+ .build();
}
public static void closeSessionPool(){
diff --git a/src/zh/UserGuide/latest-Table/API/Programming-Java-Native-API_apache.md b/src/zh/UserGuide/latest-Table/API/Programming-Java-Native-API_apache.md
index ca2005c9..d2b82af5 100644
--- a/src/zh/UserGuide/latest-Table/API/Programming-Java-Native-API_apache.md
+++ b/src/zh/UserGuide/latest-Table/API/Programming-Java-Native-API_apache.md
@@ -629,9 +629,9 @@ public class TableSessionPoolBuilder {
## 5. 示例代码
-Session 示例代码:[src/main/java/org/apache/iotdb/TableModelSessionExample.java](https://github.com/apache/iotdb/blob/rc/2.0.1/example/session/src/main/java/org/apache/iotdb/TableModelSessionExample.java)
+Session 示例代码:[src/main/java/org/apache/iotdb/TableModelSessionExample.java](https://github.com/apache/iotdb/blob/master/example/session/src/main/java/org/apache/iotdb/TableModelSessionExample.java)
-SessionPool 示例代码:[src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java](https://github.com/apache/iotdb/blob/rc/2.0.1/example/session/src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java)
+SessionPool 示例代码:[src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java](https://github.com/apache/iotdb/blob/master/example/session/src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java)
```Java
/*
@@ -662,199 +662,170 @@ import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.pool.TableSessionPoolBuilder;
+import org.apache.tsfile.enums.ColumnCategory;
import org.apache.tsfile.enums.TSDataType;
import org.apache.tsfile.write.record.Tablet;
-import org.apache.tsfile.write.record.Tablet.ColumnCategory;
-import org.apache.tsfile.write.schema.IMeasurementSchema;
-import org.apache.tsfile.write.schema.MeasurementSchema;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
+import static org.apache.iotdb.SessionExample.printDataSet;
+
public class TableModelSessionPoolExample {
- private static final String LOCAL_URL = "127.0.0.1:6667";
-
- public static void main(String[] args) {
-
- // don't specify database in constructor
- ITableSessionPool tableSessionPool =
- new TableSessionPoolBuilder()
- .nodeUrls(Collections.singletonList(LOCAL_URL))
- .user("root")
- .password("root")
- .maxSize(1)
- .build();
-
- try (ITableSession session = tableSessionPool.getSession()) {
-
- session.executeNonQueryStatement("CREATE DATABASE test1");
- session.executeNonQueryStatement("CREATE DATABASE test2");
-
- session.executeNonQueryStatement("use test2");
-
- // or use full qualified table name
- session.executeNonQueryStatement(
- "create table test1.table1("
- + "region_id STRING TAG, "
- + "plant_id STRING TAG, "
- + "device_id STRING TAG, "
- + "model STRING ATTRIBUTE, "
- + "temperature FLOAT FIELD, "
- + "humidity DOUBLE FIELD) with (TTL=3600000)");
-
- session.executeNonQueryStatement(
- "create table table2("
- + "region_id STRING TAG, "
- + "plant_id STRING TAG, "
- + "color STRING ATTRIBUTE, "
- + "temperature FLOAT FIELD, "
- + "speed DOUBLE FIELD) with (TTL=6600000)");
-
- // show tables from current database
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
- }
-
- // show tables by specifying another database
- // using SHOW tables FROM
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES FROM test1")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
- }
-
- // insert table data by tablet
- List<IMeasurementSchema> measurementSchemaList =
- new ArrayList<>(
- Arrays.asList(
- new MeasurementSchema("region_id", TSDataType.STRING),
- new MeasurementSchema("plant_id", TSDataType.STRING),
- new MeasurementSchema("device_id", TSDataType.STRING),
- new MeasurementSchema("model", TSDataType.STRING),
- new MeasurementSchema("temperature", TSDataType.FLOAT),
- new MeasurementSchema("humidity", TSDataType.DOUBLE)));
- List<ColumnCategory> columnTypeList =
- new ArrayList<>(
- Arrays.asList(
- ColumnCategory.TAG,
- ColumnCategory.TAG,
- ColumnCategory.TAG,
- ColumnCategory.ATTRIBUTE,
- ColumnCategory.FIELD,
- ColumnCategory.FIELD));
- Tablet tablet =
- new Tablet(
- "test1",
- IMeasurementSchema.getMeasurementNameList(measurementSchemaList),
- IMeasurementSchema.getDataTypeList(measurementSchemaList),
- 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();
- }
-
- // query table data
- try (SessionDataSet dataSet =
- session.executeQueryStatement(
- "select * from test1 "
- + "where region_id = '1' and plant_id in ('3', '5') and device_id = '3'")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ private static final String LOCAL_URL = "127.0.0.1:6667";
+
+ public static void main(String[] args) {
+
+ // don't specify database in constructor
+ ITableSessionPool tableSessionPool =
+ new TableSessionPoolBuilder()
+ .nodeUrls(Collections.singletonList(LOCAL_URL))
+ .user("root")
+ .password("root")
+ .maxSize(1)
+ .build();
+
+ try (ITableSession session = tableSessionPool.getSession()) {
+
+ session.executeNonQueryStatement("CREATE DATABASE test1");
+ session.executeNonQueryStatement("CREATE DATABASE test2");
+
+ session.executeNonQueryStatement("use test2");
+
+ // or use full qualified table name
+ session.executeNonQueryStatement(
+ "create table test1.table1("
+ + "region_id STRING TAG, "
+ + "plant_id STRING TAG, "
+ + "device_id STRING TAG, "
+ + "model STRING ATTRIBUTE, "
+ + "temperature FLOAT FIELD, "
+ + "humidity DOUBLE FIELD) with (TTL=3600000)");
+
+ session.executeNonQueryStatement(
+ "create table table2("
+ + "region_id STRING TAG, "
+ + "plant_id STRING TAG, "
+ + "color STRING ATTRIBUTE, "
+ + "temperature FLOAT FIELD, "
+ + "speed DOUBLE FIELD) with (TTL=6600000)");
+
+ // show tables from current database
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
+
+ // show tables by specifying another database
+ // using SHOW tables FROM
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES FROM test1")) {
+ printDataSet(dataSet);
+ }
+
+ // insert table data by tablet
+ List<String> columnNameList =
+ Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity");
+ List<TSDataType> dataTypeList =
+ Arrays.asList(
+ TSDataType.STRING,
+ TSDataType.STRING,
+ TSDataType.STRING,
+ TSDataType.STRING,
+ TSDataType.FLOAT,
+ TSDataType.DOUBLE);
+ List<ColumnCategory> columnTypeList =
+ new ArrayList<>(
+ Arrays.asList(
+ ColumnCategory.TAG,
+ ColumnCategory.TAG,
+ ColumnCategory.TAG,
+ ColumnCategory.ATTRIBUTE,
+ ColumnCategory.FIELD,
+ ColumnCategory.FIELD));
+ Tablet tablet = new Tablet("test1", 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();
+ }
+
+ // query table data
+ try (SessionDataSet dataSet =
+ session.executeQueryStatement(
+ "select * from test1 "
+ + "where region_id = '1' and plant_id in ('3', '5') and device_id = '3'")) {
+ printDataSet(dataSet);
+ }
+
+ } catch (IoTDBConnectionException e) {
+ e.printStackTrace();
+ } catch (StatementExecutionException e) {
+ e.printStackTrace();
+ } finally {
+ tableSessionPool.close();
}
- }
-
- } catch (IoTDBConnectionException e) {
- e.printStackTrace();
- } catch (StatementExecutionException e) {
- e.printStackTrace();
- } finally {
- tableSessionPool.close();
- }
- // specify database in constructor
- tableSessionPool =
- new TableSessionPoolBuilder()
- .nodeUrls(Collections.singletonList(LOCAL_URL))
- .user("root")
- .password("root")
- .maxSize(1)
- .database("test1")
- .build();
-
- try (ITableSession session = tableSessionPool.getSession()) {
-
- // show tables from current database
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
- }
-
- // change database to test2
- session.executeNonQueryStatement("use test2");
-
- // show tables by specifying another database
- // using SHOW tables FROM
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ // specify database in constructor
+ tableSessionPool =
+ new TableSessionPoolBuilder()
+ .nodeUrls(Collections.singletonList(LOCAL_URL))
+ .user("root")
+ .password("root")
+ .maxSize(1)
+ .database("test1")
+ .build();
+
+ try (ITableSession session = tableSessionPool.getSession()) {
+
+ // show tables from current database
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
+
+ // change database to test2
+ session.executeNonQueryStatement("use test2");
+
+ // show tables by specifying another database
+ // using SHOW tables FROM
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
+
+ } catch (IoTDBConnectionException e) {
+ e.printStackTrace();
+ } catch (StatementExecutionException e) {
+ e.printStackTrace();
}
- }
- } catch (IoTDBConnectionException e) {
- e.printStackTrace();
- } catch (StatementExecutionException e) {
- e.printStackTrace();
- }
+ try (ITableSession session = tableSessionPool.getSession()) {
- try (ITableSession session = tableSessionPool.getSession()) {
+ // show tables from default database test1
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
- // show tables from default database test1
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ } catch (IoTDBConnectionException e) {
+ e.printStackTrace();
+ } catch (StatementExecutionException e) {
+ e.printStackTrace();
+ } finally {
+ tableSessionPool.close();
}
- }
-
- } catch (IoTDBConnectionException e) {
- e.printStackTrace();
- } catch (StatementExecutionException e) {
- e.printStackTrace();
- } finally {
- tableSessionPool.close();
}
- }
}
```
\ No newline at end of file
diff --git a/src/zh/UserGuide/latest-Table/API/Programming-Java-Native-API_timecho.md b/src/zh/UserGuide/latest-Table/API/Programming-Java-Native-API_timecho.md
index ae979081..49f63cdb 100644
--- a/src/zh/UserGuide/latest-Table/API/Programming-Java-Native-API_timecho.md
+++ b/src/zh/UserGuide/latest-Table/API/Programming-Java-Native-API_timecho.md
@@ -629,9 +629,9 @@ public class TableSessionPoolBuilder {
## 5. 示例代码
-Session 示例代码:[src/main/java/org/apache/iotdb/TableModelSessionExample.java](https://github.com/apache/iotdb/blob/rc/2.0.1/example/session/src/main/java/org/apache/iotdb/TableModelSessionExample.java)
+Session 示例代码:[src/main/java/org/apache/iotdb/TableModelSessionExample.java](https://github.com/apache/iotdb/blob/master/example/session/src/main/java/org/apache/iotdb/TableModelSessionExample.java)
-SessionPool 示例代码:[src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java](https://github.com/apache/iotdb/blob/rc/2.0.1/example/session/src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java)
+SessionPool 示例代码:[src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java](https://github.com/apache/iotdb/blob/master/example/session/src/main/java/org/apache/iotdb/TableModelSessionPoolExample.java)
```Java
/*
@@ -662,199 +662,170 @@ import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.pool.TableSessionPoolBuilder;
+import org.apache.tsfile.enums.ColumnCategory;
import org.apache.tsfile.enums.TSDataType;
import org.apache.tsfile.write.record.Tablet;
-import org.apache.tsfile.write.record.Tablet.ColumnCategory;
-import org.apache.tsfile.write.schema.IMeasurementSchema;
-import org.apache.tsfile.write.schema.MeasurementSchema;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
+import static org.apache.iotdb.SessionExample.printDataSet;
+
public class TableModelSessionPoolExample {
- private static final String LOCAL_URL = "127.0.0.1:6667";
-
- public static void main(String[] args) {
-
- // don't specify database in constructor
- ITableSessionPool tableSessionPool =
- new TableSessionPoolBuilder()
- .nodeUrls(Collections.singletonList(LOCAL_URL))
- .user("root")
- .password("root")
- .maxSize(1)
- .build();
-
- try (ITableSession session = tableSessionPool.getSession()) {
-
- session.executeNonQueryStatement("CREATE DATABASE test1");
- session.executeNonQueryStatement("CREATE DATABASE test2");
-
- session.executeNonQueryStatement("use test2");
-
- // or use full qualified table name
- session.executeNonQueryStatement(
- "create table test1.table1("
- + "region_id STRING TAG, "
- + "plant_id STRING TAG, "
- + "device_id STRING TAG, "
- + "model STRING ATTRIBUTE, "
- + "temperature FLOAT FIELD, "
- + "humidity DOUBLE FIELD) with (TTL=3600000)");
-
- session.executeNonQueryStatement(
- "create table table2("
- + "region_id STRING TAG, "
- + "plant_id STRING TAG, "
- + "color STRING ATTRIBUTE, "
- + "temperature FLOAT FIELD, "
- + "speed DOUBLE FIELD) with (TTL=6600000)");
-
- // show tables from current database
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
- }
-
- // show tables by specifying another database
- // using SHOW tables FROM
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES FROM test1")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
- }
-
- // insert table data by tablet
- List<IMeasurementSchema> measurementSchemaList =
- new ArrayList<>(
- Arrays.asList(
- new MeasurementSchema("region_id", TSDataType.STRING),
- new MeasurementSchema("plant_id", TSDataType.STRING),
- new MeasurementSchema("device_id", TSDataType.STRING),
- new MeasurementSchema("model", TSDataType.STRING),
- new MeasurementSchema("temperature", TSDataType.FLOAT),
- new MeasurementSchema("humidity", TSDataType.DOUBLE)));
- List<ColumnCategory> columnTypeList =
- new ArrayList<>(
- Arrays.asList(
- ColumnCategory.TAG,
- ColumnCategory.TAG,
- ColumnCategory.TAG,
- ColumnCategory.ATTRIBUTE,
- ColumnCategory.FIELD,
- ColumnCategory.FIELD));
- Tablet tablet =
- new Tablet(
- "test1",
- IMeasurementSchema.getMeasurementNameList(measurementSchemaList),
- IMeasurementSchema.getDataTypeList(measurementSchemaList),
- 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();
- }
-
- // query table data
- try (SessionDataSet dataSet =
- session.executeQueryStatement(
- "select * from test1 "
- + "where region_id = '1' and plant_id in ('3', '5') and device_id = '3'")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ private static final String LOCAL_URL = "127.0.0.1:6667";
+
+ public static void main(String[] args) {
+
+ // don't specify database in constructor
+ ITableSessionPool tableSessionPool =
+ new TableSessionPoolBuilder()
+ .nodeUrls(Collections.singletonList(LOCAL_URL))
+ .user("root")
+ .password("root")
+ .maxSize(1)
+ .build();
+
+ try (ITableSession session = tableSessionPool.getSession()) {
+
+ session.executeNonQueryStatement("CREATE DATABASE test1");
+ session.executeNonQueryStatement("CREATE DATABASE test2");
+
+ session.executeNonQueryStatement("use test2");
+
+ // or use full qualified table name
+ session.executeNonQueryStatement(
+ "create table test1.table1("
+ + "region_id STRING TAG, "
+ + "plant_id STRING TAG, "
+ + "device_id STRING TAG, "
+ + "model STRING ATTRIBUTE, "
+ + "temperature FLOAT FIELD, "
+ + "humidity DOUBLE FIELD) with (TTL=3600000)");
+
+ session.executeNonQueryStatement(
+ "create table table2("
+ + "region_id STRING TAG, "
+ + "plant_id STRING TAG, "
+ + "color STRING ATTRIBUTE, "
+ + "temperature FLOAT FIELD, "
+ + "speed DOUBLE FIELD) with (TTL=6600000)");
+
+ // show tables from current database
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
+
+ // show tables by specifying another database
+ // using SHOW tables FROM
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES FROM test1")) {
+ printDataSet(dataSet);
+ }
+
+ // insert table data by tablet
+ List<String> columnNameList =
+ Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity");
+ List<TSDataType> dataTypeList =
+ Arrays.asList(
+ TSDataType.STRING,
+ TSDataType.STRING,
+ TSDataType.STRING,
+ TSDataType.STRING,
+ TSDataType.FLOAT,
+ TSDataType.DOUBLE);
+ List<ColumnCategory> columnTypeList =
+ new ArrayList<>(
+ Arrays.asList(
+ ColumnCategory.TAG,
+ ColumnCategory.TAG,
+ ColumnCategory.TAG,
+ ColumnCategory.ATTRIBUTE,
+ ColumnCategory.FIELD,
+ ColumnCategory.FIELD));
+ Tablet tablet = new Tablet("test1", 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();
+ }
+
+ // query table data
+ try (SessionDataSet dataSet =
+ session.executeQueryStatement(
+ "select * from test1 "
+ + "where region_id = '1' and plant_id in ('3', '5') and device_id = '3'")) {
+ printDataSet(dataSet);
+ }
+
+ } catch (IoTDBConnectionException e) {
+ e.printStackTrace();
+ } catch (StatementExecutionException e) {
+ e.printStackTrace();
+ } finally {
+ tableSessionPool.close();
}
- }
-
- } catch (IoTDBConnectionException e) {
- e.printStackTrace();
- } catch (StatementExecutionException e) {
- e.printStackTrace();
- } finally {
- tableSessionPool.close();
- }
- // specify database in constructor
- tableSessionPool =
- new TableSessionPoolBuilder()
- .nodeUrls(Collections.singletonList(LOCAL_URL))
- .user("root")
- .password("root")
- .maxSize(1)
- .database("test1")
- .build();
-
- try (ITableSession session = tableSessionPool.getSession()) {
-
- // show tables from current database
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
- }
-
- // change database to test2
- session.executeNonQueryStatement("use test2");
-
- // show tables by specifying another database
- // using SHOW tables FROM
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ // specify database in constructor
+ tableSessionPool =
+ new TableSessionPoolBuilder()
+ .nodeUrls(Collections.singletonList(LOCAL_URL))
+ .user("root")
+ .password("root")
+ .maxSize(1)
+ .database("test1")
+ .build();
+
+ try (ITableSession session = tableSessionPool.getSession()) {
+
+ // show tables from current database
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
+
+ // change database to test2
+ session.executeNonQueryStatement("use test2");
+
+ // show tables by specifying another database
+ // using SHOW tables FROM
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
+
+ } catch (IoTDBConnectionException e) {
+ e.printStackTrace();
+ } catch (StatementExecutionException e) {
+ e.printStackTrace();
}
- }
- } catch (IoTDBConnectionException e) {
- e.printStackTrace();
- } catch (StatementExecutionException e) {
- e.printStackTrace();
- }
+ try (ITableSession session = tableSessionPool.getSession()) {
- try (ITableSession session = tableSessionPool.getSession()) {
+ // show tables from default database test1
+ try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
+ printDataSet(dataSet);
+ }
- // show tables from default database test1
- try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
- System.out.println(dataSet.getColumnNames());
- System.out.println(dataSet.getColumnTypes());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ } catch (IoTDBConnectionException e) {
+ e.printStackTrace();
+ } catch (StatementExecutionException e) {
+ e.printStackTrace();
+ } finally {
+ tableSessionPool.close();
}
- }
-
- } catch (IoTDBConnectionException e) {
- e.printStackTrace();
- } catch (StatementExecutionException e) {
- e.printStackTrace();
- } finally {
- tableSessionPool.close();
}
- }
}
```
\ No newline at end of file
diff --git a/src/zh/UserGuide/latest/API/Programming-Java-Native-API.md b/src/zh/UserGuide/latest/API/Programming-Java-Native-API.md
index 42bf6c1b..7935c438 100644
--- a/src/zh/UserGuide/latest/API/Programming-Java-Native-API.md
+++ b/src/zh/UserGuide/latest/API/Programming-Java-Native-API.md
@@ -306,33 +306,51 @@ public class SessionPoolExample {
sessionPool.executeNonQueryStatement("set TTL to root.test.** 10000");
// 3. delete time series
sessionPool.executeNonQueryStatement("delete timeseries root.test.d1.s1");
- private static void executeQueryExample() throws IoTDBConnectionException, StatementExecutionException {
+ }
+
+ private static void executeQueryExample() throws IoTDBConnectionException, StatementExecutionException {
// 1. execute normal query
try(SessionDataSetWrapper wrapper = sessionPool.executeQueryStatement("select s1 from root.sg1.d1 limit 10")) {
- while (wrapper.hasNext()) {
- System.out.println(wrapper.next());
- }
+ // get DataIterator like JDBC
+ DataIterator dataIterator = wrapper.iterator();
+ System.out.println(wrapper.getColumnNames());
+ System.out.println(wrapper.getColumnTypes());
+ while (dataIterator.next()) {
+ StringBuilder builder = new StringBuilder();
+ for (String columnName : wrapper.getColumnNames()) {
+ builder.append(dataIterator.getString(columnName) + " ");
}
+ System.out.println(builder);
+ }
+ }
// 2. execute aggregate query
try(SessionDataSetWrapper wrapper = sessionPool.executeQueryStatement("select count(s1) from root.sg1.d1 group by ([0, 40), 5ms) ")) {
- while (wrapper.hasNext()) {
- System.out.println(wrapper.next());
+ // get DataIterator like JDBC
+ DataIterator dataIterator = wrapper.iterator();
+ System.out.println(wrapper.getColumnNames());
+ System.out.println(wrapper.getColumnTypes());
+ while (dataIterator.next()) {
+ StringBuilder builder = new StringBuilder();
+ for (String columnName : wrapper.getColumnNames()) {
+ builder.append(dataIterator.getString(columnName) + " ");
+ }
+ System.out.println(builder);
}
}
- }
+ }
- private static void constructSessionPool() {
- // Using nodeUrls ensures that when one node goes down, other nodes are automatically connected to retry
- List<String> nodeUrls = new ArrayList<>();
- nodeUrls.add("127.0.0.1:6667");
- nodeUrls.add("127.0.0.1:6668");
- sessionPool =
- new SessionPool.Builder()
- .nodeUrls(nodeUrls)
- .user("root")
- .password("root")
- .maxSize(3)
- .build();
+ private static void constructSessionPool() {
+ // Using nodeUrls ensures that when one node goes down, other nodes are automatically connected to retry
+ List<String> nodeUrls = new ArrayList<>();
+ nodeUrls.add("127.0.0.1:6667");
+ nodeUrls.add("127.0.0.1:6668");
+ sessionPool =
+ new SessionPool.Builder()
+ .nodeUrls(nodeUrls)
+ .user("root")
+ .password("root")
+ .maxSize(3)
+ .build();
}
public static void closeSessionPool(){