从Python更新BigQuery表属性会使表消失

2024-04-27 00:08:25 发布

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

主题几乎说明了一切。当我运行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)

Tags: importclientidformatcloudgetdatetimemode