InfluxDB与GoLang

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

InfluxDB With GoLang

问题

我对InfluxDB非常陌生,似乎在理解如何使用Go客户端方面遇到了一些困难。我目前正在使用默认的示例代码,但我无法理解数据被上传到哪里,或者是否真的被上传了。当前的代码如下:

package main

import (
	"context"
	"fmt"
	"time"

	influxdb2 "github.com/influxdata/influxdb-client-go/v2"
)

func main() {

	token := "tokenInsertedHere"

	fmt.Println("testing influxdb")
	// Create a new client using an InfluxDB server base URL and an authentication token
	client := influxdb2.NewClient("http://localhost:8086", token)
	// Use blocking write client for writes to desired bucket
	writeAPI := client.WriteAPIBlocking("orgName", "bucketName")
	// Create point using full params constructor
	p := influxdb2.NewPoint("test",
		map[string]string{"unit": "temperature"},
		map[string]interface{}{"avg": 24.5, "max": 45.0},
		time.Now())
	// write point immediately
	writeAPI.WritePoint(context.Background(), p)
    client.Close()
}

当我在数据浏览器页面中过滤存储桶中的测量时,该代码应该写入的测量不会出现。我做错了什么?我注意到客户端没有抛出任何错误,这对我来说很奇怪。我尝试使用一个虚假的令牌,当写入数据库时,它表现得好像没有任何问题。希望能得到帮助!

英文:

I'm very new to InfluxDB and seem to be having some trouble understanding how to use the Go client. I'm currently using the default example code but I can't understand where to find the data that is being uploaded, or if it is being uploaded at all. The current code looks like

package main

import (
	"context"
	"fmt"
	"time"

	influxdb2 "github.com/influxdata/influxdb-client-go/v2"
)

func main() {

	token := "tokenInsertedHere"

	fmt.Println("testing influxdb")
	// Create a new client using an InfluxDB server base URL and an authentication token
	client := influxdb2.NewClient("http://localhost:8086", token)
	// Use blocking write client for writes to desired bucket
	writeAPI := client.WriteAPIBlocking("orgName", "bucketName")
	// Create point using full params constructor
	p := influxdb2.NewPoint("test",
		map[string]string{"unit": "temperature"},
		map[string]interface{}{"avg": 24.5, "max": 45.0},
		time.Now())
	// write point immediately
	writeAPI.WritePoint(context.Background(), p)
    client.Close()
}

When I'm on the data explorer page filtering measurements in the bucket the code should've wrote to, the measurement doesn't pop up. What am I doing wrong? I've noticed that the client doesn't throw any errors, which is strange to me. I've tried using a fake token and it acts like there were no problems when writing to the db. Would appreciate any help!

答案1

得分: 1

我无法确定这是否是你的问题,但是保持我的令牌和组织名称正确对应总是让我困惑。在Influx网络客户端中,转到“切换组织”并验证你正在使用的组织是否与网络客户端中选择的组织匹配。希望这能帮到你。

英文:

I can't conclusively know if this is your issue, but keeping my token and org names lined up correctly always trips me up. In the Influx web client, go to "Switch Organizations" and verify that the organization that you are using matches the organization selected in the web client. Hopefully this helps.

答案2

得分: 1

将带有write的行更改为捕获错误并检查发生了什么错误:

   err := writeAPI.WritePoint(context.Background(), p)
   if err != nil {
      fmt.Println("发生错误:", err)
   }
英文:

Change the line with write to catch an error and check what error occured:

   err := writeAPI.WritePoint(context.Background(), p)
   if err != nil {
      panic(err)
   }

huangapple
  • 本文由 发表于 2022年8月19日 12:22:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/73411807.html
匿名

发表评论

匿名网友

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

确定