Skip to content

Commit fd721d0

Browse files
committed
fix java native api difference between zh and en
1 parent 6e5b3cd commit fd721d0

8 files changed

Lines changed: 1048 additions & 80 deletions

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

Lines changed: 259 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@
2020
-->
2121
# Java Native API
2222

23+
## 1. Function Introduction
24+
2325
IoTDB provides a Java native client driver and a session pool management mechanism. These tools enable developers to interact with IoTDB using object-oriented APIs, allowing time-series objects to be directly assembled and inserted into the database without constructing SQL statements. It is recommended to use the `ITableSessionPool` for multi-threaded database operations to maximize efficiency.
2426

25-
## 1. Prerequisites
27+
## 2. Usage Instructions
2628

27-
### 1.1 Environment Requirements
29+
**Environment Requirements**
2830

2931
- **JDK**: Version 1.8 or higher
3032
- **Maven**: Version 3.6 or higher
3133

32-
### 1.2 Adding Maven Dependencies
34+
**Adding Maven Dependencies**
3335

3436
```XML
3537
<dependencies>
@@ -41,13 +43,15 @@ IoTDB provides a Java native client driver and a session pool management mechani
4143
</dependencies>
4244
```
4345

44-
## 2. Read and Write Operations
46+
## 3. Read and Write Operations
47+
48+
### 3.1 ITableSession Interface
4549

46-
### 2.1 ITableSession Interface
50+
#### 3.1.1 Feature Description
4751

4852
The `ITableSession` interface defines basic operations for interacting with IoTDB, including data insertion, query execution, and session closure. Note that this interface is **not thread-safe**.
4953

50-
#### Method Overview
54+
#### 3.1.2 Method Overview
5155

5256
| **Method Name** | **Description** | **Parameters** | **Return Value** | **Exceptions** |
5357
| --------------------------------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | ---------------- | --------------------------------------------------------- |
@@ -57,7 +61,7 @@ The `ITableSession` interface defines basic operations for interacting with IoTD
5761
| executeQueryStatement(String sql, long timeoutInMs) | Executes a query SQL statement with a specified timeout in milliseconds. | `sql`: The SQL query statement. `timeoutInMs`: Query timeout in milliseconds. | `SessionDataSet` | `StatementExecutionException` |
5862
| close() | Closes the session and releases resources. | None | None | IoTDBConnectionException |
5963

60-
#### Sample Code
64+
#### 3.1.3 Sample Code
6165

6266
```Java
6367
/**
@@ -125,11 +129,13 @@ public interface ITableSession extends AutoCloseable {
125129
}
126130
```
127131

128-
### 2.2 TableSessionBuilder Class
132+
### 3.2 TableSessionBuilder Class
133+
134+
#### 3.2.1 Feature Description
129135

130136
The `TableSessionBuilder` class is a builder for configuring and creating instances of the `ITableSession` interface. It allows developers to set connection parameters, query parameters, and security features.
131137

132-
#### Parameter Configuration
138+
#### 3.2.2 Parameter Configuration
133139

134140
| **Parameter** | **Description** | **Default Value** |
135141
|-----------------------------------------------------| ------------------------------------------------------------ | ------------------------------------------------- |
@@ -152,7 +158,7 @@ The `TableSessionBuilder` class is a builder for configuring and creating instan
152158
| enableCompression(boolean enableCompression) | Enables or disables RPC compression for the connection. | `false` |
153159
| connectionTimeoutInMs(int connectionTimeoutInMs) | Sets the connection timeout in milliseconds. | `0` (no timeout) |
154160

155-
#### Sample Code
161+
#### 3.2.3 Sample Code
156162

157163
```Java
158164
/**
@@ -337,20 +343,22 @@ public class TableSessionBuilder {
337343
}
338344
```
339345

340-
## 3. Session Pool
346+
## 4. Session Pool
347+
348+
### 4.1 ITableSessionPool Interface
341349

342-
### 3.1 ITableSessionPool Interface
350+
#### 4.1.1 Feature Description
343351

344352
The `ITableSessionPool` interface manages a pool of `ITableSession` instances, enabling efficient reuse of connections and proper cleanup of resources.
345353

346-
#### Method Overview
354+
#### 4.1.2 Method Overview
347355

