【问题标题】:BigQuery Avro load job with useAvroLogicalTypes incorrect field typesBigQuery Avro 加载作业使用 useAvroLogicalTypes 不正确的字段类型
【发布时间】:2019-12-30 09:55:59
【问题描述】:

我使用 node.js 来:

  • 使用 avsc NPM 包创建 Avro 文件
  • 使用@google-cloud/storage NPM 包上传到 GCS
  • 使用@google-cloud/bigquery NPM 包调用 BQ API 以将 Avro 从 GCS 加载到 BQ

我的问题是,即使我在创建加载作业时设置了useAvroLogicalTypes,我的日期数据也永远不会在 BQ 中正确创建为TIMESTAMP,总是INTEGER - which should be the normal behavioruseAvroLogicalTypes设置。

根据相同的文档,如果在 Avro 架构定义中设置了 timestamp-millis,它应该加载为 TIMESTAMP

我的配置:

Avro 架构

{
      "name": "metadata",
      "type": {
        "name": "metadata",
        "type": "record",
        "fields": [
          {
            "name": "creationTime",
            "type": "long",
            "logicalType": "timestamp-millis"
          },
          {
            "name": "lastActivity",
            "type": ["null", "long"],
            "logicalType": "timestamp-millis"
          },
          {
            "name": "deletionTime",
            "type": ["null", "long"],
            "logicalType": "timestamp-millis"
          },
          {
            "name": "lastSignInTime",
            "type": ["null", "long"],
            "logicalType": "timestamp-millis"
          }
        ]
      }
    }

创建加载作业

const metadata = {
  sourceFormat: 'AVRO',
  useAvroLogicalTypes: true
}

我可以让它工作的唯一方法是在加载作业的元数据变量中再次指定 scema。我的问题是,指定 Avro 架构并打开 useAvroLogicalTypes 以避免必须再次显式指定加载作业元数据的架构不是重点吗?

工作设置

const metadata = {
    sourceFormat: 'AVRO',
    useAvroLogicalTypes: true,
    schema: {
      fields: [
        ...otherFields,
        {
          name: 'metadata',
          type: 'STRUCT',
          mode: 'REQUIRED',
          fields: [
            { name: 'creationTime', type: 'TIMESTAMP', mode: 'REQUIRED' },
            { name: 'lastActivity', type: 'TIMESTAMP', mode: 'NULLABLE' },
            { name: 'deletionTime', type: 'TIMESTAMP', mode: 'NULLABLE' },
            { name: 'lastSignInTime', type: 'TIMESTAMP', mode: 'NULLABLE' }
          ]
        }
      ]
    }
  }

【问题讨论】:

    标签: google-bigquery avro


    【解决方案1】:

    回答有关在创建加载作业期间指定 Avro 架构的问题。可以省略模式属性,它是目标表的模式:

    如果目标表已经存在,或者您正在从 Google Cloud Datastore 加载数据,则可以省略架构。

    如果您想了解更多相关信息,请参阅documentation

    此外,您还可以查看替换架构定义的--autodetect flag

    希望对你有帮助。

    【讨论】:

    • 谢谢你,Alexandre,你是对的,我没有注意到或正确理解这些信息。我要特别感谢您加入 StackOverflow 来帮助我! :) 新年快乐!
    猜你喜欢
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    相关资源
    最近更新 更多