Documentation

Get started writing data

This tutorial walks you through the fundamental of creating line protocol data and writing it to InfluxDB.

InfluxDB provides many different options for ingesting or writing data, including the following:

  • Influx user interface (UI)
  • InfluxDB HTTP API (v1 and v2)
  • Telegraf
  • influx3 data CLI
  • InfluxDB client libraries
  • influx CLI

If using tools like Telegraf or InfluxDB client libraries, they can build the line protocol for you, but it’s good to understand how line protocol works.

Line protocol

All data written to InfluxDB is written using line protocol, a text-based format that lets you provide the necessary information to write a data point to InfluxDB. This tutorial covers the basics of line protocol, but for detailed information, see the Line protocol reference.

Line protocol elements

Each line of line protocol contains the following elements:

* Required
  • * measurement: String that identifies the measurement to store the data in.
  • tag set: Comma-delimited list of key value pairs, each representing a tag. Tag keys and values are unquoted strings. Spaces, commas, and equal characters must be escaped.
  • * field set: Comma-delimited list of key value pairs, each representing a field. Field keys are unquoted strings. Spaces and commas must be escaped. Field values can be strings (quoted), floats, integers, unsigned integers, or booleans.
  • timestamp: Unix timestamp associated with the data. InfluxDB supports up to nanosecond precision. If the precision of the timestamp is not in nanoseconds, you must specify the precision when writing the data to InfluxDB.

Line protocol element parsing

  • measurement: Everything before the first unescaped comma before the first whitespace.
  • tag set: Key-value pairs between the first unescaped comma and the first unescaped whitespace.
  • field set: Key-value pairs between the first and second unescaped whitespaces.
  • timestamp: Integer value after the second unescaped whitespace.
  • Lines are separated by the newline character (\n). Line protocol is whitespace sensitive.

myMeasurement,tag1=val1,tag2=val2 field1="v1",field2=1i 0000000000000000000


For schema design recommendations, see InfluxDB schema design.

Construct line protocol

With a basic understanding of line protocol, you can now construct line protocol and write data to InfluxDB. Consider a use case where you collect data from sensors in your home. Each sensor collects temperature, humidity, and carbon monoxide readings. To collect this data, use the following schema:

  • measurement: home
    • tags
      • room: Living Room or Kitchen
    • fields
      • temp: temperature in °C (float)
      • hum: percent humidity (float)
      • co: carbon monoxide in parts per million (integer)
    • timestamp: Unix timestamp in second precision

The following line protocol sample represents data collected hourly beginning at 2025-04-29T08:00:00Z (UTC) until 2025-04-29T20:00:00Z (UTC).

Home sensor data line protocol
home,room=Living\ Room temp=21.1,hum=35.9,co=0i 1719924000
home,room=Kitchen temp=21.0,hum=35.9,co=0i 1719924000
home,room=Living\ Room temp=21.4,hum=35.9,co=0i 1719927600
home,room=Kitchen temp=23.0,hum=36.2,co=0i 1719927600
home,room=Living\ Room temp=21.8,hum=36.0,co=0i 1719931200
home,room=Kitchen temp=22.7,hum=36.1,co=0i 1719931200
home,room=Living\ Room temp=22.2,hum=36.0,co=0i 1719934800
home,room=Kitchen temp=22.4,hum=36.0,co=0i 1719934800
home,room=Living\ Room temp=22.2,hum=35.9,co=0i 1719938400
home,room=Kitchen temp=22.5,hum=36.0,co=0i 1719938400
home,room=Living\ Room temp=22.4,hum=36.0,co=0i 1719942000
home,room=Kitchen temp=22.8,hum=36.5,co=1i 1719942000
home,room=Living\ Room temp=22.3,hum=36.1,co=0i 1719945600
home,room=Kitchen temp=22.8,hum=36.3,co=1i 1719945600
home,room=Living\ Room temp=22.3,hum=36.1,co=1i 1719949200
home,room=Kitchen temp=22.7,hum=36.2,co=3i 1719949200
home,room=Living\ Room temp=22.4,hum=36.0,co=4i 1719952800
home,room=Kitchen temp=22.4,hum=36.0,co=7i 1719952800
home,room=Living\ Room temp=22.6,hum=35.9,co=5i 1719956400
home,room=Kitchen temp=22.7,hum=36.0,co=9i 1719956400
home,room=Living\ Room temp=22.8,hum=36.2,co=9i 1719960000
home,room=Kitchen temp=23.3,hum=36.9,co=18i 1719960000
home,room=Living\ Room temp=22.5,hum=36.3,co=14i 1719963600
home,room=Kitchen temp=23.1,hum=36.6,co=22i 1719963600
home,room=Living\ Room temp=22.2,hum=36.4,co=17i 1719967200
home,room=Kitchen temp=22.7,hum=36.5,co=26i 1719967200
  • Copy
  • Fill window

