python - Google BigQuery:在 Python 中,列添加使所有其他列都为 Nullable
问题描述
我有一个已经存在的表,其架构如下:
{
"schema": {
"fields": [
{
"mode": "required",
"name": "full_name",
"type": "string"
},
{
"mode": "required",
"name": "age",
"type": "integer"
}]
}
}
它已经包含以下条目:
{'full_name': 'John Doe',
'age': int(33)}
我想插入一个带有新字段的新记录,并让加载作业在加载时自动添加新列。新格式如下所示:
record = {'full_name': 'Karen Walker',
'age': int(48),
'zipcode': '63021'}
我的代码如下:
from google.cloud import bigquery
client = bigquery.Client(project=projectname)
table = client.get_table(table_id)
config = bigquery.LoadJobConfig()
config.autoedetect = True
config.source_format = bigquery.SourceFormat.NEWLINE_DELIMITED_JSON
config.write_disposition = bigquery.WriteDisposition.WRITE_APPEND
config.schema_update_options = [
bigquery.SchemaUpdateOption.ALLOW_FIELD_ADDITION,
]
job = client.load_table_from_json([record], table, job_config=config)
job.result()
这会导致以下错误:
400 提供的架构与表 my_project:my_dataset:mytable 不匹配。字段年龄已将模式从 REQUIRED 更改为 NULLABLE
我可以通过config.schema_update_options
如下更改来解决此问题:
bigquery.SchemaUpdateOption.ALLOW_FIELD_ADDITION,
bigquery.SchemaUpdateOption.ALLOW_FIELD_RELAXATION
]
这允许我插入新记录,并zipcode
添加到架构中,但它会导致full_name
和age
成为NULLABLE
,这不是我想要的行为。有没有办法防止模式自动检测更改现有列?
解决方案
如果您需要向架构中添加字段,您可以执行以下操作:
from google.cloud import bigquery
client = bigquery.Client()
table = client.get_table("your-project.your-dataset.your-table")
original_schema = table.schema # Get your current table's schema
new_schema = original_schema[:] # Creates a copy of the schema.
# Add new field to schema
new_schema.append(bigquery.SchemaField("new_field", "STRING"))
# Set new schema in your table object
table.schema = new_schema
# Call API to update your table with the new schema
table = client.update_table(table, ["schema"])
更新表的架构后,您可以使用此附加字段加载新记录,而忽略任何架构配置。
推荐阅读
- node.js - 如何解码使用firebase admin NodeJS sdk生成的自定义令牌
- delegates - 为什么我的 Graphicsmagick 构建不使用 delegates.mgk 文件?
- java - 如何以角度验证我的 java 服务的响应?
- excel - Excel 序列函数的问题
- node.js - 从 npm 使用 tabula-js 时如何修复错误?
- maven - 如何在 OS-Big Sur 上安装 Maven(最好使用自制软件;两个文档都不起作用)
- facebook-graph-api - 如何通过 Facebook 图形 API 将我的 Instagram Messenger 页面订阅到网络挂钩
- 3d - 生成带有 3D 条形叠加和 2D 图像的图形
- php - PHP时区仅更改部分时间
- python - 在 iterrows() 期间通过startswith选择列