Shahar Sadkovich
Shahar Sadkovich

Reputation: 110

InfluxDB - limit query result by number of series using Flux

I'm trying to query my InfluxDB (1.8) using Flux and retrieve only 100 series, at first I thought the "limit" function will do it, however, I found out it only limits the number of records in each table (series) which can result in max(100) * N(series). then I tried a workaround:

from(bucket: "bucket")
 |> range(start:1970-01-01T00:00:00Z)
 |> filter(fn: (r) => (r["_measurement"] == "measurement" ))
 |> group()
 |> limit(n:100)
 |> group(columns:["column1","column2"])

by doing so, I'm able to group all results into a single table and limit the results, however, it's not even close to what I need. I'm retrieving only 100 points and also losing the ability to regroup by columns. I know that by using InfluxQL "SLIMIT" function, it can be done.

Any thoughts about how I can achieve that using flux query language? Thanks!

Upvotes: 1

Views: 2441

Answers (1)

Mathe S
Mathe S

Reputation: 71

I had the some problem and indeed found no solution online.

Now after some tests I found a hacky solution that might help. As I understood from influxdb, there cant be several tag values in one table ... or so. So after grouping you have many tables with some or even just one value.

So, what I did, was to get rid of the tags without loosing them - and this seems a little hacky: move the tag to _field, drop it and done.

Here an example:

from(bucket: "current")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "jobs")
  |> filter(fn: (r) => r["_field"] == "DurationSum")
  |> group (columns: ["jobName"])   // all durations - each jobname has its table
  |> last()                         // each table has only the last value
  |> drop (columns: ["_start", "_stop", "_time"])
  |> map(fn: (r) => ({ r with _field: r.jobName }))   // hack: transfer the tag-name
  |> drop (columns: ["jobName"])                      // Now there is only ONE table
  |> sort (desc: true)                  
  |> limit (n: 10)

Upvotes: 1

Related Questions