繁体   English   中英

Schema avro 在时间戳中,但在 bigquery 中以整数形式出现

[英]Schema avro is in timestamp but in bigquery comes as integer

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

架构的 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.')

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

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

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

希望这会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM