to() function

The to() function writes data to an InfluxDB v2.0 bucket.

*Function type: Output*

  1. to(
  2. bucket: "my-bucket",
  3. org: "my-org",
  4. timeColumn: "_time",
  5. tagColumns: ["tag1", "tag2", "tag3"],
  6. fieldFn: (r) => ({ [r._field]: r._value })
  7. )
  8. // OR
  9. to(
  10. bucketID: "1234567890",
  11. orgID: "0987654321",
  12. timeColumn: "_time",
  13. tagColumns: ["tag1", "tag2", "tag3"],
  14. fieldFn: (r) => ({ [r._field]: r._value })
  15. )

Output data requirements

The to() function converts output data into line protocol and writes it to InfluxDB. Line protocol requires each record to have a timestamp, a measurement, a field, and a value. All output data must include the following columns:

  • _time
  • _measurement
  • _field
  • _value

The to() function ignores rows with a null _time value and does not write them to InfluxDB.

Parameters

You must provide a bucket or bucketID and an org or orgID.

bucket

The bucket to write data to. bucket and bucketID are mutually exclusive.

*Data type: String*

bucketID

The ID of the bucket to write data to. bucketID and bucket are mutually exclusive.

*Data type: String*

org

The organization name of the specified bucket. org and orgID are mutually exclusive.

*Data type: String*

orgID

The organization ID of the specified bucket. orgID and org are mutually exclusive.

*Data type: String*

timeColumn

Time column of the output. Default is "_time".

*Data type: String*

tagColumns

Tag columns in the output. Defaults to all columns with type string, excluding all value columns and columns identified by fieldFn.

*Data type: Array of strings*

fieldFn

Function that takes a record from the input table and returns a record. For each record from the input table, fieldFn returns a record that maps the output field key to the output value. Default is (r) => ({ [r._field]: r._value })

*Data type: Function **Output data type:* Record

Make sure fieldFn parameter names match each specified parameter. To learn why, see Match parameter names.

Examples

Default to() operation

Given the following table:

_time_start_stop_measurement_field_value
000500000009“a”“temp”100.1
000600000009“a”“temp”99.3
000700000009“a”“temp”99.9

The default to operation:

  1. // ...
  2. |> to(bucket:"my-bucket", org:"my-org")

is equivalent to writing the above data using the following line protocol:

  1. _measurement=a temp=100.1 0005
  2. _measurement=a temp=99.3 0006
  3. _measurement=a temp=99.9 0007

Custom to() operation

The to() functions default operation can be overridden. For example, given the following table:

_time_start_stoptag1tag2humtemp
000500000009“a”“b”55.3100.1
000600000009“a”“b”55.499.3
000700000009“a”“b”55.599.9

The operation:

  1. // ...
  2. |> to(
  3. bucket:"my-bucket",
  4. org:"my-org",
  5. tagColumns:["tag1"],
  6. fieldFn: (r) => ({"hum": r.hum, "temp": r.temp})
  7. )

is equivalent to writing the above data using the following line protocol:

  1. _tag1=a hum=55.3,temp=100.1 0005
  2. _tag1=a hum=55.4,temp=99.3 0006
  3. _tag1=a hum=55.5,temp=99.9 0007

Related articles