如何使用通配符TABLE_DATE_RANGE()在Big查询中删除多个表?

时间:2018-01-07 23:44:24

标签: sql google-bigquery google-cloud-platform

我正在查看文档,但我还没有找到使用通配符删除多个表的方法。

我试图做这样的事情,但它不起作用:

owl = $(".owl-carousel");
owl.owlCarousel({
    loop:true,
    autoplaySpeed:1000,
    items:5,
    autoplay:true
});

4 个答案:

答案 0 :(得分:3)

DDL,例如在BigQuery中,DROP TABLE尚不存在。但是,我知道谷歌目前正在开展这项工作。

与此同时,您需要使用API​​删除表格。例如,使用gCloud工具:

bq rm -f -t dataset.table

如果你想进行批量删除,那么你可以使用一些bash / awk魔法。或者,如果您愿意,可以直接使用例如Rest API调用Rest API。 Python client

另见here

答案 1 :(得分:3)

对于数据集stats和诸如daily_table_20181017之类的保留日期约定的表,我将使用简单的脚本和gcloud命令行工具:

for table in `bq ls --max_results=10000000 stats |grep TABLE |grep daily_table |awk '{print $1}'`; do echo stats.$table; bq rm -f -t stats.$table; done

答案 2 :(得分:1)

我只是使用python循环这个并使用Graham示例解决它:

 from subprocess import call


   return_code = call('bq  rm -f -t dataset.' + table_name +'_'+  period + '', shell=True)

答案 3 :(得分:0)

@graham的方法很长时间对我有用。就在最近,BQ CLI停止有效运行,并且每次我运行上述命令时都冻结。因此,我研究了一种新方法,并使用了Google云官方文档的某些部分。我使用Jupyter笔记本遵循以下方法。

from google.cloud import bigquery

# TODO(developer): Construct a BigQuery client object.
client = bigquery.Client.from_service_account_json('/folder/my_service_account_credentials.json')

dataset_id = 'project_id.dataset_id'
dataset = client.get_dataset(dataset_id)

# Creating a list of all tables in the above dataset
tables = list(client.list_tables(dataset))  # API request(s)


## Filtering out relevant wildcard tables to be deleted
## Mention a substring that's common in all your tables that you want to delete

tables_to_delete = ["{}.{}.{}".format(dataset.project, dataset.dataset_id, table.table_id) 
                    for table in tables if "search_sequence_" in format(table.table_id)]

for table in tables_to_delete:
    client.delete_table(table)
    print("Deleted table {}".format(table))  ```