Skip to main content

Connect

Any application programs running on any kind of platform can access TDengine through the REST API provided by TDengine. For details, please refer to REST API. Additionally, application programs can use the connectors of multiple programming languages including C/C++, Java, Python, Go, Node.js, C#, Rust to access TDengine. This chapter describes how to establish a connection to TDengine and briefly introduces how to install and use connectors. TDengine community also provides connectors in LUA and PHP languages. For details about the connectors, please refer to Connectors.

Establish Connection

There are two ways for a connector to establish connections to TDengine:

  1. Connection through the REST API provided by the taosAdapter component, this way is called "REST connection" hereinafter.
  2. Connection through the TDengine client driver (taosc), this way is called "Native connection" hereinafter.

Key differences:

  1. The TDengine client driver (taosc) has the highest performance with all the features of TDengine like Parameter Binding, Subscription, etc.
  2. The TDengine client driver (taosc) is not supported across all platforms, and applications built on taosc may need to be modified when updating taosc to newer versions.
  3. The REST connection is more accessible with cross-platform support, however it results in a 30% performance downgrade.

Install Client Driver taosc

If you are choosing to use the native connection and the the application is not on the same host as TDengine server, the TDengine client driver taosc needs to be installed on the application host. If choosing to use the REST connection or the application is on the same host as TDengine server, this step can be skipped. It's better to use same version of taosc as the TDengine server.

Install

  1. Download the TDengine client installation package

    All Packages

    1. Unzip

      Download the package to any directory the current user has read/write permission. Then execute tar -xzvf TDengine-client-VERSION.tar.gz command. The VERSION should be the version of the package you just downloaded.

    2. Execute the install script

      Once the package is unzipped, you will see the following files in the directory:

      • _ install_client.sh_: install script
      • _ taos.tar.gz_: client driver package
      • _ driver_: TDengine client driver
      • examples: some example programs of different programming languages (C/C#/go/JDBC/MATLAB/python/R)

      You can run install_client.sh to install it.

    3. Edit taos.cfg

      Edit taos.cfg file (full path is /etc/taos/taos.cfg by default), modify firstEP with actual TDengine server's End Point, for example h1.tdengine.com:6030

    tip
    1. If the computer does not run the TDengine service but installs the TDengine client driver, then you need to config firstEP in taos.cfg only, and there is no need to configure FQDN;

    2. If you encounter the "Unable to resolve FQDN" error, please make sure the FQDN in the /etc/hosts file of the current computer is correctly configured, or the DNS service is correctly configured.

    Verify

    After the above installation and configuration are done and making sure TDengine service is already started and in service, the TDengine command-line interface taos can be launched to access TDengine.

    Execute TDengine CLI program taos directly from the Linux shell to connect to the TDengine service and enter the TDengine CLI interface, as shown in the following example.

    $ taos
    Welcome to the TDengine shell from Linux, Client Version:2.0.5.0
    Copyright (c) 2017 by TAOS Data, Inc. All rights reserved.
    taos> show databases;
    name | created_time | ntables | vgroups | replica | quorum | days | keep1,keep2,keep(D) | cache(MB)| blocks | minrows | maxrows | wallevel | fsync | comp | precision | status |
    =========================================================================================================================================================================================================================
    test | 2020-10-14 10:35:48.617 | 10 | 1 | 1 | 1 | 2 | 3650,3650,3650 | 16| 6 | 100 | 4096 | 1 | 3000 | 2 | ms | ready |
    log | 2020-10-12 09:08:21.651 | 4 | 1 | 1 | 1 | 10 | 30,30,30 | 1| 3 | 100 | 4096 | 1 | 3000 | 2 | us | ready |
    Query OK, 2 row(s) in set (0.001198s)
    taos>

    Install Connectors

    If maven is used to manage the projects, what needs to be done is only adding below dependency in pom.xml.

    <dependency>
    <groupId>com.taosdata.jdbc</groupId>
    <artifactId>taos-jdbcdriver</artifactId>
    <version>2.0.38</version>
    </dependency>

    Establish Connection

    Prior to establishing connection, please make sure TDengine is already running and accessible. The following sample code assumes TDengine is running on the same host as the client program, with FQDN configured to "localhost" and serverPort configured to "6030".

    Native Connection
    package com.taos.example;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.Properties;

    import com.taosdata.jdbc.TSDBDriver;

    public class JNIConnectExample {
    public static void main(String[] args) throws SQLException {
    String jdbcUrl = "jdbc:TAOS://localhost:6030?user=root&password=taosdata";
    Properties connProps = new Properties();
    connProps.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
    connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
    connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
    Connection conn = DriverManager.getConnection(jdbcUrl, connProps);
    System.out.println("Connected");
    conn.close();
    }
    }

    // use
    // String jdbcUrl = "jdbc:TAOS://localhost:6030/dbName?user=root&password=taosdata";
    // if you want to connect a specified database named "dbName".

    view source code

    REST Connection
        public static void main(String[] args) throws SQLException {
    String jdbcUrl = "jdbc:TAOS-RS://localhost:6041?user=root&password=taosdata";
    Connection conn = DriverManager.getConnection(jdbcUrl);
    System.out.println("Connected");
    conn.close();
    }

    view source code

    When using REST connection, the feature of bulk pulling can be enabled if the size of resulting data set is huge.

    Enable Bulk Pulling
        public static void main(String[] args) throws SQLException {
    String jdbcUrl = "jdbc:TAOS-RS://localhost:6041?user=root&password=taosdata";
    Properties connProps = new Properties();
    connProps.setProperty(TSDBDriver.PROPERTY_KEY_BATCH_LOAD, "true");
    Connection conn = DriverManager.getConnection(jdbcUrl, connProps);
    System.out.println("Connected");
    conn.close();
    }

    view source code

    More configuration about connection,please refer to Java Connector

    tip

    If the connection fails, in most cases it's caused by improper configuration for FQDN or firewall. Please refer to the section "Unable to establish connection" in FAQ.