搜索
首页后端开发GolangGolang InfluxDB 客户端使用 _field 键写入点

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

问题内容

我想用这样的签名写点InfluxDB:

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

但是 uuid 写入“field”键而不是“_field”,并且“_field”键具有默认值 - “value”。 我该如何修复它?

这是我的示例代码:

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()
}

正确答案


如果有人遇到同样的问题 - 可能的解决方案是:

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

以上是Golang InfluxDB 客户端使用 _field 键写入点的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:stackoverflow。如有侵权,请联系admin@php.cn删除
初始功能和副作用:平衡初始化与可维护性初始功能和副作用:平衡初始化与可维护性Apr 26, 2025 am 12:23 AM

Toensureinitfunctionsareeffectiveandmaintainable:1)Minimizesideeffectsbyreturningvaluesinsteadofmodifyingglobalstate,2)Ensureidempotencytohandlemultiplecallssafely,and3)Breakdowncomplexinitializationintosmaller,focusedfunctionstoenhancemodularityandm

开始GO:初学者指南开始GO:初学者指南Apr 26, 2025 am 12:21 AM

goisidealforbeginnersandsubableforforcloudnetworkservicesduetoitssimplicity,效率和concurrencyFeatures.1)installgromtheofficialwebsitealwebsiteandverifywith'.2)

进行并发模式:开发人员的最佳实践进行并发模式:开发人员的最佳实践Apr 26, 2025 am 12:20 AM

开发者应遵循以下最佳实践:1.谨慎管理goroutines以防止资源泄漏;2.使用通道进行同步,但避免过度使用;3.在并发程序中显式处理错误;4.了解GOMAXPROCS以优化性能。这些实践对于高效和稳健的软件开发至关重要,因为它们确保了资源的有效管理、同步的正确实现、错误的适当处理以及性能的优化,从而提升软件的效率和可维护性。

进行生产:现实世界的用例和示例进行生产:现实世界的用例和示例Apr 26, 2025 am 12:18 AM

Goexcelsinproductionduetoitsperformanceandsimplicity,butrequirescarefulmanagementofscalability,errorhandling,andresources.1)DockerusesGoforefficientcontainermanagementthroughgoroutines.2)UberscalesmicroserviceswithGo,facingchallengesinservicemanageme

go中的自定义错误类型:提供详细的错误信息go中的自定义错误类型:提供详细的错误信息Apr 26, 2025 am 12:09 AM

我们需要自定义错误类型,因为标准错误接口提供的信息有限,自定义类型能添加更多上下文和结构化信息。1)自定义错误类型能包含错误代码、位置、上下文数据等,2)提高调试效率和用户体验,3)但需注意其复杂性和维护成本。

使用GO编程语言构建可扩展系统使用GO编程语言构建可扩展系统Apr 25, 2025 am 12:19 AM

goisidealforbuildingscalablesystemsduetoitssimplicity,效率和建筑物内currencysupport.1)go'scleansyntaxandaxandaxandaxandMinimalisticDesignenhanceProductivityAndRedCoductivityAndRedCuceErr.2)ItSgoroutinesAndInesAndInesAndInesAndineSandChannelsEnablenableNablenableNableNablenableFifficConcurrentscorncurrentprogragrammentworking torkermenticmminging

有效地使用Init功能的最佳实践有效地使用Init功能的最佳实践Apr 25, 2025 am 12:18 AM

Initfunctionsingorunautomationbeforemain()andareusefulforsettingupenvorments和InitializingVariables.usethemforsimpletasks,避免使用辅助效果,andbecautiouswithTestingTestingTestingAndLoggingTomaintAnainCodeCodeCodeClarityAndTestesto。

INIT函数在GO软件包中的执行顺序INIT函数在GO软件包中的执行顺序Apr 25, 2025 am 12:14 AM

goinitializespackagesintheordertheordertheyimported,thenexecutesInitFunctionswithinApcageIntheirdeFinityOrder,andfilenamesdetermineTheOrderAcractacractacrosmultiplefiles.thisprocessCanbeCanbeinepessCanbeInfleccessByendercrededBydeccredByDependenciesbetenciesbetencemendencenciesbetnependendpackages,whermayleLeadtocomplexinitialitialializizesizization

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。