English | 简体中文
opengemini-client-java
是一个用 Java 语言编写的 OpenGemini 客户端
OpenGemini 是一款云原生分布式时序数据库。获取更多信息,请点击这里
- 编译本项目至少需要OpenJDK 17, Maven 3.8.0或更高版本
使用常见的maven构建语法即可
mvn install -Dmaven.test.skip=true
若要使用mvn test
请先本地运行一个opengemini的server,推荐使用官方容器镜像版本,如:
docker run -p 8086:8086 --name opengemini --rm opengeminidb/opengemini-server
<dependency>
<groupId>io.opengemini</groupId>
<artifactId>opengemini-client</artifactId>
<version>${latest.version}</version>
</dependency>
默认情况下,客户端使用 JDK 自带的 HTTP 引擎,并根据 Java 版本自动选择合适的实现,支持 Java 8 和 Java 11+。如果有需要,你可以在 HttpClientConfig
中通过 .engine
选项配置不同的 HTTP 引擎。请注意,若选择不同的引擎,则需要手动添加相应的依赖。
package org.example;
import io.github.openfacade.http.HttpClientConfig;
import io.opengemini.client.api.Address;
import io.opengemini.client.api.Configuration;
import io.opengemini.client.api.OpenGeminiException;
import io.opengemini.client.api.Point;
import io.opengemini.client.api.Query;
import io.opengemini.client.api.QueryResult;
import io.opengemini.client.impl.OpenGeminiClient;
import io.opengemini.client.impl.OpenGeminiClientFactory;
import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class Main {
public static void main(String[] args) throws ExecutionException, InterruptedException, OpenGeminiException {
HttpClientConfig httpConfig = new HttpClientConfig.Builder()
.connectTimeout(Duration.ofSeconds(3))
.timeout(Duration.ofSeconds(3))
.build();
Configuration configuration = Configuration.builder()
.addresses(Collections.singletonList(new Address("127.0.0.1", 8086)))
.httpConfig(httpConfig)
.build();
OpenGeminiClient client = OpenGeminiClientFactory.create(configuration);
String databaseName = "db_quick_start";
CompletableFuture<Void> createdb = client.createDatabase(databaseName);
createdb.get();
Point point = new Point();
point.setMeasurement("ms_quick_start");
HashMap<String, String> tags = new HashMap<>();
HashMap<String, Object> fields = new HashMap<>();
tags.put("tag1", "tag value1");
fields.put("field1", "field value1");
point.setTags(tags);
point.setFields(fields);
client.write(databaseName, point).get();
// Creating a new tag requires waiting for the server to create and update indexes
Thread.sleep(3000);
Query selectQuery = new Query("select * from " + "ms_quick_start", databaseName, "");
CompletableFuture<QueryResult> queryRst = client.query(selectQuery);
System.out.println("query result: " + queryRst.get());
}
}
欢迎加入我们