Home > Backend Development > Golang > Golang InfluxDB client uses _field key write point

Golang InfluxDB client uses _field key write point

WBOY
Release: 2024-02-06 11:33:04
forward
835 people have browsed it

Golang InfluxDB 客户端使用 _field 键写入点

Question content

I want to write some InfluxDB with this signature:

{
"_field": "some_uuid",
"_value": float64,
"_time": "2006-01-02T15:04:05.000Z"
}
Copy after login

But the uuid is written to the "field" key instead of "_field", and the "_field" key has a default value - "value". How can I fix it?

This is my sample code:

import (
  "fmt"
  influxdb2 "github.com/influxdata/influxdb-client-go/v2"
  "time"
)

type TagData struct {
    TagId     string  `json:"tag_id"`
    Value     float64 `json:"value"`
    Timestamp string  `json:"timestamp"`
}

func main() {
    inflClient := influxdb2.NewClient("my_url", "my_token")
    writeAPI := inflClient.WriteAPI("my_org", "my_bucket")

    data := TagData{
        TagId:     "d8f623a9-c255-4e0d-9c2f-126056875cef",
        Value:     300,
        Timestamp: "2023-12-11T15:35:42.343Z",
    }
    parsedTime, err := time.Parse("2006-01-02T15:04:05.000Z", data.Timestamp)
    if err != nil {
        fmt.Println("Error parsing time:", err)
        return
    }
    point := influxdb2.NewPointWithMeasurement("data").
        AddTag("field", data.TagId).
        AddField("value", data.Value).
        SetTime(parsedTime.UTC())
    writeAPI.WritePoint(point)
    writeAPI.Flush()
    inflClient.Close()
}
Copy after login


Correct answer


If anyone has the same problem - possible solutions are:

point := influxdb2.NewPointWithMeasurement("your_measurement").
    AddField(data.TagId, data.Value).
    SetTime(parsedTime.UTC())
Copy after login
point := influxdb2.NewPoint("your_measurement",
    map[string]string{},
    map[string]interface{}{data.TagId: data.Value},
    parsedTime.UTC())
Copy after login

The above is the detailed content of Golang InfluxDB client uses _field key write point. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template