【问题标题】:Schema avro is in timestamp but in bigquery comes as integer架构 avro 在时间戳中,但在 bigquery 中以整数形式出现
【发布时间】:2020-03-24 05:19:16
【问题描述】:

我有一个将 avro 文件上传到 bigquery 的管道,配置的架构似乎没问题,但 BigQuery 将其理解为整数值而不是日期字段。在这种情况下我该怎么办?

Schema 的 avro - 日期字段:

{
  "name": "date",
  "type": {
    "type": "long",
    "logicalType": "timestamp-millis"
  },
  "doc": "the date where the transaction happend"
}

大查询表:

我尝试使用下面的代码,但它只是忽略了它。你知道原因吗?

import gcloud
from gcloud import storage
from google.cloud import bigquery

def insert_bigquery_avro(target_uri, dataset_id, table_id):
    bigquery_client = bigquery.Client()
    dataset_ref = bigquery_client.dataset(dataset_id)
    job_config = bigquery.LoadJobConfig()
    job_config.autodetect = True
    job_config.source_format = bigquery.SourceFormat.AVRO
    job_config.use_avro_logical_types = True
    time_partitioning = bigquery.table.TimePartitioning()
#    time_partitioning = bigquery.table.TimePartitioning(type_=bigquery.TimePartitioningType.DAY, field="date")
    job_config.time_partitioning = time_partitioning
    uri = target_uri
    load_job = bigquery_client.load_table_from_uri(
        uri,
        dataset_ref.table(table_id),
        job_config=job_config
        )
    print('Starting job {}'.format(load_job.job_id))
    load_job.result()
    print('Job finished.')

【问题讨论】:

    标签: python google-bigquery schema avro


    【解决方案1】:

    这是因为 BigQuery 在默认情况下会忽略 logicalType 属性,而是使用底层 Avro 类型。例如,Avro timestamp-millis 逻辑类型在 BigQuery 中设置为 Integer。

    要启用转换,请使用命令行工具将--use_avro_logical_types 设置为True,或在调用jobs.insert 方法创建加载作业时在作业资源中设置useAvroLogicalTypes 属性。在此之后,您的字段 date 将设置为 Timestamp 在 BigQuery 中输入。

    查看Avro logical types and BigQuery 文档以查看所有被忽略的 Avro 逻辑类型以及在设置该标志后如何转换它们。这也将帮助您为您的字段确定最佳的 Avro 逻辑类型。

    希望这有帮助。

    【讨论】:

    • 我尝试使用下面的代码,但它只是忽略了它。你知道原因吗? (我在问题中包含脚本)
    • 你能帮帮我吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多