Java client library for InfluxDB 3
The InfluxDB 3 influxdb3-java Java client library integrates
with Java application code to write and query data stored in InfluxDB 3 Enterprise.
InfluxDB client libraries provide configurable batch writing of data to InfluxDB 3 Enterprise. Use client libraries to construct line protocol data, transform data from other formats to line protocol, and batch write line protocol data to InfluxDB HTTP APIs.
InfluxDB 3 client libraries can query InfluxDB 3 Enterprise using SQL or InfluxQL.
The influxdb3-java Java client library wraps the Apache Arrow org.apache.arrow.flight.FlightClient
in a convenient InfluxDB 3 interface for executing SQL and InfluxQL queries, requesting
server metadata, and retrieving data from InfluxDB 3 Enterprise using the Flight protocol with gRPC.
Example: write and query data
The following example shows how to use influxdb3-java to write and query data stored in InfluxDB 3 Enterprise.
package com.influxdata.demo;
import com.influxdb.v3.client.InfluxDBClient;
import com.influxdb.v3.client.Point;
import com.influxdb.v3.client.query.QueryOptions;
import com.influxdb.v3.client.query.QueryType;
import java.time.Instant;
import java.util.stream.Stream;
public class HelloInfluxDB {
private static final String HOST_URL = "https://localhost:8181"; // your cluster URL
private static final String DATABASE = "DATABASE_NAME"; // your InfluxDB database name
private static final char[] TOKEN = System.getenv("DATABASE_TOKEN"); // a local environment variable that stores your database token
// Create a client instance that writes and queries data in your database.
public static void main(String[] args) {
// Instantiate the client with your InfluxDB credentials
try (InfluxDBClient client = InfluxDBClient.getInstance(HOST_URL, TOKEN, DATABASE)) {
writeData(client);
queryData(client);
}
catch (Exception e) {
System.err.println("An error occurred while connecting to InfluxDB!");
e.printStackTrace();
}
}
// Use the Point class to construct time series data.
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 is written to the database.");
}
catch (Exception e) {
System.err.println("Failed to write data to the database.");
e.printStackTrace();
}
}
// Use SQL to query the most recent 10 measurements
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 database.");
e.printStackTrace();
}
}
}Source: suyashcjoshi/SimpleJavaInfluxDB on GitHub
Replace the following:
DATABASE_NAME: the name of your InfluxDB 3 Enterprise database to read and write data toDATABASE_TOKEN: a local environment variable that stores your token–the token must have read and write permissions on the specified database.
Run the example to write and query data
Build an executable JAR for the project–for example, using Maven:
mvn packageIn your terminal, run the
javacommand to write and query data in your database:java \ --add-opens=java.base/java.nio=org.apache.arrow.memory.core,ALL-UNNAMED \ -jar target/PROJECT_NAME.jarInclude the following in your command:
--add-opens=java.base/java.nio=org.apache.arrow.memory.core,ALL-UNNAMED: with Java version 9 or later and Apache Arrow version 16 or later, exposes JDK internals for Arrow. For more options, see the Apache Arrow Java install documentation.-jar target/PROJECT_NAME.jar: your.jarfile to run.
The output is the newly written data from your InfluxDB 3 Enterprise database.
Installation
Include com.influxdb.influxdb3-java in your project dependencies.
<dependency>