【问题标题】:get the last modified date of tables using bigquery tables GET api使用 bigquery 表 GET api 获取表的最后修改日期
【发布时间】:2018-05-23 12:57:20
【问题描述】:

我正在尝试使用 bigquery REST API 获取表列表及其 last_modified_date。
在 bigquery API 资源管理器中,我正确获取了所有字段,但是当我使用 Python 代码中的 api 时,它返回“无”以表示修改日期。
这是用python编写的代码

from google.cloud import bigquery
client = bigquery.Client(project='temp')
datasets = list(client.list_datasets())

for dataset in datasets:
    print dataset.dataset_id

for dataset in datasets:
    for table in dataset.list_tables():
        print table.table_id
        print table.created
        print table.modified

在这段代码中,我得到了正确的创建日期,但所有表格的修改日期都是“无”。

【问题讨论】:

  • 试试print table 和/或dir(table)。它还有哪些其他属性?
  • modified 是它的属性之一,但它返回值“None”表示已修改。
  • 当您列出表 (tables.list behind the scenes) 时,唯一包含的属性似乎是 creationTimeexpirationTime。我认为您需要 get/reload 表(我不确定正确的 API),以便获取更多信息。

标签: python google-bigquery


【解决方案1】:

不太确定您使用的是哪个版本的 API,但我怀疑 latest versions 没有方法 dataset.list_tables()

不过,这是获取最后修改字段的一种方法,看看这是否适合您(或让您了解如何获取此数据):

from google.cloud import bigquery
client = bigquery.Client.from_service_account_json('/key.json')

dataset_list = list(client.list_datasets())
for dataset_item in dataset_list:
    dataset = client.get_dataset(dataset_item.reference)
    tables_list = list(client.list_tables(dataset))

    for table_item in tables_list:
        table = client.get_table(table_item.reference)
        print "Table {} last modified: {}".format(
            table.table_id, table.modified)

【讨论】:

    【解决方案2】:

    如果您只想从一个表中获取上次修改时间:

    from google.cloud import bigquery
        
    def get_last_bq_update(project, dataset, table_name):
        client = bigquery.Client.from_service_account_json('/key.json')
        table_id = f"{project}.{dataset}.{table_name}"
        table = client.get_table(table_id)
        print(table.modified)
    

    【讨论】:

    • 嗨 :),请在您的回答中添加一些描述。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2018-09-13
    • 2011-05-02
    相关资源
    最近更新 更多