使用Airflow加载到bigQuery时如何忽略未知列?

时间:2018-09-25 13:35:36

标签: google-bigquery airflow

我正在使用GoogleCloudStorageToBigQueryOperator

将数据从Google存储空间加载到bigQuery

可能Json文件中的列将比我定义的更多。在这种情况下,我希望继续加载作业-只需忽略此无法识别的列即可。 我尝试使用ignore_unknown_values参数,但没有任何区别。

我的接线员:

def dc():
    return [
    {
        "name": "id",
        "type": "INTEGER",
        "mode": "NULLABLE"
    },
    {
        "name": "storeId",
        "type": "INTEGER",
        "mode": "NULLABLE"
    },
 ...
]
gcs_to_bigquery_st = GoogleCloudStorageToBigQueryOperator(
    dag=dag,
    task_id='load_to_BigQuery_stage',
    bucket=GCS_BUCKET_ID,
    destination_project_dataset_table=table_name_template_st,
    source_format='NEWLINE_DELIMITED_JSON',
    source_objects=[gcs_export_uri_template],
    ignore_unknown_values = True,
    schema_fields=dc(),
    create_disposition='CREATE_IF_NEEDED',
    write_disposition='WRITE_APPEND',
    skip_leading_rows = 1,
    google_cloud_storage_conn_id=CONNECTION_ID,
    bigquery_conn_id=CONNECTION_ID
)

错误:

  读取数据时出现

u'Error,错误消息:JSON解析行中的错误   从位置0开始:没有这样的字段:shippingService。',

是真的。 shippingService不存在,也不会添加到表中。

我该如何解决?

编辑: 从运算符中删除了schema_fields=dc()

gcs_to_bigquery_st = GoogleCloudStorageToBigQueryOperator(
    dag=dag,
    task_id='load_to_BigQuery_stage',
    bucket=GCS_BUCKET_ID,
    destination_project_dataset_table=table_name_template_st,
    source_format='NEWLINE_DELIMITED_JSON',
    source_objects=[gcs_export_uri_template],
    ignore_unknown_values = True,
    create_disposition='CREATE_IF_NEEDED',
    write_disposition='WRITE_APPEND',
    skip_leading_rows = 1,
    google_cloud_storage_conn_id=CONNECTION_ID,
    bigquery_conn_id=CONNECTION_ID
)

仍然给出相同的错误。 这不会产生场景。它具有忽略未知值的命令:(

1 个答案:

答案 0 :(得分:1)

我能想到的唯一原因是您可能正在使用Airflow 1.9。此功能已添加到 Airflow 1.10 中。

不过,您可以在 Airflow 1.9 中通过添加src_fmt_configs={'ignoreUnknownValues': True}来使用它:

gcs_to_bigquery_st = GoogleCloudStorageToBigQueryOperator(
    dag=dag,
    task_id='load_to_BigQuery_stage',
    bucket=GCS_BUCKET_ID,
    destination_project_dataset_table=table_name_template_st,
    source_format='NEWLINE_DELIMITED_JSON',
    source_objects=[gcs_export_uri_template],
    src_fmt_configs={'ignoreUnknownValues': True},
    create_disposition='CREATE_IF_NEEDED',
    write_disposition='WRITE_APPEND',
    skip_leading_rows = 1,
    google_cloud_storage_conn_id=CONNECTION_ID,
    bigquery_conn_id=CONNECTION_ID
)