Java

Data Query Process

使用下方的示例代码来查询数据:

java

  1. QueryRequest request = QueryRequest.newBuilder()
  2. .exprType(SelectExprType.Sql) // It also supports PromQL query language (SelectExprType.Promql)
  3. .ql("SELECT * FROM monitor;")
  4. .build();
  5. // For performance reasons, the SDK is designed to be purely asynchronous.
  6. // The return value is a future object. If you want to immediately obtain
  7. // the result, you can call `future.get()`.
  8. CompletableFuture<Result<QueryOk, Err>> future = greptimeDB.query(request);
  9. Result<QueryOk, Err> result = future.get();
  10. if (result.isOk()) {
  11. QueryOk queryOk = result.getOk();
  12. SelectRows rows = queryOk.getRows();
  13. // `collectToMaps` will discard type information, if type information is needed,
  14. // please use `collect`.
  15. List<Map<String, Object>> maps = rows.collectToMaps();
  16. for (Map<String, Object> map : maps) {
  17. LOG.info("Query row: {}", map);
  18. }
  19. } else {
  20. LOG.error("Failed to query: {}", result.getErr());
  21. }

The code will output the details of querying which will look something like this:

  1. [main] INFO QuickStart:114 - Query row: {memory=null, host=127.0.0.1, cpu=0.1, ts=2023-03-16T07:20:26.043}
  2. [main] INFO QuickStart:114 - Query row: {memory=null, host=127.0.0.1, cpu=0.1, ts=2023-03-16T07:21:48.050}
  3. [main] INFO QuickStart:114 - Query row: {memory=null, host=127.0.0.1, cpu=0.1, ts=2023-03-16T07:29:47.780}
  4. [main] INFO QuickStart:114 - Query row: {memory=0.5, host=127.0.0.2, cpu=0.3, ts=2023-03-16T07:20:26.096}
  5. [main] INFO QuickStart:114 - Query row: {memory=0.5, host=127.0.0.2, cpu=0.3, ts=2023-03-16T07:21:48.103}
  6. [main] INFO QuickStart:114 - Query row: {memory=0.5, host=127.0.0.2, cpu=0.3, ts=2023-03-16T07:29:47.882}

查询 API

java

  1. /**
  2. * According to the conditions, query data from the DB.
  3. *
  4. * @param req the query request
  5. * @param ctx invoke context
  6. * @return query result
  7. */
  8. CompletableFuture<Result<QueryOk, Err>> query(QueryRequest req, Context ctx);