Java 客户端库,适用于 InfluxDB 3
InfluxDB 3 influxdb3-java
Java 客户端库 与 Java 应用程序代码集成,用于写入和查询存储在 InfluxDB 3 Core 中的数据。
InfluxDB 客户端库提供可配置的批量写入数据到 InfluxDB 3 Core。使用客户端库来构建 Line Protocol 数据,将其他格式的数据转换为 Line Protocol,并将 Line Protocol 数据批量写入 InfluxDB HTTP API。
InfluxDB 3 客户端库可以使用 SQL 或 InfluxQL 查询 InfluxDB 3 Core。influxdb3-java
Java 客户端库将 Apache Arrow org.apache.arrow.flight.FlightClient
封装在一个方便的 InfluxDB 3 接口中,用于执行 SQL 和 InfluxQL 查询,请求服务器元数据,并使用 Flight 协议和 gRPC 从 InfluxDB 3 Core 检索数据。
示例:写入和查询数据
以下示例展示了如何使用 influxdb3-java
写入和查询存储在 InfluxDB 3 Core 中的数据。
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();
}
}
}
来源:suyashcjoshi/SimpleJavaInfluxDB on GitHub
替换以下内容
DATABASE_NAME
:你的 InfluxDB 3 Core 数据库 名称,用于读取和写入数据DATABASE_TOKEN
:一个本地环境变量,用于存储你的 token——该 token 必须具有对指定数据库的读取和写入权限。
运行示例以写入和查询数据
为项目构建可执行 JAR 文件——例如,使用 Maven
mvn package
在你的终端中,运行
java
命令以在你的数据库中写入和查询数据java \ --add-opens=java.base/java.nio=org.apache.arrow.memory.core,ALL-UNNAMED \ -jar target/PROJECT_NAME.jar
在你的命令中包含以下内容
--add-opens=java.base/java.nio=org.apache.arrow.memory.core,ALL-UNNAMED
:对于 Java 9 或更高版本以及 Apache Arrow 16 或更高版本,公开用于 Arrow 的 JDK 内部组件。有关更多选项,请参阅 Apache Arrow Java 安装文档。-jar target/PROJECT_NAME.jar
:你要运行的.jar
文件。
输出是你从 InfluxDB 3 Core 数据库新写入的数据。
安装
在你的项目依赖项中包含 com.influxdb.influxdb3-java
。
<dependency>
<groupId>com.influxdb</groupId>
<artifactId>influxdb3-java</artifactId>
<version>RELEASE</version>
</dependency>
dependencies {
implementation group: 'com.influxdb', name: 'influxdb3-java', version: 'latest.release'
}
导入客户端
influxdb3-java
客户端库包提供 com.influxdb.v3.client
类,用于构建、写入和查询存储在 InfluxDB 3 Core 中的数据。
API 参考
InfluxDBClient 接口
InfluxDBClient
提供了一个与 InfluxDB API 交互以进行数据写入和查询的接口。
InfluxDBClient.getInstance
构造函数初始化并返回一个客户端实例,其中包含以下内容
- 一个写入客户端,配置用于写入数据库。
- 一个 Arrow Flight 客户端,配置用于查询数据库。
要初始化客户端,请调用 getInstance
并将你的凭据作为以下类型之一传递
- 参数
- 一个
ClientConfig
- 一个 数据库连接字符串
使用凭据参数初始化
static InfluxDBClient getInstance(@Nonnull final String host,
@Nullable final char[] token,
@Nullable final String database)
示例:使用凭据参数初始化
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";
private static final String DATABASE = "DATABASE_NAME";
private static final char[] TOKEN = System.getenv("DATABASE_TOKEN");
// Create a client instance, and then write and query data in InfluxDB.
public static void main(String[] args) {
try (InfluxDBClient client = InfluxDBClient.getInstance(HOST_URL, DATABASE_TOKEN, DATABASE)) {
writeData(client);
queryData(client);
}
catch (Exception e) {
System.err.println("An error occurred while connecting to InfluxDB!");
e.printStackTrace();
}
}
}
替换以下内容
默认标签
要在所有写入数据中包含默认标签,请传递一个标签键和值的 Map
。
InfluxDBClient getInstance(@Nonnull final String host,
@Nullable final char[] token,
@Nullable final String database,
@Nullable Map<String, String> defaultTags)
使用数据库连接字符串初始化
"https://localhost:8181"
+ "?token=DATABASE_TOKEN&database=DATABASE_NAME"
替换以下内容
InfluxDBClient 实例方法
InfluxDBClient.writePoint
要将点作为 Line Protocol 写入数据库
- 初始化
client
——你的 token 必须具有对指定数据库的写入权限。 - 使用
com.influxdb.v3.client.Point
类来创建时间序列数据。 - 调用
client.writePoint()
方法以将点作为 Line Protocol 写入你的数据库。
// Use the Point class to construct time series data.
// Call client.writePoint to write the point in your database.
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 database.");
}
catch (Exception e) {
System.err.println("Failed to write data to the database.");
e.printStackTrace();
}
}
InfluxDBClient.query
要查询数据并处理结果
- 初始化
client
——token 必须具有对你要查询的数据库的读取权限。 - 调用
client.query()
并提供你的 SQL 查询作为字符串。 - 使用结果流的内置迭代器来处理行数据。
// 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 database.");
e.printStackTrace();
}
}
此页面是否对您有帮助?
感谢您的反馈!
支持和反馈
感谢您成为我们社区的一份子!我们欢迎并鼓励您提供关于 InfluxDB 3 Core 和本文档的反馈和错误报告。要获得支持,请使用以下资源
拥有年度合同或支持合同的客户 可以联系 InfluxData 支持。