首页 > 解决方案 > 从 Python 更新 BigQuery 表属性会使表消失

问题描述

主题几乎说明了一切。当我运行 BigQuery 文档中的代码来更改表属性(在本例中为过期日期)时,它似乎只是简单地删除了表。(在 BQ GUI 中也找不到。)有人知道为什么吗?谢谢。

# Replace "dk" with your own initials before running this
s_table_id = 'hcwisdom.temp_tables.new_test_table'
from google.cloud import bigquery
client = bigquery.Client()
schema = [
    bigquery.SchemaField("full_name", "STRING", mode="REQUIRED"),
    bigquery.SchemaField("age", "INTEGER", mode="REQUIRED"),
]
try:
    client.get_table(s_table_id)  # Make an API request.
    print("Table {} exists.".format(s_table_id))
except:
    print("Creating table {}.".format(s_table_id))
    table = bigquery.Table(s_table_id, schema=schema)
    table = client.create_table(table)
# Verify
table = client.get_table(s_table_id)
print(
    "Found {} rows and {} columns in {}".format(
        table.num_rows, len(table.schema), s_table_id
    )
)
# Update table property
#   in the manner of https://cloud.google.com/bigquery/docs/samples/bigquery-update-table-expiration
import datetime
table = client.get_table(s_table_id)
table.expires = datetime.datetime.now() + datetime.timedelta(hours = 2)
client.update_table(table, ['expires'])
# Try to access the table -- you'll get a "not found" error
table = client.get_table(s_table_id)

标签: pythongoogle-bigquery

解决方案


您设置为在两小时内过期表 - 所以可能与时区有关。要检查这个“理论”,例如尝试设置 24 小时,看看问题是否仍然存在


推荐阅读