duan0818 2018-09-19 06:40
浏览 90

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

I am trying to figure out how to load dollar values into a Numeric column in BigQuery using an Avro file. I am using golang and the goavro package to generate the avro file.

It appears that the appropriate datatype in go to handle money is big.Rat.

BigQuery documentation indicates it should be possible to use Avro for this.

I can see from a few goavro test cases that encoding a *big.Rat into a fixed.decimal type is possible.

I am using a goavro.OCFWriter to encode data using a simple avro schema as follows:

{
  "type": "record",
  "name": "MyData",
  "fields": [
    {
      "name": "ID",
      "type": [
        "string"
      ]
    },
    {
      "name": "Cost",
      "type": [
        "null",
        {
          "type": "fixed",
          "size": 12,
          "logicalType": "decimal",
          "precision": 4,
          "scale": 2
        }
      ]
    }
  ]
}

I am attempting to Append data with the "Cost" field as follows:

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

This is successfully encoded, but the resulting avro file fails to load into 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"}

So am doing something wrong here... Hoping someone can point me in the right direction.

  • 写回答

1条回答 默认 最新

  • dragon88112 2018-09-20 18:27
    关注

    I figured it out. I need to use bytes.decimal instead of fixed.decimal

    {
      "type": "record",
      "name": "MyData",
      "fields": [
        {
          "name": "ID",
          "type": [
            "string"
          ]
        },
        {
          "name": "Cost",
          "type": [
            "null",
            {
              "type": "bytes",
              "logicalType": "decimal",
              "precision": 4,
              "scale": 2
            }
          ]
        }
      ]
    }
    

    Then encode similarly

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

    And it works nicely!

    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试