TL;DR InfluxDB Tech Tips: Parameterized Flux Queries with InfluxDB
By
Anais Dotis-Georgiou /
Product, Use Cases, Developer
Jun 21, 2021
Navigate to:
If you’re familiar with InfluxDB v2 then you’re probably familiar with Flux already. Flux enables you to transform your data in any way you need and write custom tasks, checks, and notification rules. But what you might not know is that Flux now supports parameterized queries in InfluxDB v2 Cloud. A parameterized query enables you to supply arguments which are then inserted into the Flux query for it to be executed. Parameterized queries can only be used with the InfluxDB v2 API right now, but will be landing in the InfluxDB User Interface soon.
Writing your first parameterized Flux query
For the most basic example, we’ll pass in a bucket name as an argument to our parameterized Flux query. The Flux script looks like this:
"from(bucket:params.mybucket)
|> range(start: -7d)
|> limit(n:2)","params":{"mybucket":"telegraf"}"
The Flux engine will replace params.mybucket
with the bucket name that we want to query. We specify the value of the mybucket parameter at the end of the Flux query request payload with "params":{"mybucket":"telegraf"}
to query the "telegraf"
bucket. Since this query must be executed with the API, we convert the query into JSON to execute the following cURL request:
curl -X POST \
'https://us-west-2-1.aws.cloud2.influxdata.com/api/v2/query?orgID=<myOrgID>' \
-H 'authorization: Token <myToken>' \
-H 'content-type: application/json' \
-d '{"query":"from(bucket:params.mybucket) |> range(start: -7d) |> limit(n:2)","params":{"mybucket":"telegraf"}}'
That’s all there is to it! Now you can start constructing parameterized queries to secure your IoT application and help prevent injection attacks. Parameterized queries also make updating a query to reflect a new bucket, filter or timestamp easy and encourage code reuse.
Typing for parameterized Flux query
Using parameterized Flux queries is pretty straightforward. Parameterized Flux queries support parameters of int, float, and string types. However, Flux itself supports more types, such as duration and others. Therefore, you must make sure to correctly type date parameters. For example if you want to make a parameter a timestamp, you must convert that value into a duration with the duration() function. The body of your request should look like this:
{"query":"from(bucket:\"telegraf\") |> range(start: duration(v : params.mystart)) |> limit(n:2)","params":{"mystart":"-7d"}}
Final thoughts on parameterized Flux queries with InfluxDB
I hope this InfluxDB Tech Tips post inspires you to take advantage of parameterized Flux queries with InfluxDB v2. If you are using Flux and need help, please ask for some in our community site or Slack channel. If you’re developing a cool IoT application on top of InfluxDB, we’d love to hear about it, so make sure to share your story! Additionally, please share your thoughts, concerns or questions in the comments section. We’d love to get your feedback and help you with any problems you run into!