Java SDK

The GreptimeDB Java SDK uses gRPC to communicate with the database. For more information on how to use the SDK, please refer to the Java SDK documentation.

To connect to GreptimeCloud, using information below:

  • Host: <host>
  • Port: 4001
  • Database: <dbname>
  • Username: <username>
  • Password: <password>

The following code snippet shows how to create a client.

java

  1. String endpoint = "<host>:4001";
  2. AuthInfo authInfo = new AuthInfo("<username>", "<password>");
  3. GreptimeOptions opts = GreptimeOptions.newBuilder(endpoint) //
  4. .authInfo(authInfo)
  5. .writeMaxRetries(1) //
  6. .readMaxRetries(2) //
  7. .routeTableRefreshPeriodSeconds(-1) //
  8. .build();
  9. GreptimeDB greptimeDB = new GreptimeDB();
  10. if (!greptimeDB.init(opts)) {
  11. throw new RuntimeException("Fail to start GreptimeDB client");
  12. }

After creating a GreptimeDB client, you can set database while writing data or querying data. For example query data:

java

  1. QueryRequest request = QueryRequest.newBuilder() //
  2. .exprType(SelectExprType.Sql) //
  3. .ql("SELECT * FROM monitor;") //
  4. .databaseName("<dbname>") //
  5. .build();