All Products
Search
Document Center

Use Influx CLI (command-line interface) to connect TSDB For InfluxDB®

Last Updated: Apr 06, 2021

You can use the influx command-line interface (CLI) to connect to TSDB for InfluxDB® to read and write data. This topic describes how to use the influx CLI to connect to Alibaba Cloud TSDB for InfluxDB®. This topic also describes how to use the influx CLI to write and query data.

Prerequisites

A TSDB for InfluxDB® instance is purchased. To purchase a TSDB for InfluxDB® instance, visit the International site of Alibaba Cloud and perform the required operations as prompted. In addition, a TSDB for InfluxDB® account is created. To use the influx CLI to connect to TSDB for InfluxDB®, you must have a username and a password.

Download the influx CLI

Download the CLI by using this link: Download. On the download page, select v1.7.6.

Download a binary package that is specific to your operating system. The following figure shows the binary packages for different operating systems.

Then, decompress your binary package, and go to the folder that is extracted from the binary package. In the usr/bin/ directory, you can find the influx CLI. If you download a Windows binary package, you can find the influx.exe binary file in the folder that is extracted from the package. If you use the Mac OS X operating system, run the following commands to download the binary package and go to the directory where the influx CLI resides:

$ wget https://dl.influxdata.com/influxdb/releases/influxdb-1.7.6_darwin_amd64.tar.gz $ tar zxvf influxdb-1.7.6_darwin_amd64.tar.gz $ cd influxdb-1.7.6-1/usr/bin $

Connect TSDB For InfluxDB via CLI®

Open the terminal and enter the following command to connect to the instance you purchased:

$ ./influx -ssl -username <account name>-password <password>-host <network address>-port 3242

TSDB for InfluxDB® uses the Secure Sockets Layer (SSL) protocol to ensure secure data transmission. Therefore, each time you connect to a TSDB for InfluxDB® instance, you must set the following five parameters: -ssl, -username, -password, -host and -port.

Parameter description

  • -ssl: indicates that HTTPS is used to connect to TSDB for InfluxDB® server.

  • -username: specifies the username that is used to connect to the server.

  • -password: specifies the password for the username.

  • -host: specifies the domain name. You can find the domain name on the Instance Details page in the TSDB console. The following figure shows the Instance Details page.

  • -port: specifies a network port. The default port is port 3242.

Use the influx CLI to write data

Before you write data, you must create a database. For more information, see create a database. Assume that a database named mydb has been created.

  • Write a single point

    1. Connect to TSDB for InfluxDB® by using the influx CLI.

      $ ./influx -ssl -username <account name>-password <password>-host <network address>-port 3242
    2. Specify the mydb database to which the data is written.

      > USE mydb Using database mydb
    3. Write a single point to the mydb database.

      > INSERT cpu,host=serverA,region=us_west value=0.64

      This statement writes a data point to the default retention policy of the database mydb. Among them, the cpu is measurement, the host and region are tag, the value is field, and the format of the data point conforms to the line protocol. If no timestamp is provided, the system will use the local nanosecond Unix time of the server as the timestamp of the data point. In TSDB For InfluxDB®, any timestamp is UTC time (Coordinated Universal Time).

  • Write multiple points by importing files

    1. Prepare the sample data. The test.txt file contains multiple points.

      # DML # CONTEXT-DATABASE: mydb # CONTEXT-RETENTION-POLICY: autogen h2o_feet,location=coyote_creek water_level=3.524,level\ description="between 3 and 6 feet"1439868600 h2o_feet,location=coyote_creek water_level=3.399,level\ description="between 3 and 6 feet"1439868960 h2o_feet,location=coyote_creek water_level=3.278,level\ description="between 3 and 6 feet"1439869320 h2o_feet,location=coyote_creek water_level=3.159,level\ description="between 3 and 6 feet"1439869680 h2o_feet,location=coyote_creek water_level=3.048,level\ description="between 3 and 6 feet"1439870040 h2o_feet,location=coyote_creek water_level=2.943,level\ description="below 3 feet"1439870400 h2o_feet,location=coyote_creek water_level=2.831,level\ description="below 3 feet"1439870760 h2o_feet,location=coyote_creek water_level=2.717,level\ description="below 3 feet"1439871120 h2o_feet,location=coyote_creek water_level=2.625,level\ description="below 3 feet"1439871480 h2o_feet,location=coyote_creek water_level=2.533,level\ description="below 3 feet"1439871840
    2. Import data that is stored in the test.txt file to your TSDB for InfluxDB® instance.

      $ ./influx -ssl -username <account name>-password <password>-host <network address>-port 3242-import-path=test.txt

      Use parameter -import to import data from files into TSDB For InfluxDB®. Among them, the -path specifies the file to be imported. This statement will be measurement as h2o_feet, tag as location, and field written to database mydb for water_level and level description data.

