Java

使用下方的示例代码来查询数据:
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();
6. // For performance reasons, the SDK is designed to be purely asynchronous.
7. // The return value is a future object. If you want to immediately obtain
8. // the result, you can call `future.get()`.
9. CompletableFuture<Result<QueryOk, Err>> future = greptimeDB.query(request);
10. Result<QueryOk, Err> result = future.get();
12. if (result.isOk()) {
13. QueryOk queryOk = result.getOk();
14. SelectRows rows = queryOk.getRows();
15. // `collectToMaps` will discard type information, if type information is needed,
16. // please use `collect`.
17. List<Map<String, Object>> maps = rows.collectToMaps();
18. for (Map<String, Object> map : maps) {
19. LOG.info("Query row: {}", map);
20. }
21. } else {
22. LOG.error("Failed to query: {}", result.getErr());
23. }
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);
