Golang InfluxDB客户端响应转换为数组或切片。

huangapple go评论148阅读模式
英文:

golang influxdb client response to array or slice

问题

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

函数queryDB:
https://github.com/influxdata/influxdb/tree/master/client#querying-data
通过以下方式调用:

  1. 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")

当我执行

  1. fmt.Printf("%s", resp)

我得到类似以下的结果

  1. [{[{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地址组成的简单数组或切片?

英文:

I have a go program to query an Influxdb via Influxdb client.

Function queryDB:
https://github.com/influxdata/influxdb/tree/master/client#querying-data
is called via

  1. resp, err := queryDB(c, &quot;SELECT ip FROM events WHERE time &gt;= &#39;2016-10-24T00:00:00Z&#39; AND time &lt; &#39;2016-10-24T01:00:00Z&#39; ORDER BY time DESC&quot;)

when I do

  1. fmt.Printf(&quot;%s&quot;, resp)

I get something like

  1. [{[{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]]}] [] }]

How can I get a simple array or slice consisting of time and IP address?

答案1

得分: 2

好的,以下是翻译好的代码部分:

  1. var myData [][]interface{} = make([][]interface{}, len(resp[0].Series[0].Values))
  2. for i, d := range resp[0].Series[0].Values {
  3. myData[i] = d
  4. }
  5. fmt.Println("", myData[0]) // 切片中的第一个元素
  6. fmt.Println("", myData[0][0])
  7. fmt.Println("", myData[0][1])

输出结果:

  1. [2016-10-24T00:12:12.123456Z 192.168.123.107]
  2. 2016-10-24T00:12:12.123456Z
  3. 192.168.123.107
英文:

Ok, I got it:

  1. var myData [][]interface{} = make([][]interface{}, len(resp[0].Series[0].Values))
  2. for i, d := range resp[0].Series[0].Values {
  3. myData[i] = d
  4. }
  5. fmt.Println(&quot;&quot;, myData[0]) //first element in slice
  6. fmt.Println(&quot;&quot;, myData[0][0])
  7. fmt.Println(&quot;&quot;, myData[0][1])

output:

  1. [2016-10-24T00:12:12.123456Z 192.168.123.107]
  2. 2016-10-24T00:12:12.123456Z
  3. 192.168.123.107

huangapple
  • 本文由 发表于 2016年12月30日 17:03:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/41393446.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定