Use curl to write dinfluxDB
In this article i will show you how to use curl a well known and trusted tool to write data to influxdb, probably now you are wondering…
Use curl to write data to influxDB
In this article i will show you how to use curl a well known and trusted tool to write data to influxdb, probably now you are wondering why someone would like to use curl to write data to influxdb? there are several good reasons to do this.
- No need for code: input data that are already in the line protocol format (a plain text file) can be used with curl to written to influxdb
- Flexibility: curl runs everywhere from embedded devices to Linux, MacOS and windows, can be used as a standalone command or part of a script
- Enhanced functionality : can write one line of data or a big file of lines at once
Lets see how we can use curl with influxdb
Line Protocol
The line protocol format is a text-based format that provides the measurement, tag set, field set, and timestamp of a data point.
// Syntax
<measurement>[,<tag_key>=<tag_value>[,<tag_key>=<tag_value>]] <field_key>=<field_value>[,<field_key>=<field_value>] [<timestamp>]
// Example
myMeasurement,tag1=value1,tag2=value2 fieldKey="fieldValue" 1556813561098000000- Measurement: (Required) InfluxDB accepts one measurement per point. Measurement names are case-sensitive
- Tag Set: (Optional) — All tag key-value pairs for the point. Key-value relationships are denoted with the
=operand. Multiple tag key-value pairs are comma-delimited. Tag keys and tag values are case-sensitive. - Field Set: (Required) All field key-value pairs for the point. Points must have at least one field. Field keys and string values are case-sensitive.
- Timestamp: (Optional) — The unix timestamp for the data point. InfluxDB accepts one timestamp per point. If no timestamp is provided, InfluxDB uses the system time (UTC) of its host machine.
Now that we know the format of line protocol lets see how we can use curl to write data to influxDB
Using curl to write data
Lets assume that we dont have an influxDB database, to create a database using influx use the following command (adjusted to your needs)
curl -i -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE mydb"Then depending your influxDB version you can write data using the following commands
InfluxDB ≤ 1.8
To write a single measurement:
curl -i -XPOST 'http://localhost:8086/write?db=mydb'
--data-binary 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'To write from a file:
We create file cpu_data.txt with the following data
cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000
cpu_load_short,host=server02,region=us-west value=0.60 1434055563000000000
cpu_load_short,host=server01,region=us-west value=0.74 1434055564000000000Then we enter:
curl -i -XPOST 'http://localhost:8086/write?db=mydb' --data-binary @cpu_data.txtIf we need authentication in our query we can use the in our curl statement as part of basic authentication
-u username:passwordInfluxDB ≥ 1.8
If the influxDB version is version greater or equal to 1.8 we can use the following syntax
curl -i -XPOST 'http://localhost:8086/api/v2/write?bucket=db/rp&precision=ns' \
--header 'Authorization: Token username:password' \
--data-raw 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'To write a file of data to the database we enter:
curl -i -XPOST 'http://localhost:8086/api/v2/write?bucket=db/rp&precision=ns' \
--header 'Authorization: Token username:password' \
--data-binary @cpu_data.txtConclusion
Using curl to write data to influxDB is super easy! i think it will make your life easier and will allow you to complete faster than using a language like Python. Of course if you need to do something complex curl might not be a suitable solution for you but good to know that exists as an option :)