Write line protocol to InfluxDB

The following examples show how to write the preceding sample data, already in line protocol format, to an InfluxDB Cloud Serverless bucket.

To learn more about available tools and options, see Write data.

Some examples in this getting started tutorial assume your InfluxDB credentials (URL, organization, and token) are provided by environment variables.

The tutorial assumes using Maven version 3.9 and Java version >= 15.

  1. If you haven’t already, follow the instructions to download and install the Java JDK and Maven for your system.

  2. In your terminal or editor, use Maven to generate a project–for example:

    mvn org.apache.maven.plugins:maven-archetype-plugin:3.1.2:generate \
    -DarchetypeArtifactId="maven-archetype-quickstart" \
    -DarchetypeGroupId="org.apache.maven.archetypes" -DarchetypeVersion="1.4" \
    -DgroupId="com.influxdbv3" -DartifactId="influxdb_java_client"
    -Dversion="1.0"
    
    • Copy
    • Fill window

    Maven creates the <artifactId> directory (./influxdb_java_client) that contains a pom.xml and scaffolding for your com.influxdbv3.influxdb_java_client Java application.

  3. In your terminal or editor, change into the ./influxdb_java_client directory–for example:

    cd ./influxdb_java_client
    
    • Copy
    • Fill window
  4. In your editor, open the pom.xml Maven configuration file and add the com.influxdb.influxdb3-java client library into dependencies.

    ...
    <dependencies>
      ...
      <dependency>
      <groupId>com.influxdb</groupId>
      <artifactId>influxdb3-java</artifactId>
      <version>0.1.0</version>
      </dependency>
      ...
    </dependencies>
    
    • Copy
    • Fill window
  5. To check your pom.xml for problems, run Maven’s validate command–for example, enter the following in your terminal:

    mvn validate
    
    • Copy
    • Fill window
  6. In your editor, navigate to the ./influxdb_java_client/src/main/java/com/influxdbv3 directory and create a Write.java file.

  7. In Write.java, enter the following sample code:

    // Write.java
    package com.influxdbv3;
    
    import java.util.List;
    import com.influxdb.v3.client.InfluxDBClient;
    import com.influxdb.v3.client.write.WriteOptions;
    import com.influxdb.v3.client.write.WritePrecision;
    
    /**
      * Writes line protocol to InfluxDB using the Java client
      * library.
      */
    public final class Write {
        /**
        * Write data to InfluxDB 3.
        */
        private Write() {
            //not called
        }
    
        /**
          * @throws Exception
          */
        public static void writeLineProtocol() throws Exception {
    
            // Set InfluxDB credentials
            final String host = "https://cloud2.influxdata.com";
            final String database = "get-started";
    
            /**
              * INFLUX_TOKEN is an environment variable you assigned to your
              * WRITE token value.
              */
            final char[] token = (System.getenv("INFLUX_TOKEN")).
            toCharArray();
    
            // Instantiate the InfluxDB client.
            try (InfluxDBClient client = InfluxDBClient.getInstance(host,
            token, database)) {
                // Create a list of line protocol records.
                final List<String> records = List.of(
                  "home,room=Living\\ Room temp=21.1,hum=35.9,co=0i 1719924000",
                  "home,room=Kitchen temp=21.0,hum=35.9,co=0i 1719924000",
                  "home,room=Living\\ Room temp=21.4,hum=35.9,co=0i 1719927600",
                  "home,room=Kitchen temp=23.0,hum=36.2,co=0i 1719927600",
                  "home,room=Living\\ Room temp=21.8,hum=36.0,co=0i 1719931200",
                  "home,room=Kitchen temp=22.7,hum=36.1,co=0i 1719931200",
                  "home,room=Living\\ Room temp=22.2,hum=36.0,co=0i 1719934800",
                  "home,room=Kitchen temp=22.4,hum=36.0,co=0i 1719934800",
                  "home,room=Living\\ Room temp=22.2,hum=35.9,co=0i 1719938400",
                  "home,room=Kitchen temp=22.5,hum=36.0,co=0i 1719938400",
                  "home,room=Living\\ Room temp=22.4,hum=36.0,co=0i 1719942000",
                  "home,room=Kitchen temp=22.8,hum=36.5,co=1i 1719942000",
                  "home,room=Living\\ Room temp=22.3,hum=36.1,co=0i 1719945600",
                  "home,room=Kitchen temp=22.8,hum=36.3,co=1i 1719945600",
                  "home,room=Living\\ Room temp=22.3,hum=36.1,co=1i 1719949200",
                  "home,room=Kitchen temp=22.7,hum=36.2,co=3i 1719949200",
                  "home,room=Living\\ Room temp=22.4,hum=36.0,co=4i 1719952800",
                  "home,room=Kitchen temp=22.4,hum=36.0,co=7i 1719952800",
                  "home,room=Living\\ Room temp=22.6,hum=35.9,co=5i 1719956400",
                  "home,room=Kitchen temp=22.7,hum=36.0,co=9i 1719956400",
                  "home,room=Living\\ Room temp=22.8,hum=36.2,co=9i 1719960000",
                  "home,room=Kitchen temp=23.3,hum=36.9,co=18i 1719960000",
                  "home,room=Living\\ Room temp=22.5,hum=36.3,co=14i 1719963600",
                  "home,room=Kitchen temp=23.1,hum=36.6,co=22i 1719963600",
                  "home,room=Living\\ Room temp=22.2,hum=36.4,co=17i 1719967200",
                  "home,room=Kitchen temp=22.7,hum=36.5,co=26i 1719967200"
                );
    
                /**
                 * Write each record separately to InfluxDB with timestamp
                 * precision in seconds.
                 * If no error occurs, print a success message.
                 * */
                for (String record : records) {
                    client.writeRecord(record, new WriteOptions(null, null,
                    WritePrecision.S));
                    System.out.printf("Data has been written successfully:
                    %s%n", record);
                }
            }
        }
    }
    
    • Copy
    • Fill window

    The sample code does the following:

    1. Imports the following classes:

      • java.util.List;
      • com.influxdb.v3.client.InfluxDBClient
      • com.influxdb.v3.client.write.WriteParameters
      • com.influxdb.v3.client.write.WritePrecision
    2. Calls InfluxDBClient.getInstance() to instantiate a client configured with InfluxDB credentials.

      • host: your InfluxDB Cloud Serverless region URL
      • database: the name of the InfluxDB Cloud Serverless bucket to write to
      • token: a token with write access to the specified bucket. Store this in a secret store or environment variable to avoid exposing the raw token string.
    3. Defines a list of line protocol strings where each string represents a data record.

    4. Calls the client’s writeRecord() method to write each record separately to InfluxDB.

      Because the timestamps in the sample line protocol are in second precision, the example passes the WritePrecision.S enum value as the precision argument to set the write timestamp precision to seconds.

  8. In your editor, open the App.java file (created by Maven) and replace its contents with the following sample code:

    // App.java
    
    package com.influxdbv3;
    
    /**
    * Execute the client functions.
    *
    */
    public class App {
    
        /**
        * @param args
        * @throws Exception
        */
        public static void main(final String[] args) throws Exception {
            // Write data to InfluxDB 3.
            Write.writeLineProtocol();
        }
    }
    
    • Copy
    • Fill window
    • The App class and Write class are part of the same com.influxdbv3 package (your project groupId).
    • App defines a main() function that calls Write.writeLineProtocol().
  9. In your terminal or editor, use Maven to install dependencies and compile the project code–for example:

    mvn compile
    
    • Copy
    • Fill window
  10. In your terminal or editor, execute App.main() to write to InfluxDB–for example, using Maven:

    mvn exec:java -Dexec.mainClass="com.influxdbv3.App"
    
    • Copy
    • Fill window

If successful, the output is the success message; otherwise, error details and the failure message.

View the written data

Congratulations! You have written data to InfluxDB. With data now stored in InfluxDB, let’s query it.


Was this page helpful?

Thank you for your feedback!


The future of Flux

Flux is going into maintenance mode. You can continue using it as you currently are without any changes to your code.

Read more

Now Generally Available

InfluxDB 3 Core and Enterprise

Start fast. Scale faster.

Get the Updates

InfluxDB 3 Core is an open source, high-speed, recent-data engine that collects and processes data in real-time and persists it to local disk or object storage. InfluxDB 3 Enterprise builds on Core’s foundation, adding high availability, read replicas, enhanced security, and data compaction for faster queries and optimized storage. A free tier of InfluxDB 3 Enterprise is available for non-commercial at-home or hobbyist use.

For more information, check out:

InfluxDB Cloud Serverless