348356
| **Method Name** | **Description** | **Return Value** | **Exceptions** |
349357
| --------------- | ---------------------------------------------------------- | ---------------- | -------------------------- |
350358
| getSession() | Acquires a session from the pool for database interaction. | `ITableSession` | `IoTDBConnectionException` |
351359
| close() | Closes the session pool and releases resources.。 | None | None |
352360

353-
#### Sample Code
361+
#### 4.1.3 Sample Code
354362

355363
```Java
356364
/**
@@ -379,11 +387,13 @@ public interface ITableSessionPool {
379387
}
380388
```
381389

382-
### 3.2 TableSessionPoolBuilder Class
390+
### 4.2 TableSessionPoolBuilder Class
391+
392+
#### 4.2.1 Feature Description
383393

384394
The `TableSessionPoolBuilder` class is a builder for configuring and creating `ITableSessionPool` instances, supporting options like connection settings and pooling behavior.
385395

386-
#### Parameter Configuration
396+
#### 4.2.2 Parameter Configuration
387397

388398
| **Parameter** | **Description** | **Default Value** |
389399
|---------------------------------------------------------------| ------------------------------------------------------------ | --------------------------------------------- |
@@ -408,7 +418,7 @@ The `TableSessionPoolBuilder` class is a builder for configuring and creating `I
408418
| trustStore(String keyStore) | Sets the path to the trust store for SSL connections. | `null` |
409419
| trustStorePwd(String keyStorePwd) | Sets the password for the SSL trust store. | `null` |
410420

411-
#### Sample Code
421+
#### 4.2.3 Sample Code
412422

413423
```Java
414424
/**
@@ -608,4 +618,236 @@ public class TableSessionPoolBuilder {
608618
*/
609619
public TableSessionPoolBuilder trustStorePwd(String keyStorePwd);
610620
}
621+
```
622+
623+
## 5. Example Code
624+
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)
626+
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)
628+
629+
```Java
630+
/*
631+
* Licensed to the Apache Software Foundation (ASF) under one
632+
* or more contributor license agreements. See the NOTICE file
633+
* distributed with this work for additional information
634+
* regarding copyright ownership. The ASF licenses this file
635+
* to you under the Apache License, Version 2.0 (the
636+
* "License"); you may not use this file except in compliance
637+
* with the License. You may obtain a copy of the License at
638+
*
639+
* http://www.apache.org/licenses/LICENSE-2.0
640+
*
641+
* Unless required by applicable law or agreed to in writing,
642+
* software distributed under the License is distributed on an
643+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
644+
* KIND, either express or implied. See the License for the
645+
* specific language governing permissions and limitations
646+
* under the License.
647+
*/
648+
649+
package org.apache.iotdb;
650+
651+
import org.apache.iotdb.isession.ITableSession;
652+
import org.apache.iotdb.isession.SessionDataSet;
653+
import org.apache.iotdb.isession.pool.ITableSessionPool;
654+
import org.apache.iotdb.rpc.IoTDBConnectionException;
655+
import org.apache.iotdb.rpc.StatementExecutionException;
656+
import org.apache.iotdb.session.pool.TableSessionPoolBuilder;
657+
658+
import org.apache.tsfile.enums.TSDataType;
659+
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;
663+
664+
import java.util.ArrayList;
665+
import java.util.Arrays;
666+
import java.util.Collections;
667+
import java.util.List;
668+
669+
public class TableModelSessionPoolExample {
670+
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());
782+
}
783+
}
784+
785+
} catch (IoTDBConnectionException e) {
786+
e.printStackTrace();
787+
} catch (StatementExecutionException e) {
788+
e.printStackTrace();
789+
} finally {
790+
tableSessionPool.close();
791+
}
792+
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());
824+
}
825+
}
826+
827+
} catch (IoTDBConnectionException e) {
828+
e.printStackTrace();
829+
} catch (StatementExecutionException e) {
830+
e.printStackTrace();
831+
}
832+
833+
try (ITableSession session = tableSessionPool.getSession()) {
834+
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());
841+
}
842+
}
843+
844+
} catch (IoTDBConnectionException e) {
845+
e.printStackTrace();
846+
} catch (StatementExecutionException e) {
847+
e.printStackTrace();
848+
} finally {
849+
tableSessionPool.close();
850+
}
851+
}
852+
}
611853
```

0 commit comments

Comments
 (0)