fluxinfluxqlinfluxdb-pythoninfluxdb-2flux-influxdb

influxDB: How to convert field to tag in influxDB v2.0


We need to convert field to tag in influxDB v2.0 but not able to find any proper solution. Can someone help me out to achieve the same ?

Solution we found was to create new measurement by altering fields and tags of existing measurement but not able achieve it using Flux language.

Using below flux query we can copy the data from one measurement to another but not able to change the field to tag while adding data in new measurement.

from(bucket: "bucket_name")
    |> range(start: -10y)
    |> filter(fn: (r) => r._measurement == "cu_om")
    |> aggregateWindow(every: 5s, fn: last, createEmpty: false)
    |> yield(name: "last")
    |> set(key: "_measurement", value: "cu_om_new1")
    |> to(org: "org_name", bucket: "bucket_name")

Any help appreciated.


Solution

  • You're almost there with your original code, there are extra fields with the to() function that allow this. If you have a set of data already where you have a tag name as value, you can specify it as a tagColumn in to(). Also, the new tag(s) must be string(s).

    |> to(bucket: "NewBucketName",
       tagColumns: ["NewTagName"],
       fieldFn: (r) => ({"SomeValue": r._value })
       )