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)
  6. resMetric, err := client.Query(context.Background(), queryRequest)
  7. if err != nil {
  8. fmt.Printf("fail to query, err: %+v\n", err)
  9. return
  10. }
  11. type Monitor struct {
  12. host string
  13. cpu float64
  14. memory int64
  15. ts time.Time
  16. }
  17. monitors := []Monitor{}
  18. for _, series := range resMetric.GetSeries() {
  19. monitor := Monitor{}
  20. host, exist := series.Get("host") // you can directly call Get and do the type assertion
  21. if exist {
  22. monitor.host = host.(string)
  23. }
  24. monitor.cpu, _ = series.GetFloat("cpu") // also, you can directly GetFloat
  25. monitor.memory, _ = series.GetInt("memory") // also, you can directly GetInt
  26. monitor.ts = series.GetTimestamp() // GetTimestamp
  27. monitors = append(monitors, monitor)
  28. }
  29. fmt.Println(monitors)
  30. }