docs-v2/content/v2.0/write-data/_index.md

7.6 KiB

title list_title description weight menu v2.0/tags
Write data to InfluxDB Write data Collect and write time series data to InfluxDB using line protocol, Telegraf, data scrapers, the InfluxDB v2 API, `influx` CLI, the InfluxDB UI, and client libaries. 2
v2_0
name
Write data
write
line protocol

Collect and write time series data to InfluxDB using line protocol, Telegraf, data scrapers, the InfluxDB v2 API, influx command line interface (CLI), the InfluxDB user interface (UI), and client libraries.

What you'll need

To write data into InfluxDB, you need the following:

The InfluxDB setup process creates each of these.

Use line protocol format to write data into InfluxDB. Each line represents a data point. Each point requires a measurement and field set and may also include a tag set and a timestamp.

Line protocol data looks like this:

mem,host=host1 used_percent=23.43234543 1556892576842902000
cpu,host=host1 usage_user=3.8234,usage_system=4.23874 1556892726597397000
mem,host=host1 used_percent=21.83599203 1556892777007291000

Timestamp precision

Timestamps are essential in InfluxDB. If a data point does not include a timestamp when it is received by the database, InfluxDB uses the current system time (UTC) of its host machine.

The default precision for timestamps is in nanoseconds. If the precision of the timestamps is anything other than nanoseconds (ns), you must specify the precision in your write request. InfluxDB accepts the following precisions:

  • ns - Nanoseconds
  • us - Microseconds
  • ms - Milliseconds
  • s - Seconds

For more details about line protocol, see the Line protocol reference and Best practices for writing data.

Ways to write data into InfluxDB

To write data into InfluxDB, use one of the following methods:

User Interface

To quickly start writing data, use the provided user interface.

  1. Do one of the following:

    • InfluxDB 2.0 OSS users: In your terminal, run influxd and then in your browser, go to the location where you're hosting the UI (by default, localhost:9999).
    • InfluxDB 2.0 Cloud users: In your browser, go to https://cloud2.influxdata.com/.
  2. Click Load Data in the navigation menu on the left.

  3. Select Buckets.

  4. Under the bucket you want to write data to, click {{< icon "plus" >}} Add Data.

  5. Select from the following options:


Configure Telegraf Agent

To configure a Telegraf agent, see Automatically create a Telegraf configuration.


Line Protocol

  1. Select Upload File or Enter Manually.
    • Upload File: Select the time precision of your data. Drag and drop the line protocol file into the UI or click to select the file from your file manager.
    • Enter Manually: Select the time precision of your data. Manually enter line protocol.
  2. Click Continue. A message indicates whether data is successfully written to InfluxDB.
  3. To add more data or correct line protocol, click Previous.
  4. Click Finish.

Scrape Metrics

To scrape metrics, see Create a scraper.

{{% cloud-msg %}}{{< cloud-name >}} does not support scrapers. {{% /cloud-msg %}}

influx CLI

From the command line, use the influx write command to write data to InfluxDB. Include the following in your command:

Requirement Include by
Organization Use the -o,--org, or --org-id flags.
Bucket Use the -b, --bucket, or --bucket-id flags.
Precision Use the -p, --precision flag.
Authentication token Set the INFLUX_TOKEN environment variable or use the t, --token flag.
Line protocol Write a single line as a string or pass a file path prefixed with @.
Example influx write commands

To write a single data point, for example, run

influx write -b bucketName -o orgName -p s 'myMeasurement,host=myHost testField="testData" 1556896326'

To write data in line protocol from a file, try

influx write -b bucketName -o orgName -p s @/path/to/line-protocol.txt

InfluxDB API

Write data to InfluxDB using an HTTP request to the InfluxDB API /write endpoint. Include the following in your request:

Requirement Include by
Organization Use the org query parameter in your request URL.
Bucket Use the bucket query parameter in your request URL.
Precision Use the precision query parameter in your request URL.
Authentication token Use the Authorization: Token header.
Line protocol Pass as plain text in your request body.
Example API write request

Below is an example API write request using curl. The URL depends on the version and location of your InfluxDB 2.0 instance.

{{< tabs-wrapper >}} {{% tabs %}} InfluxDB OSS {{< cloud-name "short">}} {{% /tabs %}} {{% tab-content %}}

curl -XPOST "http://localhost:9999/api/v2/write?org=YOUR_ORG&bucket=YOUR_BUCKET&precision=s" \
  --header "Authorization: Token YOURAUTHTOKEN" \
  --data-raw "mem,host=host1 used_percent=23.43234543 1556896326"

{{% /tab-content %}} {{% tab-content %}}

{{% cloud-msg %}} For the specific URL of your {{< cloud-name "short" >}} instance, see InfluxDB Cloud URLs. {{% /cloud-msg %}}

curl -XPOST "YOUR-INFLUXDB-CLOUD-URL/api/v2/write?org=YOUR_ORG&bucket=YOUR_BUCKET&precision=s" \
  --header "Authorization: Token YOURAUTHTOKEN" \
  --data-raw "mem,host=host1 used_percent=23.43234543 1556896326"

{{% /tab-content %}} {{< /tabs-wrapper >}}

Others

{{< children >}}

InfluxDB client libraries

Use language-specific client libraries to integrate with the InfluxDB v2 API. See Client libraries reference for more information.