登录
首页 >  Golang >  Go问答

使用 goavro 创建的 Avro 文件将数值数据加载到 BigQuery 中

来源:stackoverflow

时间:2024-04-04 17:54:38 340浏览 收藏

小伙伴们对Golang编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《使用 goavro 创建的 Avro 文件将数值数据加载到 BigQuery 中》,就很适合你,本篇文章讲解的知识点主要包括。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!

问题内容

我试图弄清楚如何使用 avro 文件将美元值加载到 bigquery 中的数字列中。我使用 golang 和 goavro 包来生成 avro 文件。

看来 go 中处理钱的合适数据类型是 big.rat。

bigquery 文档表明应该可以使用 avro 来实现此目的。

从一些 goavro 测试用例中我可以看出,将 *big.rat 编码为固定的.decimal 类型是可能的。

我正在使用 goavro.ocfwriter 使用简单的 avro 模式对数据进行编码,如下所示:

{
  "type": "record",
  "name": "mydata",
  "fields": [
    {
      "name": "id",
      "type": [
        "string"
      ]
    },
    {
      "name": "cost",
      "type": [
        "null",
        {
          "type": "fixed",
          "size": 12,
          "logicaltype": "decimal",
          "precision": 4,
          "scale": 2
        }
      ]
    }
  ]
}

我正在尝试使用“成本”字段附加数据,如下所示:

map[string]interface{}{"fixed.decimal": big.newrat(617, 50)}

编码已成功,但生成的 avro 文件无法加载到 bigquery 中:

Err: load Table MyTable Job: {Location: ""; Message: "Error while reading data, error message: The Apache Avro library failed to parse the header with the following error: Missing Json field \"name\": {\"logicalType\":\"decimal\",\"precision\":4,\"scale\":2,\"size\":12,\"type\":\"fixed\"}"; Reason: "invalid"}

所以我在这里做错了什么......希望有人能指出我正确的方向。


解决方案


我明白了。我需要使用bytes.decimal而不是fixed.decimal

{
  "type": "record",
  "name": "mydata",
  "fields": [
    {
      "name": "id",
      "type": [
        "string"
      ]
    },
    {
      "name": "cost",
      "type": [
        "null",
        {
          "type": "bytes",
          "logicaltype": "decimal",
          "precision": 4,
          "scale": 2
        }
      ]
    }
  ]
}

然后类似地编码

map[string]interface{}{"bytes.decimal": big.NewRat(617, 50)}

而且效果很好!

理论要掌握,实操不能落!以上关于《使用 goavro 创建的 Avro 文件将数值数据加载到 BigQuery 中》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

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