From 44b4fbeec2ddc065a8a3e221241d700207d06a89 Mon Sep 17 00:00:00 2001 From: Leto_b Date: Fri, 15 Aug 2025 15:29:33 +0800 Subject: [PATCH] update java native api examples --- .../API/Programming-Java-Native-API_apache.md | 327 ++++++++---------- .../Programming-Java-Native-API_timecho.md | 327 ++++++++---------- .../Tree/API/Programming-Java-Native-API.md | 56 ++- .../V1.3.x/API/Programming-Java-Native-API.md | 56 ++- .../API/Programming-Java-Native-API.md | 56 ++- .../API/Programming-Java-Native-API_apache.md | 327 ++++++++---------- .../Programming-Java-Native-API_timecho.md | 327 ++++++++---------- .../latest/API/Programming-Java-Native-API.md | 56 ++- .../API/Programming-Java-Native-API_apache.md | 327 ++++++++---------- .../Programming-Java-Native-API_timecho.md | 327 ++++++++---------- .../Tree/API/Programming-Java-Native-API.md | 56 ++- .../V1.3.x/API/Programming-Java-Native-API.md | 36 +- .../API/Programming-Java-Native-API.md | 58 ++-- .../API/Programming-Java-Native-API_apache.md | 327 ++++++++---------- .../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 80a2c0f95..497e02953 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 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 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 columnNameList = + Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); + List dataTypeList = + Arrays.asList( + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.FLOAT, + TSDataType.DOUBLE); + List columnTypeList = + new ArrayList<>( + Arrays.asList( + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.ATTRIBUTE, + ColumnCategory.FIELD, + ColumnCategory.FIELD)); + Tablet tablet = new Tablet("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 fba9f72ce..945a55e33 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 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 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 columnNameList = + Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); + List dataTypeList = + Arrays.asList( + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.FLOAT, + TSDataType.DOUBLE); + List columnTypeList = + new ArrayList<>( + Arrays.asList( + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.ATTRIBUTE, + ColumnCategory.FIELD, + ColumnCategory.FIELD)); + Tablet tablet = new Tablet("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 159ab303b..347493e0b 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 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 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 0644e6067..7a7b922bf 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 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 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 c9f20a262..66f27bba8 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 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 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 80a2c0f95..497e02953 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 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 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 columnNameList = + Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); + List dataTypeList = + Arrays.asList( + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.FLOAT, + TSDataType.DOUBLE); + List columnTypeList = + new ArrayList<>( + Arrays.asList( + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.ATTRIBUTE, + ColumnCategory.FIELD, + ColumnCategory.FIELD)); + Tablet tablet = new Tablet("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 fba9f72ce..945a55e33 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 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 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 columnNameList = + Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); + List dataTypeList = + Arrays.asList( + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.FLOAT, + TSDataType.DOUBLE); + List columnTypeList = + new ArrayList<>( + Arrays.asList( + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.ATTRIBUTE, + ColumnCategory.FIELD, + ColumnCategory.FIELD)); + Tablet tablet = new Tablet("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 a1f25e8fc..7ef45e59e 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 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 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 ca2005c9f..d2b82af56 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 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 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 columnNameList = + Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); + List dataTypeList = + Arrays.asList( + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.FLOAT, + TSDataType.DOUBLE); + List columnTypeList = + new ArrayList<>( + Arrays.asList( + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.ATTRIBUTE, + ColumnCategory.FIELD, + ColumnCategory.FIELD)); + Tablet tablet = new Tablet("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 ae979081b..49f63cdbd 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 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 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 columnNameList = + Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); + List dataTypeList = + Arrays.asList( + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.FLOAT, + TSDataType.DOUBLE); + List columnTypeList = + new ArrayList<>( + Arrays.asList( + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.ATTRIBUTE, + ColumnCategory.FIELD, + ColumnCategory.FIELD)); + Tablet tablet = new Tablet("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 8a9f27d02..e0e65cf27 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 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 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 628c47ea1..5459c77f5 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 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 668fc0c7d..53a232502 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 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 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 ca2005c9f..d2b82af56 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 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 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 columnNameList = + Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); + List dataTypeList = + Arrays.asList( + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.FLOAT, + TSDataType.DOUBLE); + List columnTypeList = + new ArrayList<>( + Arrays.asList( + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.ATTRIBUTE, + ColumnCategory.FIELD, + ColumnCategory.FIELD)); + Tablet tablet = new Tablet("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 ae979081b..49f63cdbd 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 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 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 columnNameList = + Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); + List dataTypeList = + Arrays.asList( + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.FLOAT, + TSDataType.DOUBLE); + List columnTypeList = + new ArrayList<>( + Arrays.asList( + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.ATTRIBUTE, + ColumnCategory.FIELD, + ColumnCategory.FIELD)); + Tablet tablet = new Tablet("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 42bf6c1b1..7935c4383 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 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 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(){