Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AddPoint(…) method missing in v3 #118

Open
nargetdev opened this issue Nov 27, 2024 · 4 comments
Open

AddPoint(…) method missing in v3 #118

nargetdev opened this issue Nov 27, 2024 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@nargetdev
Copy link

Specifications

Follow the tutorial for influx cloud server less.

AddTag and AddField methods not found on Point (I think it’s supposed to be on the encoder)

Code sample to reproduce problem

CreatePoint(…).AddPoint(…).AddTag(…)

Expected behavior

New point

Actual behavior

Compile error

Additional info

No response

@nargetdev nargetdev added the bug Something isn't working label Nov 27, 2024
@bednar
Copy link
Member

bednar commented Nov 27, 2024

Hi @nargetdev,

Thank you for reaching out.

You mentioned AddTag and AddField, but these methods are not currently part of our official source base.

To better assist you and ensure you’re receiving accurate guidance, could you please share which tutorial or documentation you are following?

Looking forward to your response so we can help you.

Best Regards.

@nargetdev
Copy link
Author

Log into the influxdb cloud dashboard.

Load Data => Sources => ClientLibs::Go

Below instructions copied:

mkdir -p influxdb_go_client cd influxdb_go_client go mod init influxdb_go_client touch main.go

go get github.com/InfluxCommunity/influxdb3-go

export INFLUXDB_TOKEN=2fPEDnY6nD...........GGN0SlA==

package main

import (
    "context"
    "fmt"
    "time"

    "github.com/InfluxCommunity/influxdb3-go/influxdb3"
)

func main() {
    // Create client
    url := "https://us-east-1-1.aws.cloud2.influxdata.com"
    token := os.Getenv("INFLUXDB_TOKEN")

    // Create a new client using an InfluxDB server base URL and an authentication token
    client, err := influxdb3.New(influxdb3.ClientConfig{
      Host:  url,
      Token: token,
    })

    if err != nil {
      panic(err)
    }
    // Close client at the end and escalate error if present
    defer func (client *influxdb3.Client)  {
      err := client.Close()
      if err != nil {
        panic(err)
      }
    }(client)

    database := "generators"

    data := map[string]map[string]interface{}{
  "point1": {
    "location": "Klamath",
    "species":  "bees",
    "count":    23,
  },
  "point2": {
    "location": "Portland",
    "species":  "ants",
    "count":    30,
  },
  "point3": {
    "location": "Klamath",
    "species":  "bees",
    "count":    28,
  },
  "point4": {
    "location": "Portland",
    "species":  "ants",
    "count":    32,
  },
  "point5": {
    "location": "Klamath",
    "species":  "bees",
    "count":    29,
  },
  "point6": {
    "location": "Portland",
    "species":  "ants",
    "count":    40,
  },
}

// Write data
options := influxdb3.WriteOptions{
  Database: database,
}
for key := range data {
  point := influxdb3.NewPointWithMeasurement("census").
    AddTag("location", data[key]["location"].(string)).
    AddField(data[key]["species"].(string), data[key]["count"])

  if err := client.WritePointsWithOptions(context.Background(), &options, point); err != nil {
    panic(err)
  }

  time.Sleep(1 * time.Second) // separate points by 1 second
}


}

go run ./main.go

... specifically:

  for key := range data {
    point := influxdb3.NewPointWithMeasurement("census").
      AddTag("location", data[key]["location"].(string)).
      AddField(data[key]["species"].(string), data[key]["count"])

@nargetdev
Copy link
Author

I think they just forgot to update the tutorial to current syntax.

corrected:

package main

import (
    "context"
    "fmt"
    "os"
    "time"

    "github.com/InfluxCommunity/influxdb3-go/influxdb3"
)

func main() {
    // Create client
    url := "https://us-east-1-1.aws.cloud2.influxdata.com"
    token := os.Getenv("INFLUXDB_TOKEN")

    database := "test-telem"

    // Create a new client using an InfluxDB server base URL and an authentication token
    client, err := influxdb3.New(influxdb3.ClientConfig{
	    Host:     url,
	    Token:    token,
	    Database: database,
    })

    if err != nil {
	    panic(err)
    }
    // Close client at the end and escalate error if present
    defer func(client *influxdb3.Client) {
	    err := client.Close()
	    if err != nil {
		    panic(err)
	    }
    }(client)

    data := map[string]map[string]interface{}{
	    "point1": {
		    "location": "Klamath",
		    "species":  "bees",
		    "count":    23,
	    },
	    "point2": {
		    "location": "Portland",
		    "species":  "ants",
		    "count":    30,
	    },
	    "point3": {
		    "location": "Klamath",
		    "species":  "bees",
		    "count":    28,
	    },
	    "point4": {
		    "location": "Portland",
		    "species":  "ants",
		    "count":    32,
	    },
	    "point5": {
		    "location": "Klamath",
		    "species":  "bees",
		    "count":    29,
	    },
	    "point6": {
		    "location": "Portland",
		    "species":  "ants",
		    "count":    40,
	    },
    }

    // Write data
    points := make([]*influxdb3.Point, 0, len(data))
    for key := range data {
	    point := influxdb3.NewPoint(
		    "census",
		    map[string]string{
			    "location": data[key]["location"].(string),
		    },
		    map[string]any{
			    data[key]["species"].(string): data[key]["count"],
		    },
		    time.Now(),
	    )
	    points = append(points, point)

	    time.Sleep(1 * time.Second) // separate points by 1 second
    }

    if err := client.WritePoints(context.Background(), points); err != nil {
	    panic(err)
    }

    // Query data
    // Execute query
    query := `SELECT *
  FROM 'census'
  WHERE time >= now() - interval '1 hour'
    AND ('bees' IS NOT NULL OR 'ants' IS NOT NULL)`

    queryOptions := influxdb3.QueryOptions{
	    Database: database,
    }
    iterator, err := client.QueryWithOptions(context.Background(), &queryOptions, query)

    if err != nil {
	    panic(err)
    }

    for iterator.Next() {
	    value := iterator.Value()

	    location := value["location"]
	    ants := value["ants"]
	    bees := value["bees"]
	    fmt.Printf("in %s are %d ants and %d bees\n", location, ants, bees)
    }

}

@bednar
Copy link
Member

bednar commented Nov 29, 2024

@nargetdev thanks for clarification. We will take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants