在Flux中对单列进行多次操作 [英] Multiple operation on single column in Flux

查看:65
本文介绍了在Flux中对单列进行多次操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与InfluxQL相比,尝试重新创建Flux中的一些基础知识是很困难的事情.

Trying to recreate some of the basics in Flux compared to InfluxQL is quite a stretch at present.

我不知道如何提出多个预测.

I can’t work out how to ask for multiple projections.

select max(temp) - min(temp) from my_measurement

我不确定如何在Flux中进行此操作.

I’m not sure how to go about this in Flux.

谢谢

推荐答案

选项1-内置函数

最简单的方法可能是使用内置的传播函数:

spread()函数输出指定列中的最小值和最大值之差.

The spread() function outputs the difference between the minimum and maximum values in a specified column.

from(bucket: "example-bucket")
  |> range(start: -5m)
  |> filter(fn: (r) =>
    r._measurement == "cpu" and
    r._field == "usage_system"
  )
  |> spread()

选项2-自定义功能

如果要完全控制逻辑,请创建自定义聚合函数即可.与此类似(假设您的数据至少具有一个正值):

Option 2 - custom function

If you want complete control over the logic, creating a custom aggregate function for this would work. Something along the lines of this (assuming your data has at least one positive value):

delta = (tables=<-, outputField="delta") =>
  tables
    |> reduce(
      // Define the initial accumulator record
      identity: {
        maximum: 0.0,
        minimum: math.maxfloat,
        delta: 0.0
      },
      fn: (r, accumulator) => ({
        // update max and min on each reduce loop
        maximum: if r._value > accumulator.maximum then r._value else accumulator.maximum,
        minimum: if r._value < accumulator.minimum then r._value else accumulator.minimum,
        // take the delta
        delta: accumulator.maximum - accumulator.minimum
      })
    )
    // Set the _field label of the output table to to the value
    // provided in the outputField parameter
    |> set(key: "_field", value: outputField)
    // Rename delta column to _value
    |> rename(columns: {delta: "_value"})
    // Optionally, Drop the max and min columns since they are no longer needed
//     |> drop(columns: ["maximum", "minimum"])


// apply your custom function wherever you want to calculate a delta
from(bucket: "my-bucket")
  |> range(start: -1h)
// optionally filter here to isolate the field that you want 
// |> filter(fn: (r) => <predicate>)
  |> delta()

这将从最后一个 range 值的数据(在此示例中为-h)产生最大值和最小值之间的增量.创建自定义函数后,您可以在任何流上使用它.

This would yield the delta between the max and min values from the last range worth of data (-1h in this example). Once you create the custom function, you can use it on any stream.

这篇关于在Flux中对单列进行多次操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