Java

安装 Java Development Kit(JDK)

请确认你的系统已经安装了 JDK 8 或者更高版本。更多关于如何检查你的 Java 版本和安装 JDK 的信息,请参考 Oracle Overview of JDK Installation documentation

创建项目

该指南演示了如何使用 Maven 添加 Java SDK 依赖项。建议使用集成开发环境(IDE)如 Intellij IDEA 或 Eclipse IDE 来更容易的配置 Maven 以构建和运行你的项目。 如果你没有在使用 IDE,请参考 Building Maven 来了解更多关于如何设置你的项目的信息。

添加 GreptiemDB Java SDK 作为依赖

如果你正在使用 Maven,请将以下内容添加到 pom.xml 的依赖项列表中:

1. <dependencies>
2. <dependency>
3. <groupId>io.greptime</groupId>
4. <artifactId>greptimedb-all</artifactId>
5. <version>${latest_version}</version>
6. </dependency>
7. </dependencies>

可以到这里查看最新的版本。

在配置完依赖后,请确保它们可以被你的项目使用,这可能需要在你的 IDE 中刷新项目或者运行依赖管理器。

连接数据库

现在创建一个文件 QuickStart.java 放在你的项目的基础包目录下。使用下面的示例代码来连接数据库,将 endpoints 变量的值替换为你实际的连接地址。

java

1. package io.greptime.example;

3. import io.greptime.GreptimeDB;
4. import io.greptime.models.AuthInfo;
5. import io.greptime.models.ColumnDataType;
6. import io.greptime.models.Err;
7. import io.greptime.models.QueryOk;
8. import io.greptime.models.QueryRequest;
9. import io.greptime.models.Result;
10. import io.greptime.models.SelectExprType;
11. import io.greptime.models.SelectRows;
12. import io.greptime.models.SemanticType;
13. import io.greptime.models.TableName;
14. import io.greptime.models.TableSchema;
15. import io.greptime.models.WriteOk;
16. import io.greptime.models.WriteRows;
17. import io.greptime.options.GreptimeOptions;
18. import org.slf4j.Logger;
19. import org.slf4j.LoggerFactory;
20. import java.util.List;
21. import java.util.Map;
22. import java.util.concurrent.CompletableFuture;

24. /**
25. * @author jiachun.fjc
26. */
27. public class QuickStart {

29. private static final Logger LOG = LoggerFactory.getLogger(QuickStart.class);

31. public static void main(String[] args) throws Exception {
32. String endpoint = "127.0.0.1:4001";
33. AuthInfo authInfo = new AuthInfo("username", "password");
34. GreptimeOptions opts = GreptimeOptions.newBuilder(endpoint) //
35. .authInfo(authInfo) // By default, no authentication is required; you can remove this line.
36. .writeMaxRetries(1) //
37. .readMaxRetries(2) //
38. .routeTableRefreshPeriodSeconds(-1) //
39. .build();

41. GreptimeDB greptimeDB = new GreptimeDB();

43. if (!greptimeDB.init(opts)) {
44. throw new RuntimeException("Fail to start GreptimeDB client");
45. }
46. }
47. }

请前往 Java SDK in reference 获得更多的配置和指标信息。

写入数据

请参考 写入数据.

读取数据

请参考 读取数据.