【问题标题】:Updating BigQuery table properties from Python makes table disappear从 Python 更新 BigQuery 表属性会使表消失
【发布时间】:2020-10-20 14:40:13
【问题描述】:

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

【问题讨论】:

  • 您设置为在两小时内过期表 - 所以可能与时区有关。要检查这个“理论”,例如尝试设置 24 小时,看看问题是否仍然存在
  • 你说的很对——很好!如果您想发布它,很高兴将其指定为已接受的答案。
  • 当然。谢谢你的确认。移动回答

标签: python google-bigquery


【解决方案1】:

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

【讨论】:

  • 除了正确之外,这让我注意到了我忽略的cloud.google.com/bigquery/docs/samples/… 的时区控制功能:///////////// import pytz ///////////////table.expires = datetime.datetime.now(pytz.utc) + datetime.timedelta(hours = 2)
猜你喜欢
  • 1970-01-01
  • 2017-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-12
相关资源
最近更新 更多