InfluxDB: Flux - Aggregation Function reduce(): Unterschied zwischen den Versionen
Aus Wiki-WebPerfect
Admin (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „ Kategorie:TIG-Stack“) |
Admin (Diskussion | Beiträge) |
||
Zeile 1: | Zeile 1: | ||
+ | == When should I use reduce() == | ||
+ | |||
+ | |||
+ | |||
+ | == Example == | ||
+ | ''Because the raw data in my example measurement "asset_hyperv_local" has a lot of tags I use "keep()" to decrease the number of columns.'' | ||
+ | |||
+ | === Flux === | ||
+ | bucket = "<YOUR_BUCKET>" | ||
+ | |||
+ | from(bucket: bucket) | ||
+ | |> range(start: v.timeRangeStart, stop: v.timeRangeStop) | ||
+ | |> filter(fn: (r) => | ||
+ | r._measurement == "asset_hyperv_local" and | ||
+ | r._field == "physicalmemory" and | ||
+ | r.cluster =~ /C1TN7.*/ | ||
+ | ) | ||
+ | |> last() | ||
+ | |> group(columns: ["cluster", "manufacturer"]) | ||
+ | |> keep(columns: ["_time", "_value", "cluster", "manufacturer", "_field"]) | ||
+ | </pre> | ||
+ | |||
+ | |||
+ | |||
+ | === Flux with the function "reduce()" in action === | ||
+ | <pre> | ||
+ | bucket = "<YOUR_BUCKET>" | ||
+ | |||
+ | from(bucket: bucket) | ||
+ | |> range(start: v.timeRangeStart, stop: v.timeRangeStop) | ||
+ | |> filter(fn: (r) => | ||
+ | r._measurement == "asset_hyperv_local" and | ||
+ | r._field == "physicalmemory" and | ||
+ | r.cluster =~ /C1TN7.*/ | ||
+ | ) | ||
+ | |> last() | ||
+ | |> group(columns: ["cluster", "manufacturer"]) | ||
+ | |> keep(columns: ["_time", "_value", "cluster", "manufacturer", "_field"]) | ||
+ | |> reduce( | ||
+ | fn: (r, accumulator) => ({ | ||
+ | NumberOfNodes: accumulator.NumberOfNodes + 1.0, | ||
+ | SumOfPhysicalMemory: r._value + accumulator.SumOfPhysicalMemory, | ||
+ | }), | ||
+ | identity: {NumberOfNodes: 0.0, SumOfPhysicalMemory: 0.0} | ||
+ | ) | ||
+ | </pre> | ||
+ | |||
Version vom 19. Mai 2021, 12:44 Uhr
Inhaltsverzeichnis
When should I use reduce()
Example
Because the raw data in my example measurement "asset_hyperv_local" has a lot of tags I use "keep()" to decrease the number of columns.
Flux
bucket = "<YOUR_BUCKET>"
from(bucket: bucket)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop) |> filter(fn: (r) => r._measurement == "asset_hyperv_local" and r._field == "physicalmemory" and r.cluster =~ /C1TN7.*/ ) |> last() |> group(columns: ["cluster", "manufacturer"]) |> keep(columns: ["_time", "_value", "cluster", "manufacturer", "_field"])
</pre>
Flux with the function "reduce()" in action
bucket = "<YOUR_BUCKET>" from(bucket: bucket) |> range(start: v.timeRangeStart, stop: v.timeRangeStop) |> filter(fn: (r) => r._measurement == "asset_hyperv_local" and r._field == "physicalmemory" and r.cluster =~ /C1TN7.*/ ) |> last() |> group(columns: ["cluster", "manufacturer"]) |> keep(columns: ["_time", "_value", "cluster", "manufacturer", "_field"]) |> reduce( fn: (r, accumulator) => ({ NumberOfNodes: accumulator.NumberOfNodes + 1.0, SumOfPhysicalMemory: r._value + accumulator.SumOfPhysicalMemory, }), identity: {NumberOfNodes: 0.0, SumOfPhysicalMemory: 0.0} )