Is there a recommended approach to making InfluxDB 2 queries from R?
I am struggling with using the httr package to post queries using the InfluxDB2 API even though I am having no difficulties using CURL. I suppose I could write some Python code to execute from R but that doesn’t seem particularly elegant. Before I get too involved on this one I was hoping someone might offer some guidance.
Thanks,
– Rich
1 Like
Whatever the outcome, pls share the final solution you end up using. I’d be very interested too
@FixTestRepeat After stumbling a bit, I have had some success with the httr package for R. Once I get things into slightly better shape, I will summarize my approach here.
Here is an R code fragment that works for me. Please let me know if you spot any glaring problems.
In this example, I have a bucket called exercise
where the relevant measurement is named activity
and each data point has a tag named Activity_Group
. I want to get the value of the Distance
field for each activity since the start of 2021 (I am in a UTC -6 timezone).
require (httr)
require (readr)
Influx_URL <- "http://replace-with-your-host-name:8086/api/v2/query?org=replace-with-your-org"
Influx_Token <- "replace-with-your-influx-token"
Influx_Authorization_Header <- paste ("Token", Influx_Token)
query <- 'from(bucket:"exercise")
|> range(start: 2021-01-01T06:00:00Z)
|> filter(fn:(r) => r._measurement == "activity")
|> filter(fn:(r) => r.Activity_Group == "Cycling")
|> filter(fn:(r) => r._field == "Distance")'
response <-
POST (
Influx_URL,
body = query,
accept("application/csv"),
content_type("application/vnd.flux"),
add_headers(Authorization = Influx_Authorization_Header)
)
content <- content (response)
And finally, content
yields:
# A tibble: 15 x 10
X1 result table `_start` `_stop` `_time` `_value` Activity_Group `_field` `_measurement`
<lgl> <chr> <dbl> <dttm> <dttm> <dttm> <dbl> <chr> <chr> <chr>
1 NA _result 0 2021-01-01 06:00:00 2021-03-08 18:10:19 2021-01-03 11:57:11 30.9 Cycling Distance activity
2 NA _result 0 2021-01-01 06:00:00 2021-03-08 18:10:19 2021-01-03 15:45:00 0 Cycling Distance activity
3 NA _result 0 2021-01-01 06:00:00 2021-03-08 18:10:19 2021-01-10 11:11:58 30.1 Cycling Distance activity
4 NA _result 0 2021-01-01 06:00:00 2021-03-08 18:10:19 2021-01-10 17:01:36 0 Cycling Distance activity
5 NA _result 0 2021-01-01 06:00:00 2021-03-08 18:10:19 2021-01-17 10:19:00 30.2 Cycling Distance activity
6 NA _result 0 2021-01-01 06:00:00 2021-03-08 18:10:19 2021-01-17 16:09:01 0 Cycling Distance activity
7 NA _result 0 2021-01-01 06:00:00 2021-03-08 18:10:19 2021-01-24 18:05:41 0 Cycling Distance activity
8 NA _result 0 2021-01-01 06:00:00 2021-03-08 18:10:19 2021-01-24 22:54:43 36.0 Cycling Distance activity
9 NA _result 0 2021-01-01 06:00:00 2021-03-08 18:10:19 2021-01-31 18:47:28 0 Cycling Distance activity
10 NA _result 0 2021-01-01 06:00:00 2021-03-08 18:10:19 2021-01-31 23:21:34 36.0 Cycling Distance activity
11 NA _result 0 2021-01-01 06:00:00 2021-03-08 18:10:19 2021-02-07 22:49:54 12.2 Cycling Distance activity
12 NA _result 0 2021-01-01 06:00:00 2021-03-08 18:10:19 2021-02-25 14:22:25 16.5 Cycling Distance activity
13 NA _result 0 2021-01-01 06:00:00 2021-03-08 18:10:19 2021-02-25 18:06:01 14.0 Cycling Distance activity
14 NA _result 0 2021-01-01 06:00:00 2021-03-08 18:10:19 2021-02-27 22:22:06 35.3 Cycling Distance activity
15 NA _result 0 2021-01-01 06:00:00 2021-03-08 18:10:19 2021-03-06 15:26:18 40.0 Cycling Distance activity
1 Like