Example code for InfluxDB Client sample

Their is a client sample code on InfluxDB github site that makes reference to a test client.

I have been doing code in C on and off for 20+ years and php/perl 20+ years also but I have only been using Go Language for about a week or 2 now. Still trying to get upto speed with Go. This might be very basic question.

Anyways, if someone could point me to an example of using that go client. Simple code example of open a connection to influxdb. Selecting the results of several columns ( of different types!!! ) and then dumping that select into an array. String/int/float array or into single arrays of each of the columns.

I am having hell with getting data back out of influxdb!

From the example on InfluxDB Go page using their example and having the select pull just one column that is float64 and mod the code to below

val := row[1].(float64)

I get error

panic: interface conversion: interface {} is json.Number, not float64

goroutine 1 [running]:
main.main()
/tmp/influx-test.go:83 +0x69d
exit status 2

I do not understand what json.Number is or how to convert that over to float64 which is how the data it stored in influxDB… If someone knows of a easier way to just use Influx Go Client… I have looked for 1/2 of the day and I can not find anyone who has posted example code on using the Go Client for InfluxDB…where they walk through simple examples getting the results out of influx and into arrays in GO.

Hello. I’m the developer of that library.

I think you found a bug/usability problem in the library. Thank you! The problem is that, at the moment, the server returns a JSON blob. Unfortunately, JSON has no way of separating floats from integers. So the json.Number that you see is a Go string that was a number in JSON.

I’ll work on fixing this and then inform you when I’m done.

1 Like

Thank you! I could not figure out what I am doing wrong.

Hm, interesting. So json.Number comes from the encoding/json library, but only if it’s explicitly turned on. I thought I had turned it on at some point, but I’m not seeing it on the master branch of what’s currently up. Are you using the master branch or a different one?

This brings up a bit of a problem with our current output and one we’re looking to change in 1.4. Since JSON has no way to differentiate between floats and integers, you have to essentially guess which one was actually returned by the server. This change here (https://github.com/influxdata/influxdb/pull/7154) exists to offer another method of returning the data. While the JSON method will still exist, the client will begin using that.

I had one pending change to switch to using json.Number on the cli branch (which is where I’m guessing you got this), but it isn’t merged and I don’t think I’m going to merge it knowing this situation. I would advise going back to master and doing this if you need to access integers.

val := int64(row[i].(float64))

When we finally get the PR I mentioned above sorted out, you will be able to update the library and do a type assertion directly to an integer. If you are always using floats, you won’t have to change anything.

I hope that helps. If you need any more help, feel free to ask.

I am using “github.com/influxdata/influxdb/client/v2” branch… I am guessing this is master branch?

7154 looks like what I am after. Is their any example code out their showing how to use this?

What I need to to pull the float64 that is in influxdb and put that float64 value into an array of float64 not convert it to int64.

val := float64(row[i].(float64)) I assume is what I need??

is their a simple way if you map the select output so you can do mystuff[i] = row and make the map from influxdb

var mystuff [][]interface{} = make([][]interface{},len(res[0].Series[0].Values))

so mystuff has the same struct that influxed has ?? col1 = time, col2 = float64, col3 = int64, col3 = boolean etc…

Is that possible todo some how?? Or I just haft to do all conversions in the for loop to convert everything for each column???

I’m not sure which one you’re using. The first post you made indicates you’re using the experimental client. If you’re using client/v2, that’s completely different and makes a lot more sense.

The PR that I referenced hasn’t been merged and the client I am referencing isn’t official at the moment. It’s something I’ve mostly been working on in my downtime to change the default client into something more nimble. Since the PR isn’t merged yet and no client uses it, it looks like you’ll be getting back a json.Number. That means that instead of getting back a float64, you’ll get back json.Number. So you can do this based on your example:

val, err := row[1].(json.Number).Float64()

You can either handle the potential error or just ignore it if you’re feeling confident there will never be an error.

Well when I do that I get an error…

 76         //var myData [][]interface{} = make([][]interface{},len(res[0].Series[0].Values))
 77
 78         for i, row := range res[0].Series[0].Values {
 79                 t, err := time.Parse(time.RFC3339, row[0].(string))
 80                 if err != nil {
 81                         log.Fatal(err)
 82                 }
 83                 //myData[i] = row
 84                 //fmt.Println(row[1])
 85                 fmt.Println(i)
 86                 fmt.Println(t)
 87                 val, _ := row[1].(json.Number).Float64()
 88                 //val := row[1].(float64)
 89                 //log.Printf("[%2d] %s: %s\n", i, t.Format(time.Stamp), val)
 90                 //val := row[1].(int)
 91                 log.Printf("[%2d] %s: %f\n", i, t.Format(time.Stamp), val)
 92         }
 93         //fmt.Printf("%v\n",myData)

makes this error…

command-line-arguments

./influx-test.go:87: undefined: json in json.Number

What am I doing wrong here?

You need to import encoding/json.

import (
    ...
    "encoding/json"
    ...
)

Appears var select_output [][]interface{} = make([][]interface{},len(res[0].Series[0].Values)) and select_output[loop value] = loop row is faster than doing json. But the value in select_output[1] is not a float64

Is their a way to dump the select into a struct array if you define the struct and make an array out of it in GO so InfluxDB can pass each row into a Go Array Struct or the only way currently is to do this json way?