How to process result obtained from querying influxdb database from go code

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

How to process result obtained from querying influxdb database from go code

问题

我正在使用以下方式从Go代码查询influxdb数据库。

q := fmt.Sprintf("select step,timeTaken from ZtpBoot where cvpVersion = 2017.1.1 group by step,numberOfDevices")
res, _ := queryDB(clnt, q)

在执行查询后,从influxdb中获取的结果如下所示:

name: ZtpBoot
tags: numberOfDevices=2, step=Step3.verifyZtpViaPost
time                step                   timeTaken
----                ----                   ---------
1495540747000000000 Step3.verifyZtpViaPost 0.108520030975
1495541643000000000 Step3.verifyZtpViaPost 0.115226984024

name: ZtpBoot
tags: numberOfDevices=2, step=Step4.verifyZtpViaHttp
time                step                   timeTaken
----                ----                   ---------
1495540747000000000 Step4.verifyZtpViaHttp 0.100101947784
1495541643000000000 Step4.verifyZtpViaHttp 0.103901863098

你想知道如何处理从res, _ := queryDB(clnt, q)获取的结果,以显示如表格所示的结果。

英文:

I am querying an inlfluxdb databse from a go code like this.

q := fmt.Sprintf("select step,timeTaken from ZtpBoot where cvpVersion = 
                  2017.1.1 group by step,numberOfDevices"
res, _ := queryDB(clnt, q)
Result when executing query in influxdb is like:-
name: ZtpBoot
tags: numberOfDevices=2, step=Step3.verifyZtpViaPost
time                step                   timeTaken
----                ----                   ---------
1495540747000000000 Step3.verifyZtpViaPost 0.108520030975
1495541643000000000 Step3.verifyZtpViaPost 0.115226984024

name: ZtpBoot
tags: numberOfDevices=2, step=Step4.verifyZtpViaHttp
time                step                   timeTaken
----                ----                   ---------
1495540747000000000 Step4.verifyZtpViaHttp 0.100101947784
1495541643000000000 Step4.verifyZtpViaHttp 0.103901863098

How can I process res obtained from res, _ := queryDB(clnt, q) to display the result as shown in table.

答案1

得分: 2

如何解析res驱动程序文档中,如果你仔细检查每个类型并了解如何在一般情况下使用Go类型,你就会知道。

正如你在评论中展示的那样,res的类型是[]client.Result,这意味着它是一个Result的切片。Result在文档中有定义。查看Result中的每个字段,并查找每个字段的类型。如果它们一下子对你来说不太明白,尝试阅读更多关于该类型在Go中的含义的内容。考虑进一步编写和运行一些使用该类型的代码,例如创建一个该类型的字面量并打印它,或者访问它的一部分,直到你习惯使用它并对它有更好的理解。

查找你不理解的文档的任何部分,比如Go中的符号[](切片/数组)和*(指针)的含义,或者如果你还不确定的话,如何在Go中使用切片和映射。语言规范是一个很好的资源。如果文档的某个部分不清楚,并且你对它的工作原理有更具体的问题,你可以在StackOverflow上发布一个问题。

希望这个示例足以让你入门。正如你所看到的,我使用不同类型的字面量初始化了一些变量,然后运行了你在评论中运行的相同的打印命令。注意它部分填充了你发布的结构。

package main

import(
    "fmt"
    "github.com/influxdata/influxdb/client/v2"
    "github.com/influxdata/influxdb/models"
)

func main() {
    tags := map[string]string{
        "numberOfDevices":"1",
        "step":"Step1.dhcpSetupTime",
    }
    cols := []string{"time", "step", "timeTaken"}
    row := models.Row{
        Name: "ZtpBoot",
        Tags: tags,
        Columns: cols,
        // add Values and Partial
    }
    rows := []models.Row{row}
    res := client.Result{
        Series: rows,
        // Add Messages and Err
    }
    fmt.Printf("Res: %v\nType: %T\n", res, res)
}

这个程序的输出是:

Res: {[{ZtpBoot map[numberOfDevices:1 step:Step1.dhcpSetupTime] [time step timeTaken] [] false}] [] }
Type: client.Result

希望这足以让你朝着正确的方向入门。如果你努力学习,并且看看我做了什么并阅读更多的Go文档,你可能能够填写剩下的部分,并理解如何访问这个数据结构的部分。

英文:

How to parse res is there in the driver documentation if you carefully examine each of the types and understand how to work with Go types well in general.

As you showed in your comment, res is type []client.Result, which means it's a slice of Results. Result is defined in the documentation. Look at each of the fields in Result, and look up each of their types. If they don't make sense to you right off, try to read more about what the type means in Go. Consider taking that a step further by writing and running some code that uses the type in some way -- create a literal of it and print it, or access a portion of it, for example -- until you get used to using it and have a better understanding of it.

Look up any parts of the documentation you don't understand, such as what symbols [] (slice/array) and * (pointer) mean in Go, or how slices and maps work in Go, if you're not already sure of those things. The language specification is a great resource. If a piece of the documentation isn't making sense and you have more specific questions about it works, you can post a question to StackOverflow about it.

I hope this example is enough to get you started. As you can see I initialize a few variables using literals of different types, and then run the same print command you ran in your comment. Notice that it partially fills in the structure you posted.

package main

import(
    "fmt"
    "github.com/influxdata/influxdb/client/v2"
    "github.com/influxdata/influxdb/models"
)

func main() {
    tags := map[string]string{
        "numberOfDevices":"1",
        "step":"Step1.dhcpSetupTime",
    }
    cols := []string{"time", "step", "timeTaken"}
    row := models.Row{
        Name: "ZtpBoot",
        Tags: tags,
        Columns: cols,
        // add Values and Partial
    }
    rows := []models.Row{row}
    res := client.Result{
        Series: rows,
        // Add Messages and Err
    }
    fmt.Printf("Res: %v\nType: %T\n", res, res)
}

The output from this program is:

Res: {[{ZtpBoot map[numberOfDevices:1 step:Step1.dhcpSetupTime] [time step timeTaken] [] false}] [] }
Type: client.Result

I hope that's enough to get you started in the right direction. If you work at it, and you look at what I did and read more Go documentation, you may be able to fill in the rest, and understand how to access portions of this data structure.

huangapple
  • 本文由 发表于 2017年5月25日 01:38:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/44165226.html
匿名

发表评论

匿名网友

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

确定