Updated on 2024-04-02 GMT+08:00

Java Example Code

Description

Run the IoTDB SQL statement in Session connection mode.

Sample Code

The following code snippet is used as an example. For complete code, see com.huawei.bigdata.SessionExample.

Change HOST_1, HOST_2, and HOST_3 to the service IP addresses of the nodes accommodating IoTDBServer. Set the username and password in the Session object.

  • On Manager, choose Cluster > Services > IoTDB > Instance to view the service IP addresses of the nodes where the IoTDBServer to be connected is located.
  • To obtain the RPC port number, log in to FusionInsight Manager, choose Cluster > Services > IoTDB, click Configuration, and click All Configurations, and search for IOTDB_SERVER_RPC_PORT.
  • In security mode, the username and password for logging in to the node where IoTDBServer resides are controlled by FusionInsight Manager. Ensure that the user has the permission to operate the IoTDB service. For details, see Preparing for User Authentication.
  • You need to set the username and password for authentication in the local environment variables. You are advised to store the username and password in ciphertext and decrypt them upon using.
    • Authentication username is the username for accessing IoTDB.
    • Password is the password for accessing IoTDB.
   /**
    * In security mode, the default value of SSL_ENABLE is true. You need to import the truststore.jks file.
    * In security mode, you can also log in to FusionInsight Manager, choose Cluster > Services > IoTDB > Configuration, search for SSL in the search box, and change the value of SSL_ENABLE to false. After saving the configuration, restart the IoTDB service for the configuration to take effect. Modify the following configuration in the iotdb-client.env file in the Client installation directory/IoTDB/iotdb/conf directory on the client: iotdb_ssl_enable="false"
    */
    private static final String IOTDB_SSL_ENABLE = "true"; // Set it to the SSL_ENABLE value.
public static void main(String[] args)
      throws IoTDBConnectionException, StatementExecutionException {
    // set iotdb_ssl_enable
    System.setProperty("iotdb_ssl_enable", IOTDB_SSL_ENABLE);
    if ("true".equals(IOTDB_SSL_ENABLE)) {  
      // set truststore.jks path  
      System.setProperty("iotdb_ssl_truststore", "truststore file path");
    }

    session = new Session(nodeUrls, Port, Authentication username,Authentication user password);
    session.open(false);

    // set session fetchSize
    session.setFetchSize(10000);

    try {
      session.setStorageGroup("root.sg1");
    } catch (StatementExecutionException e) {
      if (e.getStatusCode() != TSStatusCode.PATH_ALREADY_EXIST_ERROR.getStatusCode()) {
        throw e;
      }
    }

    createTimeseries();
    createMultiTimeseries();
    insertRecord();
    insertTablet();
    insertTablets();
    insertRecords();
    nonQuery();
    query();
    queryWithTimeout();
    rawDataQuery();
    queryByIterator();
    deleteData();
    deleteTimeseries();
    setTimeout();

    sessionEnableRedirect = new Session(nodeUrls, Authentication username, Authentication user password);
    sessionEnableRedirect.setEnableQueryRedirection(true);
    sessionEnableRedirect.open(false);

    // set session fetchSize
    sessionEnableRedirect.setFetchSize(10000);

    insertRecord4Redirect();
    query4Redirect();
    sessionEnableRedirect.close();
    session.close();
  }