Export InfluxDB Data to CSV

0
19726
Export InfluxDB Data to CSV
Export InfluxDB Data to CSV

In this post, I will show you a tip to export InfluxDB data to CSV format. You can use the exported sample data for analysis or whatever purpose.

There are two methods to export InfluxDB data to CSV you can use:

  1. Using client influx
  2. Using HTTP API

Let’s walkthrough both of them.

Using client influx

Once you install InfluxDB, you will have access to the binary tool influx, which is commonly located under the bin directory. You can confirm the client by issuing the command:

$ influx --version
InfluxDB shell version: 1.9.6

To export InfluxDB data to CSV, use the following command:

$ influx -database DB_NAME -host DB_HOST -username DB_USER  -password DB_PASS -execute QUERY -format csv > FILENAME.csv

You should replace DB_NAME, DB_HOST, DB_USER, DB_PASS, QUERY and FILENAME with your input.

If there is no username or password configured on InfluxDB, you can omit them.

In case you access Influx from the local shell, you don’t have to provide -host DB_HOST, since its default value is already 127.0.0.1.

This is a sample command you can refer:

$ influx -database serverdata -execute "SELECT * FROM memory_usage" -format csv > memory_usage.csv

Make sure to wrap your QUERY around the double-quotes.

Using HTTP API

Similarly, the second method leverages the usage HTTP API provided by InfluxDB.

To export InfluxDB to CSV using HTTP API, use the following command, which is done by calling curl.

$ curl -H 'Accept: application/csv' -G 'http://HOST:PORT/query?db=DATABASE' --data-urlencode 'q=QUERY' > FILE.csv

Replace HOST, PORT, DATABASE, QUERY and FILE with your input.

For example:

$ curl -H 'Accept: application/csv' -G 'http://127.0.0.1:8086/query?db=serverdata' --data-urlencode 'q=SELECT * FROM memory_usage' > memory_usage.csv

If you need to pass some parameters to the query, you can add another params query data: --data-urlencode 'params={"PARAM":"VALUE"}'.

For example:

$ curl -H 'Accept: application/csv' -G 'http://127.0.0.1:8086/query?db=serverdata' --data-urlencode 'q=SELECT * FROM memory_usage WHERE os_type=$type' --data-urlencode 'params={"type": "linux"}' > memory_usage.csv

Conclusion

I’ve shown you two methods to export InfluxDB to CSV file. Both ways are supported clean and work out of the box.

Have fun!