本文介绍了将 CSV 导入 BigQuery 中的表时无法添加字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 python 脚本,它执行 gbq 作业以将 csv 文件 f 加载到 BigQuery 中的表.我正在尝试以 csv 格式上传数据并收到以下错误:

I have a python script that executes a gbq job to load a csv file f to table in BigQuery. I am trying to upload data in csv format and getting the following error:

 400 Invalid schema update. Cannot add fields (field: string_field_8)

这是我的 csv:

    id,first_name,username,last_name,chat_username,chat_id,forward_date,message_text
    231125223|Just|koso|swissborg_bounty|-1001368946079|1517903147|tes
  481895079|Emerson|EmersonEmory|swissborg_bounty|-1001368946079|1517904387|pictu
    316560356|Ken Sam|ICOnomix|swissborg_bounty|-1001368946079|1517904515|Today

这是我的代码:

from google.cloud.bigquery import Client
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = '***.json'
os.environ['GOOGLE_CLOUD_DISABLE_GRPC'] = 'True'

from google.cloud import bigquery
dataset_name = 'test_temporary_dataset'
table_name='table_telega'
bigquery_client = bigquery.Client()
dataset = bigquery_client.dataset(dataset_name)
table = dataset.table(table_name)
job_config = bigquery.LoadJobConfig()
job_config.source_format = 'text/csv'
job_config.skip_leading_rows = 1
job_config.autodetect = True
job_config.fieldDelimiter='|'
job_config.allow_jagged_rows=True
job_config.ignoreUnknownValues=True
job_config.allow_quoted_newlines=True
with open('**.csv', 'rb') as source_file:
    #job = table.upload_from_file(source_file, source_format='text/csv')
    job=bigquery_client.load_table_from_file(source_file, table, job_config=job_config)

job.result()
print(job.result())

怎么解决?我应该改变什么?

how to fix it? what should I change ?

推荐答案

只需在代码中添加这一行

Just add this line in your code

job_config._properties['load']['schemaUpdateOptions'] = ['ALLOW_FIELD_ADDITION']

这将允许在您现有的架构中添加列.

and this will allow column addition to your existing schema.

这篇关于将 CSV 导入 BigQuery 中的表时无法添加字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 11:47
查看更多