Map specific values

RainT
Thu, 13 Feb 2025 23:04:08 +0000
Systems

Hello,
I’m trying to export a measurement from one InfluxDB2 to another. The measurement has multiple fields with different types (string, float, bool, etc.)

For the general case, I can ‘influx query … --raw "my flux query’ > xyz.csv file and then ‘influx write … -f xyz.csv’ and everything works perfectly. Unfortunately, in one of the buckets, some of the measurement fields’ strings are empty (“”) which results in a csv line like this snippet:

#group,false,false,false,false,true,true,true,true,true,true,true,true,true,true,true,true
#datatype,string,long,dateTime:RFC3339,string,string,string,string,string,string,string,string,string,string,string,string,string
#default,_result,,,,,,,,,,,,,,,
,result,table,_time,_value,_field,<...>
,,329,2025-01-29T22:49:17Z,,description,<...>

Annoyingly the csv that the query returns cannot be used with ‘influx write’ because of the row is missing the data field:
2025/02/12 17:46:42 line 529: no field data found

To work around this, I thought I could map string values that are “” to “-” during my query and thus have the ‘,-,’ in the exported csv rather than the ‘,’, however, I’m running into type errors.

Here’s my query:

import "strings"
import "types"

from(bucket: "mybucket")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "mymeasurement")
  |> map(fn: (r) => ({
    r with
      _value: if types.isType(v: r._value, type: "string") and r._value == "" then "-" else r._value
  }))

Unfortunately, when I run this query, I get:

 runtime error @8:6-11:6: map: type conflict: string != int

Can anyone provide some pointers? I’m at a loss for why this doesn’t work. It feels like the logic would be, if this field is a string and the string is empty, then map to “-” else use the original value. My guess is that I need to somehow run this specific map on only fields that are type string, but as a novice to influx, I’m struggling.

2 posts - 1 participant