【发布时间】:2021-09-08 10:49:26
【问题描述】:
如何在 Golang 中使用 goavro 重新创建使用 avro 序列化的原始数据结构?
有了这个库https://github.com/hamba/avro,这很容易。
out := SimpleRecord{}
err = avro.Unmarshal(schema, data, &out)
变量输出的类型是 SimpleRecord。
假设我有这个结构和 avro 架构:
type SimpleRecord struct {
F1 int `avro:"f1"`
F2 string `avro:"f2"`
F3 string `avro:"f3"`
Dependencies []string `avro:"dependencies"`
}
func main() {
avro_schema_txt := `{
"type": "record",
"name": "AvroData",
"namespace": "data.avro",
"doc": "docstring",
"fields": [
{
"name": "f1",
"type": "int"
},
{
"name": "f2",
"type": "string"
},
{
"name": "f3",
"type": "string"
},
{
"name": "dependencies",
"type": {
"type": "array",
"items": "string"
}
}
]
}`
}
然后
codec, err := goavro.NewCodec(avro_schema_txt)
if err != nil {
log.Fatal(err.Error())
}
out, _, err := codec.NativeFromBinary(data)
if err != nil {
log.Fatal(err.Error())
}
fmt.Println(out)
其中数据用 avro 编组,out 是 interface{} 类型,那么我怎样才能“制作”它 SimpleRecord?
【问题讨论】: