google-cloud-platform - 有没有办法一次清除多个表?
问题描述
我想清除存储在 bigquery 特定数据集中的表。
在控制台屏幕中,您不能一次删除多个表。
也无法在 bq CLI 中使用 * 进行删除。
有没有办法一次清除多个表?
解决方案
虽然在文档中声明一次只能删除一个表,但可以使用 Python 脚本发出 API 请求,以删除数据集中的所有表。
我创建并测试了以下脚本:
from google.cloud import bigquery
#construct BigQuery client object
client = bigquery.Client()
#select your dataset and list the tables within it
dataset_id='project_id.dataset'
tables = client.list_tables(dataset_id)
#inititalizing the list of tables
list_tables=[]
for table in tables:
#Create a list with the able reference for deletion 'project.dataset_id.table_id'
id =".".join([table.project,table.dataset_id,table.table_id])
list_tables.append(id)
#List of tables
print(list_tables)
#Delete all the tables inside the list of tables
for table in list_tables:
#print(table)
client.delete_table(table)
print("{} {}".format("Number of deleted tables in the dataset:", len(list_tables)))
我使用带有 Python 3 的 Jupyter Notebook 执行了上述代码。如果您在云 shell 环境中运行它,请确保安装所有依赖项pip install --upgrade google-cloud-bigquery
。
推荐阅读
- angular - 带有 Angular 8 和 Springboot 2 的 JWT:JWT 字符串必须恰好包含 2 个句点字符
- python - AttributeError 模块 'tensorflow' 在 python 3.7.6 MacOS 中没有属性
- java - 如何在android中用Canvas在一条线的尖端画一个圆圈
- c++ - 单例类不能处理多个文件
- flutter - Flutter Navigator 从替换的路由中获取数据
- http - 为什么我尝试发送 POST 请求时出现错误?
- asp.net - 为什么在.NETCORE 3.1,用Autofac,属性注册是不允许的
- python - 在gridlayout kivy中定位gridlayout
- python - 正则表达式 - 非捕获组不起作用
- kotlin - 如何在 Kotlin/多平台项目中使用 Gradle liquibaseRuntime 配置