Go

在进行查询之前,需要先构造 QueryRequest 对象,然后通过以下语言查询 GreptimeDB 中的数据:

  • SQL
  • PromQL (TODO)
  • RangePromQL

例如使用 SQL 查询:

go

1. func Query() {
2. // Query with metric via Sql, you can do it via PromQL
3. queryRequest := greptime.QueryRequest{}
4. // if you want to specify another database, you can specify it via: `WithDatabase(database)`
5. queryRequest.WithSql("SELECT * FROM monitor") // .WithDatabase(database)

7. resMetric, err := client.Query(context.Background(), queryRequest)
8. if err != nil {
9. fmt.Printf("fail to query, err: %+v\n", err)
10. return
11. }

13. type Monitor struct {
14. host   string
15. cpu    float64
16. memory int64
17. ts     time.Time
18. }

20. monitors := []Monitor{}
21. for _, series := range resMetric.GetSeries() {
22. monitor := Monitor{}
23. host, exist := series.Get("host") // you can directly call Get and do the type assertion
24. if exist {
25. monitor.host = host.(string)
26. }
27. monitor.cpu, _ = series.GetFloat("cpu")     // also, you can directly GetFloat
28. monitor.memory, _ = series.GetInt("memory") // also, you can directly GetInt
29. monitor.ts = series.GetTimestamp()          // GetTimestamp
30. monitors = append(monitors, monitor)
31. }
32. fmt.Println(monitors)
33. }