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

╄→гoц情女王★ 提交于 2021-01-28 05:40:37

问题


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:


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.



来源:https://stackoverflow.com/questions/44165226/how-to-process-result-obtained-from-querying-influxdb-database-from-go-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!