Programming - Native API

Dependencies

  • JDK >= 1.8
  • Maven >= 3.1

How to install in local maven repository

In root directory:

mvn clean install -pl session -am -DskipTests

Using IoTDB Native API with Maven

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.apache.iotdb</groupId>
  4. <artifactId>iotdb-session</artifactId>
  5. <version>0.10.0</version>
  6. </dependency>
  7. </dependencies>

Native APIs

Here we show the commonly used interfaces and their parameters in the Native API:

  • Initialize a Session

    1. Session(String host, int port)
    2. Session(String host, String port, String username, String password)
    3. Session(String host, int port, String username, String password)
  • Open a Session

    1. Session.open()
  • Close a Session

    1. Session.close()
  • Set storage group

    1. void setStorageGroup(String storageGroupId)

  • Delete one or several storage groups

    1. void deleteStorageGroup(String storageGroup)
    2. void deleteStorageGroups(List<String> storageGroups)
  • Create one or multiple timeseries

    1. void createTimeseries(String path, TSDataType dataType,
    2. TSEncoding encoding, CompressionType compressor, Map<String, String> props,
    3. Map<String, String> tags, Map<String, String> attributes, String measurementAlias)
    4. void createMultiTimeseries(List<String> paths, List<TSDataType> dataTypes,
    5. List<TSEncoding> encodings, List<CompressionType> compressors,
    6. List<Map<String, String>> propsList, List<Map<String, String>> tagsList,
    7. List<Map<String, String>> attributesList, List<String> measurementAliasList)
  • Delete one or several timeseries

    1. void deleteTimeseries(String path)
    2. void deleteTimeseries(List<String> paths)
  • Delete data before or equal to a timestamp of one or several timeseries

    1. void deleteData(String path, long time)
    2. void deleteData(List<String> paths, long time)
  • Insert a Record,which contains multiple measurement value of a device at a timestamp. Without type info the server has to do type inference, which may cost some time

    1. void insertRecord(String deviceId, long time, List<String> measurements, List<String> values)
  • Insert a Tablet,which is multiple rows of a device, each row has the same measurements

    1. void insertTablet(Tablet tablet)
  • Insert multiple Tablets

    1. void insertTablets(Map<String, Tablet> tablet)
  • Insert multiple Records. Without type info the server has to do type inference, which may cost some time

    1. void insertRecords(List<String> deviceIds, List<Long> times,
    2. List<List<String>> measurementsList, List<List<String>> valuesList)
  • Insert a Record,which contains multiple measurement value of a device at a timestamp. With type info the server has no need to do type inference, which leads a better performance

    1. void insertRecord(String deviceId, long time, List<String> measurements,
    2. List<TSDataType> types, List<Object> values)
  • Insert multiple Records. With type info the server has no need to do type inference, which leads a better performance

    1. void insertRecords(List<String> deviceIds, List<Long> times,
    2. List<List<String>> measurementsList, List<List<TSDataType>> typesList,
    3. List<List<Object>> valuesList)

Native APIs for profiling network cost

  • Test the network and client cost of insertRecords. This method NOT insert data into database and server just return after accept the request, this method should be used to test other time cost in client

    1. void testInsertRecords(List<String> deviceIds, List<Long> times,
    2. List<List<String>> measurementsList, List<List<String>> valuesList)

    or

    1. void testInsertRecords(List<String> deviceIds, List<Long> times,
    2. List<List<String>> measurementsList, List<List<TSDataType>> typesList,
    3. List<List<Object>> valuesList)
  • Test the network and client cost of insertRecord. This method NOT insert data into database and server just return after accept the request, this method should be used to test other time cost in client

    1. void testInsertRecord(String deviceId, long time, List<String> measurements, List<String> values)

    or

    1. void testInsertRecord(String deviceId, long time, List<String> measurements,
    2. List<TSDataType> types, List<Object> values)
  • Test the network and client cost of insertTablet. This method NOT insert data into database and server just return after accept the request, this method should be used to test other time cost in client

    1. void testInsertTablet(Tablet tablet)

Sample code

To get more information of the following interfaces, please view session/src/main/java/org/apache/iotdb/session/Session.java

The sample code of using these interfaces is in example/session/src/main/java/org/apache/iotdb/SessionExample.java,which provides an example of how to open an IoTDB session, execute a batch insertion.

Session Pool for Native API

We provided a connection pool (`SessionPool) for Native API. Using the interface, you need to define the pool size.

If you can not get a session connection in 60 secondes, there is a warning log but the program will hang.

If a session has finished an operation, it will be put back to the pool automatically. If a session connection is broken, the session will be removed automatically and the pool will try to create a new session and redo the operation.

For query operations:

  1. When using SessionPool to query data, the result set is SessionDataSetWrapper;
  2. Given a SessionDataSetWrapper, if you have not scanned all the data in it and stop to use it, you have to call SessionPool.closeResultSet(wrapper) manually;
  3. When you call hasNext() and next() of a SessionDataSetWrapper and there is an exception, then you have to call SessionPool.closeResultSet(wrapper) manually;
  4. You can call getColumnNames() of SessionDataSetWrapper to get the column names of query result;

Examples: session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java

Or example/session/src/main/java/org/apache/iotdb/SessionPoolExample.java