Python 模式avro在时间戳中,但在bigquery中是整数

Python 模式avro在时间戳中,但在bigquery中是整数,python,google-bigquery,schema,avro,Python,Google Bigquery,Schema,Avro,我有一个将avro文件上传到bigquery的管道,配置的模式看起来不错,但bigquery理解为一个整数值,而不是一个日期字段。在这种情况下我能做什么 模式的avro-日期字段: { "name": "date", "type": { "type": "long", "logicalType": "timestamp-millis" }, "doc": "the date where the transaction happend" } 大查询表: 我尝试

我有一个将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类型。例如,在BigQuery中,Avro timestamp millis逻辑类型设置为Integer

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

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


希望这有帮助。

我尝试使用下面的代码,但它只是忽略了它。你知道原因吗?(我把剧本包括在问题中)你能帮我吗?