【问题标题】:golang influxdb client response to array or slicegolang influxdb 客户端对数组或切片的响应
【发布时间】:2016-12-30 09:03:00
【问题描述】:

我有一个通过 Influxdb 客户端查询 Influxdb 的 go 程序。

函数查询数据库: https://github.com/influxdata/influxdb/tree/master/client#querying-data 通过

调用
resp, err := queryDB(c, "SELECT ip FROM events WHERE time >= '2016-10-24T00:00:00Z' AND time < '2016-10-24T01:00:00Z' ORDER BY time DESC")

当我这样做时

fmt.Printf("%s", resp)

我得到了类似的东西

[{[{events map[] [time ip] [[2016-10-24T00:12:12.123456Z 192.168.123.107] /*...and so on...*/ [2016-10-24T00:24:24.123456Z 192.168.123.103]]}] [] }]

如何获得由时间和 IP 地址组成的简单数组或切片?

【问题讨论】:

  • 你能把完整的输出放在这里,一个小样本就可以了,但需要完整
  • 样本输出:[{[{events map[] [time distinct] [[1970-01-01T00:00:00Z 192.168.123.100] [1970-01-01T00:00:00Z 192.168.123.233] [1970-01-01T00:00:00Z 192.168.123.24] [1970-01-01T00:00:00Z 192.168.123.222] [1970-01-01T00:00:00Z 192.168.123.112]]}] [] }]
  • resp的结构是什么?
  • 函数 queryDB 返回 ´res []client.Result
  • 我认为它是这样的:'type Response struct { Results []Result Err error }' gowalker.org/github.com/influxdata/influxdb/client#Response wich has 'type Result struct { Series []models.Row Messages []*Message Err error } ´gowalker.org/github.com/influxdata/influxdb/client#Result

标签: go influxdb


【解决方案1】:

好的,我明白了:

var myData [][]interface{} = make([][]interface{}, len(resp[0].Series[0].Values))
            for i, d := range resp[0].Series[0].Values {
                myData[i] = d
            }

fmt.Println("", myData[0]) //first element in slice
fmt.Println("", myData[0][0])
fmt.Println("", myData[0][1])

输出:

[2016-10-24T00:12:12.123456Z 192.168.123.107]
2016-10-24T00:12:12.123456Z
192.168.123.107

【讨论】:

    猜你喜欢
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    • 2010-09-30
    • 2019-05-17
    • 1970-01-01
    相关资源
    最近更新 更多