Skip to content

Commit 75705cc

Browse files
authored
update java native api examples (#851)
1 parent 94f9507 commit 75705cc

16 files changed

+1479
-1567
lines changed

src/UserGuide/Master/Table/API/Programming-Java-Native-API_apache.md

Lines changed: 149 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -622,9 +622,9 @@ public class TableSessionPoolBuilder {
622622

623623
## 5. Example Code
624624

625-
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)
625+
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)
626626

627-
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)
627+
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)
628628

629629
```Java
630630
/*
@@ -655,199 +655,170 @@ import org.apache.iotdb.rpc.IoTDBConnectionException;
655655
import org.apache.iotdb.rpc.StatementExecutionException;
656656
import org.apache.iotdb.session.pool.TableSessionPoolBuilder;
657657

658+
import org.apache.tsfile.enums.ColumnCategory;
658659
import org.apache.tsfile.enums.TSDataType;
659660
import org.apache.tsfile.write.record.Tablet;
660-
import org.apache.tsfile.write.record.Tablet.ColumnCategory;
661-
import org.apache.tsfile.write.schema.IMeasurementSchema;
662-
import org.apache.tsfile.write.schema.MeasurementSchema;
663661

664662
import java.util.ArrayList;
665663
import java.util.Arrays;
666664
import java.util.Collections;
667665
import java.util.List;
668666

667+
import static org.apache.iotdb.SessionExample.printDataSet;
668+
669669
public class TableModelSessionPoolExample {
670670

671-
private static final String LOCAL_URL = "127.0.0.1:6667";
672-
673-
public static void main(String[] args) {
674-
675-
// don't specify database in constructor
676-
ITableSessionPool tableSessionPool =
677-
new TableSessionPoolBuilder()
678-
.nodeUrls(Collections.singletonList(LOCAL_URL))
679-
.user("root")
680-
.password("root")
681-
.maxSize(1)
682-
.build();
683-
684-
try (ITableSession session = tableSessionPool.getSession()) {
685-
686-
session.executeNonQueryStatement("CREATE DATABASE test1");
687-
session.executeNonQueryStatement("CREATE DATABASE test2");
688-
689-
session.executeNonQueryStatement("use test2");
690-
691-
// or use full qualified table name
692-
session.executeNonQueryStatement(
693-
"create table test1.table1("
694-
+ "region_id STRING TAG, "
695-
+ "plant_id STRING TAG, "
696-
+ "device_id STRING TAG, "
697-
+ "model STRING ATTRIBUTE, "
698-
+ "temperature FLOAT FIELD, "
699-
+ "humidity DOUBLE FIELD) with (TTL=3600000)");
700-
701-
session.executeNonQueryStatement(
702-
"create table table2("
703-
+ "region_id STRING TAG, "
704-
+ "plant_id STRING TAG, "
705-
+ "color STRING ATTRIBUTE, "
706-
+ "temperature FLOAT FIELD, "
707-
+ "speed DOUBLE FIELD) with (TTL=6600000)");
708-
709-
// show tables from current database
710-
try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
711-
System.out.println(dataSet.getColumnNames());
712-
System.out.println(dataSet.getColumnTypes());
713-
while (dataSet.hasNext()) {
714-
System.out.println(dataSet.next());
715-
}
716-
}
717-
718-
// show tables by specifying another database
719-
// using SHOW tables FROM
720-
try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES FROM test1")) {
721-
System.out.println(dataSet.getColumnNames());
722-
System.out.println(dataSet.getColumnTypes());
723-
while (dataSet.hasNext()) {
724-
System.out.println(dataSet.next());
725-
}
726-
}
727-
728-
// insert table data by tablet
729-
List<IMeasurementSchema> measurementSchemaList =
730-
new ArrayList<>(
731-
Arrays.asList(
732-
new MeasurementSchema("region_id", TSDataType.STRING),
733-
new MeasurementSchema("plant_id", TSDataType.STRING),
734-
new MeasurementSchema("device_id", TSDataType.STRING),
735-
new MeasurementSchema("model", TSDataType.STRING),
736-
new MeasurementSchema("temperature", TSDataType.FLOAT),
737-
new MeasurementSchema("humidity", TSDataType.DOUBLE)));
738-
List<ColumnCategory> columnTypeList =
739-
new ArrayList<>(
740-
Arrays.asList(
741-
ColumnCategory.TAG,
742-
ColumnCategory.TAG,
743-
ColumnCategory.TAG,
744-
ColumnCategory.ATTRIBUTE,
745-
ColumnCategory.FIELD,
746-
ColumnCategory.FIELD));
747-
Tablet tablet =
748-
new Tablet(
749-
"test1",
750-
IMeasurementSchema.getMeasurementNameList(measurementSchemaList),
751-
IMeasurementSchema.getDataTypeList(measurementSchemaList),
752-
columnTypeList,
753-
100);
754-
for (long timestamp = 0; timestamp < 100; timestamp++) {
755-
int rowIndex = tablet.getRowSize();
756-
tablet.addTimestamp(rowIndex, timestamp);
757-
tablet.addValue("region_id", rowIndex, "1");
758-
tablet.addValue("plant_id", rowIndex, "5");
759-
tablet.addValue("device_id", rowIndex, "3");
760-
tablet.addValue("model", rowIndex, "A");
761-
tablet.addValue("temperature", rowIndex, 37.6F);
762-
tablet.addValue("humidity", rowIndex, 111.1);
763-
if (tablet.getRowSize() == tablet.getMaxRowNumber()) {
764-
session.insert(tablet);
765-
tablet.reset();
766-
}
767-
}
768-
if (tablet.getRowSize() != 0) {
769-
session.insert(tablet);
770-
tablet.reset();
771-
}
772-
773-
// query table data
774-
try (SessionDataSet dataSet =
775-
session.executeQueryStatement(
776-
"select * from test1 "
777-
+ "where region_id = '1' and plant_id in ('3', '5') and device_id = '3'")) {
778-
System.out.println(dataSet.getColumnNames());
779-
System.out.println(dataSet.getColumnTypes());
780-
while (dataSet.hasNext()) {
781-
System.out.println(dataSet.next());
671+
private static final String LOCAL_URL = "127.0.0.1:6667";
672+
673+
public static void main(String[] args) {
674+
675+
// don't specify database in constructor
676+
ITableSessionPool tableSessionPool =
677+
new TableSessionPoolBuilder()
678+
.nodeUrls(Collections.singletonList(LOCAL_URL))
679+
.user("root")
680+
.password("root")
681+
.maxSize(1)
682+
.build();
683+
684+
try (ITableSession session = tableSessionPool.getSession()) {
685+
686+
session.executeNonQueryStatement("CREATE DATABASE test1");
687+
session.executeNonQueryStatement("CREATE DATABASE test2");
688+
689+
session.executeNonQueryStatement("use test2");
690+
691+
// or use full qualified table name
692+
session.executeNonQueryStatement(
693+
"create table test1.table1("
694+
+ "region_id STRING TAG, "
695+
+ "plant_id STRING TAG, "
696+
+ "device_id STRING TAG, "
697+
+ "model STRING ATTRIBUTE, "
698+
+ "temperature FLOAT FIELD, "
699+
+ "humidity DOUBLE FIELD) with (TTL=3600000)");
700+
701+
session.executeNonQueryStatement(
702+
"create table table2("
703+
+ "region_id STRING TAG, "
704+
+ "plant_id STRING TAG, "
705+
+ "color STRING ATTRIBUTE, "
706+
+ "temperature FLOAT FIELD, "
707+
+ "speed DOUBLE FIELD) with (TTL=6600000)");
708+
709+
// show tables from current database
710+
try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
711+
printDataSet(dataSet);
712+
}
713+
714+
// show tables by specifying another database
715+
// using SHOW tables FROM
716+
try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES FROM test1")) {
717+
printDataSet(dataSet);
718+
}
719+
720+
// insert table data by tablet
721+
List<String> columnNameList =
722+
Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity");
723+
List<TSDataType> dataTypeList =
724+
Arrays.asList(
725+
TSDataType.STRING,
726+
TSDataType.STRING,
727+
TSDataType.STRING,
728+
TSDataType.STRING,
729+
TSDataType.FLOAT,
730+
TSDataType.DOUBLE);
731+
List<ColumnCategory> columnTypeList =
732+
new ArrayList<>(
733+
Arrays.asList(
734+
ColumnCategory.TAG,
735+
ColumnCategory.TAG,
736+
ColumnCategory.TAG,
737+
ColumnCategory.ATTRIBUTE,
738+
ColumnCategory.FIELD,
739+
ColumnCategory.FIELD));
740+
Tablet tablet = new Tablet("test1", columnNameList, dataTypeList, columnTypeList, 100);
741+
for (long timestamp = 0; timestamp < 100; timestamp++) {
742+
int rowIndex = tablet.getRowSize();
743+
tablet.addTimestamp(rowIndex, timestamp);
744+
tablet.addValue("region_id", rowIndex, "1");
745+
tablet.addValue("plant_id", rowIndex, "5");
746+
tablet.addValue("device_id", rowIndex, "3");
747+
tablet.addValue("model", rowIndex, "A");
748+
tablet.addValue("temperature", rowIndex, 37.6F);
749+
tablet.addValue("humidity", rowIndex, 111.1);
750+
if (tablet.getRowSize() == tablet.getMaxRowNumber()) {
751+
session.insert(tablet);
752+
tablet.reset();
753+
}
754+
}
755+
if (tablet.getRowSize() != 0) {
756+
session.insert(tablet);
757+
tablet.reset();
758+
}
759+
760+
// query table data
761+
try (SessionDataSet dataSet =
762+
session.executeQueryStatement(
763+
"select * from test1 "
764+
+ "where region_id = '1' and plant_id in ('3', '5') and device_id = '3'")) {
765+
printDataSet(dataSet);
766+
}
767+
768+
} catch (IoTDBConnectionException e) {
769+
e.printStackTrace();
770+
} catch (StatementExecutionException e) {
771+
e.printStackTrace();
772+
} finally {
773+
tableSessionPool.close();
782774
}
783-
}
784-
785-
} catch (IoTDBConnectionException e) {
786-
e.printStackTrace();
787-
} catch (StatementExecutionException e) {
788-
e.printStackTrace();
789-
} finally {
790-
tableSessionPool.close();
791-
}
792775

793-
// specify database in constructor
794-
tableSessionPool =
795-
new TableSessionPoolBuilder()
796-
.nodeUrls(Collections.singletonList(LOCAL_URL))
797-
.user("root")
798-
.password("root")
799-
.maxSize(1)
800-
.database("test1")
801-
.build();
802-
803-
try (ITableSession session = tableSessionPool.getSession()) {
804-
805-
// show tables from current database
806-
try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
807-
System.out.println(dataSet.getColumnNames());
808-
System.out.println(dataSet.getColumnTypes());
809-
while (dataSet.hasNext()) {
810-
System.out.println(dataSet.next());
811-
}
812-
}
813-
814-
// change database to test2
815-
session.executeNonQueryStatement("use test2");
816-
817-
// show tables by specifying another database
818-
// using SHOW tables FROM
819-
try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
820-
System.out.println(dataSet.getColumnNames());
821-
System.out.println(dataSet.getColumnTypes());
822-
while (dataSet.hasNext()) {
823-
System.out.println(dataSet.next());
776+
// specify database in constructor
777+
tableSessionPool =
778+
new TableSessionPoolBuilder()
779+
.nodeUrls(Collections.singletonList(LOCAL_URL))
780+
.user("root")
781+
.password("root")
782+
.maxSize(1)
783+
.database("test1")
784+
.build();
785+
786+
try (ITableSession session = tableSessionPool.getSession()) {
787+
788+
// show tables from current database
789+
try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
790+
printDataSet(dataSet);
791+
}
792+
793+
// change database to test2
794+
session.executeNonQueryStatement("use test2");
795+
796+
// show tables by specifying another database
797+
// using SHOW tables FROM
798+
try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
799+
printDataSet(dataSet);
800+
}
801+
802+
} catch (IoTDBConnectionException e) {
803+
e.printStackTrace();
804+
} catch (StatementExecutionException e) {
805+
e.printStackTrace();
824806
}
825-
}
826807

827-
} catch (IoTDBConnectionException e) {
828-
e.printStackTrace();
829-
} catch (StatementExecutionException e) {
830-
e.printStackTrace();
831-
}
808+
try (ITableSession session = tableSessionPool.getSession()) {
832809

833-
try (ITableSession session = tableSessionPool.getSession()) {
810+
// show tables from default database test1
811+
try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
812+
printDataSet(dataSet);
813+
}
834814

835-
// show tables from default database test1
836-
try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES")) {
837-
System.out.println(dataSet.getColumnNames());
838-
System.out.println(dataSet.getColumnTypes());
839-
while (dataSet.hasNext()) {
840-
System.out.println(dataSet.next());
815+
} catch (IoTDBConnectionException e) {
816+
e.printStackTrace();
817+
} catch (StatementExecutionException e) {
818+
e.printStackTrace();
819+
} finally {
820+
tableSessionPool.close();
841821
}
842-
}
843-
844-
} catch (IoTDBConnectionException e) {
845-
e.printStackTrace();
846-
} catch (StatementExecutionException e) {
847-
e.printStackTrace();
848-
} finally {
849-
tableSessionPool.close();
850822
}
851-
}
852823
}
853824
```

0 commit comments

Comments
 (0)