Documentation

Use the influxctl CLI to write line protocol data

Use the influxctl CLI to write line protocol data to InfluxDB Cloud Dedicated.

Construct line protocol

With a basic understanding of line protocol, you can now construct line protocol and write data to InfluxDB. Consider a use case where you collect data from sensors in your home. Each sensor collects temperature, humidity, and carbon monoxide readings. To collect this data, use the following schema:

  • measurement: home
    • tags
      • room: Living Room or Kitchen
    • fields
      • temp: temperature in °C (float)
      • hum: percent humidity (float)
      • co: carbon monoxide in parts per million (integer)
    • timestamp: Unix timestamp in second precision

The following line protocol represent the schema described above:

home,room=Living\ Room temp=21.1,hum=35.9,co=0i 1641024000
home,room=Kitchen temp=21.0,hum=35.9,co=0i 1641024000
home,room=Living\ Room temp=21.4,hum=35.9,co=0i 1641027600
home,room=Kitchen temp=23.0,hum=36.2,co=0i 1641027600
home,room=Living\ Room temp=21.8,hum=36.0,co=0i 1641031200
home,room=Kitchen temp=22.7,hum=36.1,co=0i 1641031200
home,room=Living\ Room temp=22.2,hum=36.0,co=0i 1641034800
home,room=Kitchen temp=22.4,hum=36.0,co=0i 1641034800
home,room=Living\ Room temp=22.2,hum=35.9,co=0i 1641038400
home,room=Kitchen temp=22.5,hum=36.0,co=0i 1641038400
home,room=Living\ Room temp=22.4,hum=36.0,co=0i 1641042000
home,room=Kitchen temp=22.8,hum=36.5,co=1i 1641042000

For this tutorial, you can either pass this line protocol directly to the influxctl write command as a string, via stdin, or you can save it to and read it from a file.

Write the line protocol to InfluxDB

Use the influxctl write command to write the home sensor sample data to your InfluxDB Cloud Dedicated cluster. Provide the following:

  • The database name using the --database flag

  • A database token (with write permissions on the target database) using the --token flag

  • The timestamp precision as seconds (s) using the --precision flag

  • Line protocol. Pass the line protocol in one of the following ways:

    • a string on the command line
    • a path to a file that contains the query
    • a single dash (-) to read the query from stdin
influxctl write \
  --database 
DATABASE_NAME
\
--token
DATABASE_TOKEN
\
--precision s \ 'home,room=Living\ Room temp=21.1,hum=35.9,co=0i 1641024000 home,room=Kitchen temp=21.0,hum=35.9,co=0i 1641024000 home,room=Living\ Room temp=21.4,hum=35.9,co=0i 1641027600 home,room=Kitchen temp=23.0,hum=36.2,co=0i 1641027600 home,room=Living\ Room temp=21.8,hum=36.0,co=0i 1641031200 home,room=Kitchen temp=22.7,hum=36.1,co=0i 1641031200 home,room=Living\ Room temp=22.2,hum=36.0,co=0i 1641034800 home,room=Kitchen temp=22.4,hum=36.0,co=0i 1641034800 home,room=Living\ Room temp=22.2,hum=35.9,co=0i 1641038400 home,room=Kitchen temp=22.5,hum=36.0,co=0i 1641038400 home,room=Living\ Room temp=22.4,hum=36.0,co=0i 1641042000 home,room=Kitchen temp=22.8,hum=36.5,co=1i 1641042000'

Replace the following:

  • DATABASE_NAME: Name of the database to write to.
  • DATABASE_TOKEN: Database token with write permissions on the target database.
  1. In your terminal, enter the following command to create the sample data file:

    cat <<EOF > ./home.lp && LINE_PROTOCOL_FILEPATH=./home.lp
    home,room=Living\ Room temp=21.1,hum=35.9,co=0i 1641024000
    home,room=Kitchen temp=21.0,hum=35.9,co=0i 1641024000
    home,room=Living\ Room temp=21.4,hum=35.9,co=0i 1641027600
    home,room=Kitchen temp=23.0,hum=36.2,co=0i 1641027600
    home,room=Living\ Room temp=21.8,hum=36.0,co=0i 1641031200
    home,room=Kitchen temp=22.7,hum=36.1,co=0i 1641031200
    home,room=Living\ Room temp=22.2,hum=36.0,co=0i 1641034800
    home,room=Kitchen temp=22.4,hum=36.0,co=0i 1641034800
    home,room=Living\ Room temp=22.2,hum=35.9,co=0i 1641038400
    home,room=Kitchen temp=22.5,hum=36.0,co=0i 1641038400
    home,room=Living\ Room temp=22.4,hum=36.0,co=0i 1641042000
    home,room=Kitchen temp=22.8,hum=36.5,co=1i 1641042000
    
    EOF
    
  1. Enter the following CLI command to write the data from the sample file:

    influxctl write \
      --database 
    DATABASE_NAME
    \
    --token
    DATABASE_TOKEN
    \
    --precision s \
    $LINE_PROTOCOL_FILEPATH

Replace the following:

  • DATABASE_NAME: Name of the database to write to.
  • DATABASE_TOKEN: Database token with write permissions on the target database.
  • $LINE_PROTOCOL_FILEPATH: File path to the file containing the line protocol. Can be an absolute file path or relative to the current working directory.
cat 
$LINE_PROTOCOL_FILEPATH
| influxctl write \
--database
DATABASE_NAME
\
--token
DATABASE_TOKEN
\
--precision s \ -

Replace the following:

  • DATABASE_NAME: Name of the database to write to.
  • DATABASE_TOKEN: Database token with write permissions on the target database.
  • $LINE_PROTOCOL_FILEPATH: File path to the file containing the line protocol. Can be an absolute file path or relative to the current working directory.

Was this page helpful?

Thank you for your feedback!


The future of Flux

Flux is going into maintenance mode. You can continue using it as you currently are without any changes to your code.

Read more

InfluxDB v3 enhancements and InfluxDB Clustered is now generally available

New capabilities, including faster query performance and management tooling advance the InfluxDB v3 product line. InfluxDB Clustered is now generally available.

InfluxDB v3 performance and features

The InfluxDB v3 product line has seen significant enhancements in query performance and has made new management tooling available. These enhancements include an operational dashboard to monitor the health of your InfluxDB cluster, single sign-on (SSO) support in InfluxDB Cloud Dedicated, and new management APIs for tokens and databases.

Learn about the new v3 enhancements


InfluxDB Clustered general availability

InfluxDB Clustered is now generally available and gives you the power of InfluxDB v3 in your self-managed stack.

Talk to us about InfluxDB Clustered