Java
使用下方的示例代码来查询数据:
java
QueryRequest request = QueryRequest.newBuilder()
.exprType(SelectExprType.Sql) // Currently, only SQL is supported, and more query methods will be supported in the future
.ql("SELECT * FROM monitor;")
.build();
// For performance reasons, the SDK is designed to be purely asynchronous.
// The return value is a future object. If you want to immediately obtain
// the result, you can call `future.get()`.
CompletableFuture<Result<QueryOk, Err>> future = greptimeDB.query(request);
Result<QueryOk, Err> result = future.get();
if (result.isOk()) {
QueryOk queryOk = result.getOk();
SelectRows rows = queryOk.getRows();
// `collectToMaps` will discard type information, if type information is needed,
// please use `collect`.
List<Map<String, Object>> maps = rows.collectToMaps();
for (Map<String, Object> map : maps) {
LOG.info("Query row: {}", map);
}
} else {
LOG.error("Failed to query: {}", result.getErr());
}
The code will output the details of querying which will look something like this:
[main] INFO QuickStart:114 - Query row: {memory=null, host=127.0.0.1, cpu=0.1, ts=2023-03-16T07:20:26.043}
[main] INFO QuickStart:114 - Query row: {memory=null, host=127.0.0.1, cpu=0.1, ts=2023-03-16T07:21:48.050}
[main] INFO QuickStart:114 - Query row: {memory=null, host=127.0.0.1, cpu=0.1, ts=2023-03-16T07:29:47.780}
[main] INFO QuickStart:114 - Query row: {memory=0.5, host=127.0.0.2, cpu=0.3, ts=2023-03-16T07:20:26.096}
[main] INFO QuickStart:114 - Query row: {memory=0.5, host=127.0.0.2, cpu=0.3, ts=2023-03-16T07:21:48.103}
[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
/**
* According to the conditions, query data from the DB.
*
* @param req the query request
* @param ctx invoke context
* @return query result
*/
CompletableFuture<Result<QueryOk, Err>> query(QueryRequest req, Context ctx);
当前内容版权归 GreptimeDB 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 GreptimeDB .