docs-v2/content/influxdb/cloud-serverless/reference/client-libraries/v3/java.md

3.7 KiB

title list_title description external_url menu influxdb/cloud-serverless/tags weight aliases
Java client library for InfluxDB v3 Java The InfluxDB v3 `influxdb3-java` Java client library integrates with application code to write and query data stored in an InfluxDB Cloud Serverless bucket. https://github.com/InfluxCommunity/influxdb3-java
influxdb_cloud_serverless
name parent identifier
Java v3 client libraries influxdb3-java
Flight client
Java
gRPC
SQL
Flight SQL
client libraries
201
/cloud-serverless/query-data/sql/execute-queries/java/

The InfluxDB v3 influxdb3-java Java client library integrates with Java application code to write and query data stored in an {{% product-name %}} bucket.

The documentation for this client library is available on GitHub.

InfluxDB v3 Java client library

Installation

Maven

<dependency>
  <groupId>com.influxdb</groupId>
  <artifactId>influxdb3-java</artifactId>
  <version>RELEASE</version>
</dependency>

Gradle

implementation group: 'com.influxdb', name: 'influxdb3-java', version: 'latest.release'

Sample Code

public class HelloInfluxDB {
  private static final String HOST_URL = "https://eu-central-1-1.aws.cloud2.influxdata.com"; // URL information is present in the cloud portal
  private static final String DATABASE = "java"; // Database name is the bucket name present in the cloud portal
  private static final char[] API_TOKEN = "API_TOKEN".toCharArray(); // Avoid hard-coding API_TOKEN in production. It is present in the cloud portal.

  // Authenticate, Write and Query data from the serverless InfluxDB
  public static void main(String[] args) {
    try (InfluxDBClient client = InfluxDBClient.getInstance(HOST_URL, API_TOKEN, DATABASE)) {
      writeData(client);
      queryData(client);
    }
    catch (Exception e) {
      System.err.println("An error occurred while connecting with the serverless InfluxDB!");
      e.printStackTrace();
    }
  }

  // Write sample measurement using Point class API
  private static void writeData(InfluxDBClient client) {
    Point point = Point.measurement("temperature")
                       .setTag("location", "London")
                       .setField("value", 30.01)
                       .setTimestamp(Instant.now().minusSeconds(10));
    try {
      client.writePoint(point);
      System.out.println("Data written to the bucket.");
    }
    catch (Exception e) {
      System.err.println("Failed to write data to the bucket.");
      e.printStackTrace();
    }
  }

  // Query the latest 10 measurements using SQL
  private static void queryData(InfluxDBClient client) {
    System.out.printf("--------------------------------------------------------%n");
    System.out.printf("| %-8s | %-8s | %-30s |%n", "location", "value", "time");
    System.out.printf("--------------------------------------------------------%n");

    String sql = "select time,location,value from temperature order by time desc limit 10";
    try (Stream<Object[]> stream = client.query(sql)) {
      stream.forEach(row -> System.out.printf("| %-8s | %-8s | %-30s |%n", row[1], row[2], row[0]));
    }
    catch (Exception e) {
      System.err.println("Failed to query data from the bucket.");
      e.printStackTrace();
    }
  }
}

Running the program

Build the project and then run the executable .jar file with this JVM Flag.

Example:

  java --add-opens=java.base/java.nio=org.apache.arrow.memory.core,ALL-UNNAMED -jar target/{your-jar-file}.jar