Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
327 changes: 149 additions & 178 deletions src/UserGuide/Master/Table/API/Programming-Java-Native-API_apache.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
/*
Expand Down Expand Up @@ -655,199 +655,170 @@ import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.pool.TableSessionPoolBuilder;

import org.apache.tsfile.enums.ColumnCategory;
import org.apache.tsfile.enums.TSDataType;
import org.apache.tsfile.write.record.Tablet;
import org.apache.tsfile.write.record.Tablet.ColumnCategory;
import org.apache.tsfile.write.schema.IMeasurementSchema;
import org.apache.tsfile.write.schema.MeasurementSchema;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.apache.iotdb.SessionExample.printDataSet;

public class TableModelSessionPoolExample {

private static final String LOCAL_URL = "127.0.0.1:6667";

public static void main(String[] args) {

// don't specify database in constructor
ITableSessionPool tableSessionPool =
new TableSessionPoolBuilder()
.nodeUrls(Collections.singletonList(LOCAL_URL))
.user("root")
.password("root")
.maxSize(1)
.build();

try (ITableSession session = tableSessionPool.getSession()) {

session.executeNonQueryStatement("CREATE DATABASE test1");
session.executeNonQueryStatement("CREATE DATABASE test2");

session.executeNonQueryStatement("use test2");

// or use full qualified table name
session.executeNonQueryStatement(
"create table test1.table1("
+ "region_id STRING TAG, "
+ "plant_id STRING TAG, "
+ "device_id STRING TAG, "
+ "model STRING ATTRIBUTE, "
+ "temperature FLOAT FIELD, "
+ "humidity DOUBLE FIELD) with (TTL=3600000)");

session.executeNonQueryStatement(
"create table table2("
+ "region_id STRING TAG, "
+ "plant_id STRING TAG, "
+ "color STRING ATTRIBUTE, "
+ "temperature FLOAT FIELD, "
+ "speed DOUBLE FIELD) with (TTL=6600000)");

// show tables from current database
try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
System.out.println(dataSet.getColumnNames());
System.out.println(dataSet.getColumnTypes());
while (dataSet.hasNext()) {
System.out.println(dataSet.next());
}
}

// show tables by specifying another database
// using SHOW tables FROM
try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES FROM test1")) {
System.out.println(dataSet.getColumnNames());
System.out.println(dataSet.getColumnTypes());
while (dataSet.hasNext()) {
System.out.println(dataSet.next());
}
}

// insert table data by tablet
List<IMeasurementSchema> measurementSchemaList =
new ArrayList<>(
Arrays.asList(
new MeasurementSchema("region_id", TSDataType.STRING),
new MeasurementSchema("plant_id", TSDataType.STRING),
new MeasurementSchema("device_id", TSDataType.STRING),
new MeasurementSchema("model", TSDataType.STRING),
new MeasurementSchema("temperature", TSDataType.FLOAT),
new MeasurementSchema("humidity", TSDataType.DOUBLE)));
List<ColumnCategory> columnTypeList =
new ArrayList<>(
Arrays.asList(
ColumnCategory.TAG,
ColumnCategory.TAG,
ColumnCategory.TAG,
ColumnCategory.ATTRIBUTE,
ColumnCategory.FIELD,
ColumnCategory.FIELD));
Tablet tablet =
new Tablet(
"test1",
IMeasurementSchema.getMeasurementNameList(measurementSchemaList),
IMeasurementSchema.getDataTypeList(measurementSchemaList),
columnTypeList,
100);
for (long timestamp = 0; timestamp < 100; timestamp++) {
int rowIndex = tablet.getRowSize();
tablet.addTimestamp(rowIndex, timestamp);
tablet.addValue("region_id", rowIndex, "1");
tablet.addValue("plant_id", rowIndex, "5");
tablet.addValue("device_id", rowIndex, "3");
tablet.addValue("model", rowIndex, "A");
tablet.addValue("temperature", rowIndex, 37.6F);
tablet.addValue("humidity", rowIndex, 111.1);
if (tablet.getRowSize() == tablet.getMaxRowNumber()) {
session.insert(tablet);
tablet.reset();
}
}
if (tablet.getRowSize() != 0) {
session.insert(tablet);
tablet.reset();
}

// query table data
try (SessionDataSet dataSet =
session.executeQueryStatement(
"select * from test1 "
+ "where region_id = '1' and plant_id in ('3', '5') and device_id = '3'")) {
System.out.println(dataSet.getColumnNames());
System.out.println(dataSet.getColumnTypes());
while (dataSet.hasNext()) {
System.out.println(dataSet.next());
private static final String LOCAL_URL = "127.0.0.1:6667";

public static void main(String[] args) {

// don't specify database in constructor
ITableSessionPool tableSessionPool =
new TableSessionPoolBuilder()
.nodeUrls(Collections.singletonList(LOCAL_URL))
.user("root")
.password("root")
.maxSize(1)
.build();

try (ITableSession session = tableSessionPool.getSession()) {

session.executeNonQueryStatement("CREATE DATABASE test1");
session.executeNonQueryStatement("CREATE DATABASE test2");

session.executeNonQueryStatement("use test2");

// or use full qualified table name
session.executeNonQueryStatement(
"create table test1.table1("
+ "region_id STRING TAG, "
+ "plant_id STRING TAG, "
+ "device_id STRING TAG, "
+ "model STRING ATTRIBUTE, "
+ "temperature FLOAT FIELD, "
+ "humidity DOUBLE FIELD) with (TTL=3600000)");

session.executeNonQueryStatement(
"create table table2("
+ "region_id STRING TAG, "
+ "plant_id STRING TAG, "
+ "color STRING ATTRIBUTE, "
+ "temperature FLOAT FIELD, "
+ "speed DOUBLE FIELD) with (TTL=6600000)");

// show tables from current database
try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
printDataSet(dataSet);
}

// show tables by specifying another database
// using SHOW tables FROM
try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES FROM test1")) {
printDataSet(dataSet);
}

// insert table data by tablet
List<String> columnNameList =
Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity");
List<TSDataType> dataTypeList =
Arrays.asList(
TSDataType.STRING,
TSDataType.STRING,
TSDataType.STRING,
TSDataType.STRING,
TSDataType.FLOAT,
TSDataType.DOUBLE);
List<ColumnCategory> columnTypeList =
new ArrayList<>(
Arrays.asList(
ColumnCategory.TAG,
ColumnCategory.TAG,
ColumnCategory.TAG,
ColumnCategory.ATTRIBUTE,
ColumnCategory.FIELD,
ColumnCategory.FIELD));
Tablet tablet = new Tablet("test1", columnNameList, dataTypeList, columnTypeList, 100);
for (long timestamp = 0; timestamp < 100; timestamp++) {
int rowIndex = tablet.getRowSize();
tablet.addTimestamp(rowIndex, timestamp);
tablet.addValue("region_id", rowIndex, "1");
tablet.addValue("plant_id", rowIndex, "5");
tablet.addValue("device_id", rowIndex, "3");
tablet.addValue("model", rowIndex, "A");
tablet.addValue("temperature", rowIndex, 37.6F);
tablet.addValue("humidity", rowIndex, 111.1);
if (tablet.getRowSize() == tablet.getMaxRowNumber()) {
session.insert(tablet);
tablet.reset();
}
}
if (tablet.getRowSize() != 0) {
session.insert(tablet);
tablet.reset();
}

// query table data
try (SessionDataSet dataSet =
session.executeQueryStatement(
"select * from test1 "
+ "where region_id = '1' and plant_id in ('3', '5') and device_id = '3'")) {
printDataSet(dataSet);
}

} catch (IoTDBConnectionException e) {
e.printStackTrace();
} catch (StatementExecutionException e) {
e.printStackTrace();
} finally {
tableSessionPool.close();
}
}

} catch (IoTDBConnectionException e) {
e.printStackTrace();
} catch (StatementExecutionException e) {
e.printStackTrace();
} finally {
tableSessionPool.close();
}

// specify database in constructor
tableSessionPool =
new TableSessionPoolBuilder()
.nodeUrls(Collections.singletonList(LOCAL_URL))
.user("root")
.password("root")
.maxSize(1)
.database("test1")
.build();

try (ITableSession session = tableSessionPool.getSession()) {

// show tables from current database
try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
System.out.println(dataSet.getColumnNames());
System.out.println(dataSet.getColumnTypes());
while (dataSet.hasNext()) {
System.out.println(dataSet.next());
}
}

// change database to test2
session.executeNonQueryStatement("use test2");

// show tables by specifying another database
// using SHOW tables FROM
try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
System.out.println(dataSet.getColumnNames());
System.out.println(dataSet.getColumnTypes());
while (dataSet.hasNext()) {
System.out.println(dataSet.next());
// specify database in constructor
tableSessionPool =
new TableSessionPoolBuilder()
.nodeUrls(Collections.singletonList(LOCAL_URL))
.user("root")
.password("root")
.maxSize(1)
.database("test1")
.build();

try (ITableSession session = tableSessionPool.getSession()) {

// show tables from current database
try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
printDataSet(dataSet);
}

// change database to test2
session.executeNonQueryStatement("use test2");

// show tables by specifying another database
// using SHOW tables FROM
try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
printDataSet(dataSet);
}

} catch (IoTDBConnectionException e) {
e.printStackTrace();
} catch (StatementExecutionException e) {
e.printStackTrace();
}
}

} catch (IoTDBConnectionException e) {
e.printStackTrace();
} catch (StatementExecutionException e) {
e.printStackTrace();
}
try (ITableSession session = tableSessionPool.getSession()) {

try (ITableSession session = tableSessionPool.getSession()) {
// show tables from default database test1
try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
printDataSet(dataSet);
}

// show tables from default database test1
try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
System.out.println(dataSet.getColumnNames());
System.out.println(dataSet.getColumnTypes());
while (dataSet.hasNext()) {
System.out.println(dataSet.next());
} catch (IoTDBConnectionException e) {
e.printStackTrace();
} catch (StatementExecutionException e) {
e.printStackTrace();
} finally {
tableSessionPool.close();
}
}

} catch (IoTDBConnectionException e) {
e.printStackTrace();
} catch (StatementExecutionException e) {
e.printStackTrace();
} finally {
tableSessionPool.close();
}
}
}
```
Loading