将数据帧写回BQ表

2024-09-28 21:50:59 发布

您现在位置:Python中文网/ 问答频道 /正文

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'

我如何解决它?该代码一直运行良好。这是因为熊猫升级的变化吗?我如何使这个代码工作


Tags: 代码idconfigformatschematablejobload
1条回答
网友
1楼 · 发布于 2024-09-28 21:50:59

您可以尝试以下方法:

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)) 

您可以查看Python Client for Google BigQuery,在那里可以找到如何使用LoadJobConfig的示例

相关问题 更多 >