Query data through CLI

TSDB for InfluxDB® supports a wide array of InfluxQL query statements. InfluxQL is an SQL-like query language that allows you to interact with databases in an easy and efficient way. For more information, see Data Exploration and Schema Exploration.

  1. Before you run queries, connect to TSDB for InfluxDB®.

    $ ./influx -ssl -username <account name>-password <password>-host <network address>-port 3242
  2. Specifies a database. In this example, the data that you want to query is stored in the mydb database:

    > USE mydb Using database mydb

Query all data in a specified measurement

> SELECT * FROM "h2o_feet" name: h2o_feet time level description location water_level ---------------------------------------- 1439868600 between 3and6 feet coyote_creek 3.524 1439868960 between 3and6 feet coyote_creek 3.399 1439869320 between 3and6 feet coyote_creek 3.278 1439869680 between 3and6 feet coyote_creek 3.159 1439870040 between 3and6 feet coyote_creek 3.048 1439870400 below 3 feet coyote_creek 2.943 1439870760 below 3 feet coyote_creek 2.831 1439871120 below 3 feet coyote_creek 2.717 1439871480 below 3 feet coyote_creek 2.625 1439871840 below 3 feet coyote_creek 2.533

This query returns all data corresponding to the measurement named "h2o_feet".

Query a specific field in a measurement and perform arithmetic operations

> SELECT ("water_level"+2)*3 FROM "h2o_feet" name: h2o_feet time water_level --------------- 143986860016.572 143986896016.197 143986932015.834000000000001 143986968015.477 143987004015.144 143987040014.828999999999999 143987076014.492999999999999 143987112014.151000000000002 143987148013.875 143987184013.598999999999998

In this example, the water_level field belongs to the h2o_feet measurement. This query adds two to each field value of the water_level field key and multiplies the result by three. Notes: TSDB for InfluxDB® follows the standard order of arithmetic operations. For more information, see mathematical operators.

Query data whose field value meets certain conditions

> SELECT * FROM "h2o_feet" WHERE "water_level">3 name: h2o_feet time level description location water_level ---------------------------------------- 1439868600 between 3and6 feet coyote_creek 3.524 1439868960 between 3and6 feet coyote_creek 3.399 1439869320 between 3and6 feet coyote_creek 3.278 1439869680 between 3and6 feet coyote_creek 3.159 1439870040 between 3and6 feet coyote_creek 3.048

This query returns the data that meets the following condition: The field values of the water_level field key in the h2o_feet measurement are greater than 3. For more information about the WHERE clause, see WHERE statements.

Group query results

> SELECT MEAN("water_level") FROM "h2o_feet" GROUP BY "location" name: h2o_feet tags: location=coyote_creek time mean -------- 03.0057

This query groups query results based on the location tag and calculates the average field value of the water_level field key for each tag value of the location tag key. For more information about the GROUP BY clause, see GROUP BY clauses.

Use regular expressions to specify field keys and tag keys

> SELECT /level/ FROM "h2o_feet" name: h2o_feet time level description water_level -------------------------------- 1439868600 between 3and6 feet 3.524 1439868960 between 3and6 feet 3.399 1439869320 between 3and6 feet 3.278 1439869680 between 3and6 feet 3.159 1439870040 between 3and6 feet 3.048 1439870400 below 3 feet 2.943 1439870760 below 3 feet 2.831 1439871120 below 3 feet 2.717 1439871480 below 3 feet 2.625 1439871840 below 3 feet 2.533

This query returns the values of the field keys and tag keys that contain the level string. For more information about regular expressions, see the Regular expressions.