Documentation

to() function

Flux 0.7.0+

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

to(
  bucket: "my-bucket",
  org: "my-org",
  host: "localhost:8086",
  token: "mY5uP3rS3cRe7t0k3n",
  timeColumn: "_time",
  tagColumns: ["tag1", "tag2", "tag3"],
  fieldFn: (r) => ({ r._field: r._value })
)

// OR

to(
  bucketID: "1234567890",
  orgID: "0987654321",
  host: "localhost:8086",
  token: "mY5uP3rS3cRe7t0k3n",
  timeColumn: "_time",
  tagColumns: ["tag1", "tag2", "tag3"],
  fieldFn: (r) => ({ r._field: r._value })
)

to() does not require a package import

to() is part of the influxdata/influxdb package, but is included with the universe package by default and does not require an import statement or package namespace.

Output data requirements

to() writes data structured using the standard InfluxDB v2.x and InfluxDB Cloud data structure that includes, at a minimum, the following columns:

  • _time
  • _measurement
  • _field
  • _value

All other columns are written to InfluxDB as tags.

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

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

bucketID

Bucket ID to write data to. bucketID and bucket are mutually exclusive.

org

InfluxDB organization name. org and orgID are mutually exclusive.

orgID

InfluxDB organization ID. orgID and org are mutually exclusive.

host

InfluxDB URL or InfluxDB Cloud region URL to write to.

host is required when writing to a remote InfluxDB instance. If specified, token is also required.

token

InfluxDB API token.

token is required when writing to another organization or when writing to a remote InfluxDB host.

timeColumn

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

tagColumns

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

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 }) 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
0005 0000 0009 “a” “temp” 100.1
0006 0000 0009 “a” “temp” 99.3
0007 0000 0009 “a” “temp” 99.9

The default to operation:

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

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

_measurement=a temp=100.1 0005
_measurement=a temp=99.3 0006
_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 _stop tag1 tag2 hum temp
0005 0000 0009 “a” “b” 55.3 100.1
0006 0000 0009 “a” “b” 55.4 99.3
0007 0000 0009 “a” “b” 55.5 99.9

The operation:

// ...
|> to(
  bucket:"my-bucket",
  org:"my-org",
  tagColumns:["tag1"],
  fieldFn: (r) => ({"hum": r.hum, "temp": r.temp})
)

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

_tag1=a hum=55.3,temp=100.1 0005
_tag1=a hum=55.4,temp=99.3 0006
_tag1=a hum=55.5,temp=99.9 0007

Upgrade to InfluxDB Cloud or InfluxDB 2.0!

InfluxDB Cloud and InfluxDB OSS 2.0 ready for production.