BigQuery - DROP日期分片表的最佳方法

问题描述 投票:5回答:2

我有一些我想要删除的日期分片表,但是每个表已经有超过100个分片,并且不能手动删除它们。

我尝试过使用通配符

DROP TABLE my_dataset.my_table_*;

但它似乎没有用。

我终于使用了python API:

for table_id in tables:
    table_ref = client.dataset(dataset_id).table(table_id)
    client.delete_table(table_ref)

它工作,但我需要创建表数组与我想要删除的表的名称。

有没有办法在BigQuery形式的UI中DROP日期分片表的所有日期分片?

或者在UI中使用SQL命令?

或者使用带有通配符的命令行?

谢谢

google-cloud-platform google-bigquery
2个回答
3
投票

而不是创建表格数组(使用表格的名称)而不是...

from google.cloud import bigquery
client = bigquery.Client()
dataset_ref = client.dataset('my_dataset')

tables = list(client.list_tables(dataset_ref))  # API request(s), now you have the list of tables in this dataset
queried_tables=[]
for table in tables:
    print(table.table_id)
    if table.table_id.startswith("your_favourite_prefix"): #will perform the action only if the table has the desired prefix
        queried_tables.append(table.table_id)

print(queried_tables) #the list of the desired tables names, now you can use your script to delete them all

1
投票

没有内置方法可以删除共享公共前缀的所有表。您使用Python库删除它们的方法是一个合理的选择,或者您可以使用调用bq rm dataset.table_name的循环从命令行执行相同的操作。

© www.soinside.com 2019 - 2024. All rights reserved.