登录
首页 >  Golang >  Go问答

将 Go 用于 firehose 数据流插入

来源:stackoverflow

时间:2024-02-21 08:54:24 415浏览 收藏

本篇文章向大家介绍《将 Go 用于 firehose 数据流插入》,主要包括,具有一定的参考价值,需要的朋友可以参考一下。

问题内容

我有以下 json 文件

{
    "@timestamp": "2021-11-19t21:32:55.196z",
    "@version": "1",
    "message": "manual test to firehose."
}

我有以下 go 函数

func insertintofirehose(sess *session.session, hosename string) {
    svc := firehose.new(sess, aws.newconfig().withregion("us-east-1"))
    //firehosedata := getobjectfroms3bucket(sess)
    firehosedata, _ := os.readfile("/temp/test.json")

    var rec firehose.record

    var recinput firehose.putrecordinput

    datajson, _ := json.marshal(firehosedata)
    rec.setdata(datajson)
    recinput.setdeliverystreamname(hosename)
    recinput.setrecord(&rec)

    res, err1 := svc.putrecord(&recinput)

    if err1 != nil {
        log.fatal(err1)
    }
    fmt.println(res)

}

我想做的是获取一个文件并将其插入到消防软管中,但我收到此错误消息:

{"type":"mapper_parsing_exception","reason":"failed to parse","caused_by":
{"type":"not_x_content_exception",
"reason":"not_x_content_exception: compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"}}

我不太确定我做错了什么。

更改记录以直接从文件获取数据会返回此错误:

One or more records are malformed. Please ensure that each record is single valid JSON object and that it does not contain newlines.

正确答案


{
    "@timestamp": "2021-11-19t21:32:55.196z",
    "@version": "1",
    "message": "manual test to firehose.",
}

我认为这不是有效的 json,它在第三行有一个尾随逗号。

这是有效的

{
    "@timestamp": "2021-11-19t21:32:55.196z",
    "@version": "1",
    "message": "manual test to firehose."
}

并且 firehosedata 已经是 []byte,所以我认为您不需要再次 json.marshal

这是元帅的结果

代码:

package main

import (
    "encoding/json"
    "fmt"
    "os"
)

func main() {
    firehosedata, _ := os.readfile("./file.json") // same value

    fmt.printf("%+v\n", string(firehosedata))

    test, err := json.marshal(firehosedata)

    fmt.printf("%+v\n", string(test))
    fmt.printf("%+v\n", err)
}

输出:

{
    "@timestamp": "2021-11-19T21:32:55.196Z",
    "@version": "1",
    "message": "Manual test to firehose.",   
}
"ew0KICAgICJAdGltZXN0YW1wIjogIjIwMjEtMTEtMTlUMjE6MzI6NTUuMTk2WiIsDQogICAgIkB2ZXJzaW9uIjogIjEiLA0KICAgICJtZXNzYWdlIjogIk1hbnVhbCB0ZXN0IHRvIGZpcmVob3NlLiIsDQp9"

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《将 Go 用于 firehose 数据流插入》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>