【问题标题】:Is there a way to set the expiration date of a BigQuery table to "None" using the Python client library?有没有办法使用 Python 客户端库将 BigQuery 表的到期日期设置为“无”?
【发布时间】:2022-01-20 06:16:10
【问题描述】:

在我的结算之前创建的 BigQuery 表已设置为过期。

我不希望这些表被自动删除,所以我正在寻找一种方法将这些表的到期日期设置为“无”。

但是,我有 100 多张桌子,所以我正在寻找一种方便的方法来执行此操作。 (因此,我不是在寻找一种在控制台中逐个设置它们的方法。)

我写的示例代码如下

from google.cloud import bigquery

# Construct a BigQuery client object.
client = bigquery.Client()

# TODO(developer): Set dataset_id to the ID of the dataset that contains
#                  the tables you are listing.
dataset_id = 'my_project_name.analytics_00000000'
expiration = None

tables = client.list_tables(dataset_id)  # Make an API request.
for table in tables:
    table.expires = expiration
    table = client.update_table(table, ["expires"])

这是我收到的错误消息。

can't set attribute
  File "set_expires_to_none.py", line 13, in <module>
    table.expires = expiration

任何建议将不胜感激。

【问题讨论】:

  • 设置未来日期为 2999-12-31。如果可行

标签: python google-bigquery


【解决方案1】:

这不起作用的原因是因为list_tables 实际上并没有返回一个表对象它返回一个A read-only table resource from a list operation. 请参阅此处的文档:https://googleapis.dev/python/bigquery/latest/generated/google.cloud.bigquery.table.TableListItem.html#google.cloud.bigquery.table.TableListItem

尝试以下方法:

tables = client.list_tables('so_test')
for table in tables:
    table = client.get_table(table)
    table.expires = None
    table = client.update_table(table, ["expires"])
    print(table.table_id)
    print(table.expires)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-23
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 2018-09-15
    • 2020-06-24
    • 2011-08-24
    • 2023-04-11
    相关资源
    最近更新 更多