Setting up InfluxDB v2 (Flux) with InfluxQL in Grafana
In November 2020 InfluxDB released the stable version of open source InfluxDB v2. InfluxDB v2 uses a new v2 API and new Flux data scripting language. Flux uses functional language patterns and is an alternative to InfluxQL and other SQL-like query languages for querying and analyzing data.
If you are currently starting with InfluxDB or setting up InfluxDB Cloud, you are very likely going to be using v2. And if you want to add this data source instance to Grafana, you should primarily use the InfluxDB data source and choose the Flux query language option. If you do so, setting up your Flux data source is very straight forward.
But what happens, if you are proficient in SQL and at this moment, you don’t have time to learn a new querying language? What if you want to use InfluxQL to write your queries? Is there a way you can set up InfluxDB v2 data source as InfluxDB v1 data source and use InfluxQL?
Yes there is! And I am going to walk you through the process to make it easy and straightforward for you.
There are 2 steps we need to do:
- Set up InfluxDB v2 as InfluxDB v1 data source through API v1 compatible endpoints.
- Map InfluxDB v2 buckets to InfluxDB v1 databases.
Set up InfluxDB v2 as InfluxDB v1 data source through API v1 compatible endpoints
To add your data source to Grafana, go to Data sources > Add data source and choose InfluxDB built in data source. Choose InfluxQL from the Query language dropdown.
HTTP
As a first step, we need to input the URL that is accessible from your Grafana.
Tip: If you are using InfluxDB Cloud 2.0, depending on your location and platform, the url will look similarly to https://eu-central-1-1.aws.cloud2.influxdata.com/. If you are running InfluxDB v2 locally, your url should be http://localhost:8086.
Custom HTTP details
All read and write requests to InfluxDB v2 must be authenticated. In Grafana, we are going to use the Authorization header with Token yourAuthToken value. This token has to have access to the below specified bucket.
Custom HTTP details:
Authorization: Active token in Token yourAuthToken form. Token can be created in load-data/tokens that has access to the specified bucket.
InfluxDB details
The next step is to add the InfluxDB details. You need to fill out the Database, which is going to be the name of the default bucket that we would like to query. Lastly, choose a preferred HTTP method.
InfluxDB Details:
Database: Name (not the id) of the bucket that you want to use. You can find it in load-data/buckets. Keep in mind that the provided token has to have read access to that bucket.
Save & test
Press Save & test button. You should get a confirmation message that “Data source is working”.
Map InfluxDB v2 buckets to InfluxDB v1 databases
Map buckets to databases
In InfluxDB v1, data is stored in databases and retention policies. In InfluxDB v2, data is stored in buckets. Because InfluxQL uses the v1 data model, before querying in InfluxQL, a bucket must be mapped to a database and retention policy. You can learn more here.
So to be able to query buckets, you have to map unmapped buckets following these detailed instructions. You can use Influx CLI or InfluxDB API. I have decided to use InfluxDB API and I run in my terminal following (you have to have curl or alternatively you can use API client such as Postman):
curl — request POST https://eu-central-1-1.aws.cloud2.influxdata.com/api/v2/dbrps \ — header “Authorization: Token YourAuthToken” \ — header ‘Content-type: application/json’ \ — data ‘{“bucketID”: “00oxo0oXx000x0Xo”,“database”: “example-db”,“default”: true,“orgID”: “00oxo0oXx000x0Xo”,“retention_policy”: “example-rp”}’
Don’t forget to replace the URL, auth token, bucketID, database, orgID and retention policy with your values.
Test in Grafana
The last step is to go to Grafana, choose your newly set InfluxDB data source and try to run a query to see that the setting up process was successful.