首页 > 解决方案 > 将数据帧写回 BQ 表

问题描述

job_config = bigquery.LoadJobConfig()
# job_config.autodetect = True
# job_config.source_format = bigquery.SourceFormat.NEWLINE_DELIMITED_JSON

job_config = bigquery.LoadJobConfig(schema=[
    bigquery.SchemaField("Weekend", "INT64")

])
job_config.write_disposition = "WRITE_TRUNCATE"

job = client.load_table_from_dataframe(
    full_df_at, table_id, job_config=job_config
)

# Wait for the load job to complete.
job.result()
print('A table {} is created'.format(table_id))

此代码导致以下错误。

TypeError                                 Traceback (most recent call last)
<ipython-input-39-c4a9bfab1edc> in <module>
      5 
      6 job_config = bigquery.LoadJobConfig(schema=[
----> 7     bigquery.SchemaField("Weekend", "INT64")
      8 
      9 ])

TypeError: __init__() got an unexpected keyword argument 'schema'

我该如何解决?这段代码运行良好。这是因为 Pandas 升级的变化吗?如何使此代码工作?

标签: pythongoogle-bigquery

解决方案


您可以尝试:

job_config = bigquery.LoadJobConfig()
# job_config.autodetect = True
# job_config.source_format = bigquery.SourceFormat.NEWLINE_DELIMITED_JSON

job_config.schema = [
        bigquery.SchemaField("Weekend", "INT64")
   ]

job_config.write_disposition = "WRITE_TRUNCATE"

job = client.load_table_from_dataframe(
    full_df_at, table_id, job_config=job_config
)

# Wait for the load job to complete.
job.result()
print('A table {} is created'.format(table_id)) 

您可以查看适用于 Google BigQuery 的 Python 客户端,您可以在其中找到有关如何使用 LoadJobConfig 的示例。


推荐阅读