Java

Install the Java Development Kit(JDK)

Make sure that your system has JDK 8 or later installed. For more information on how to check your version of Java and install the JDK, see the Oracle Overview of JDK Installation documentation

Create the Project

This guide demonstrates how to add Java SDK dependencies using Maven. It is advisable to utilize an integrated development environment (IDE) like Intellij IDEA or Eclipse IDE for easier configuration of Maven in building and running your project.

If you are not using an IDE, see Building Maven for more information on how to set up your project.

Add GreptiemDB Java SDK as a Dependency

If you are using Maven, add the following to your pom.xml dependencies list:

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

The latest version can be viewed here.

After configuring your dependencies, make sure they are available to your project. This may require refreshing the project in your IDE or running the dependency manager.

Connect to the Database

Now, we can create a file to contain your application called QuickStart.java in the base package directory of you project. Use the following sample code to connect database, replacing the value of endpoints variable with your real connection string.

java

  1. package io.greptime.example;
  2. import io.greptime.GreptimeDB;
  3. import io.greptime.models.AuthInfo;
  4. import io.greptime.models.ColumnDataType;
  5. import io.greptime.models.Err;
  6. import io.greptime.models.QueryOk;
  7. import io.greptime.models.QueryRequest;
  8. import io.greptime.models.Result;
  9. import io.greptime.models.SelectExprType;
  10. import io.greptime.models.SelectRows;
  11. import io.greptime.models.SemanticType;
  12. import io.greptime.models.TableName;
  13. import io.greptime.models.TableSchema;
  14. import io.greptime.models.WriteOk;
  15. import io.greptime.models.WriteRows;
  16. import io.greptime.options.GreptimeOptions;
  17. import org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.concurrent.CompletableFuture;
  22. /**
  23. * @author jiachun.fjc
  24. */
  25. public class QuickStart {
  26. private static final Logger LOG = LoggerFactory.getLogger(QuickStart.class);
  27. public static void main(String[] args) throws Exception {
  28. String endpoint = "127.0.0.1:4001";
  29. AuthInfo authInfo = new AuthInfo("username", "password");
  30. GreptimeOptions opts = GreptimeOptions.newBuilder(endpoint) //
  31. .authInfo(authInfo) // By default, no authentication is required; you can remove this line.
  32. .writeMaxRetries(1) //
  33. .readMaxRetries(2) //
  34. .routeTableRefreshPeriodSeconds(-1) //
  35. .build();
  36. GreptimeDB greptimeDB = new GreptimeDB();
  37. if (!greptimeDB.init(opts)) {
  38. throw new RuntimeException("Fail to start GreptimeDB client");
  39. }
  40. }
  41. }

See Java SDK in reference to get more configurations and metrics.

Write Data

Please refer to write data.

Query Data

Please refer to Query data.