Google cloud platform 有没有办法一次清除多个表?
我想清除bigquery的特定数据集中存储的表 在控制台屏幕中,不能同时删除多个表 在bq CLI中使用*也无法删除 是否有一种方法可以一次清除多个表?在中,说明一次只能删除一个表,但可以使用Python脚本发出API请求,以便删除数据集中的所有表 我创建并测试了以下脚本:Google cloud platform 有没有办法一次清除多个表?,google-cloud-platform,google-bigquery,Google Cloud Platform,Google Bigquery,我想清除bigquery的特定数据集中存储的表 在控制台屏幕中,不能同时删除多个表 在bq CLI中使用*也无法删除 是否有一种方法可以一次清除多个表?在中,说明一次只能删除一个表,但可以使用Python脚本发出API请求,以便删除数据集中的所有表 我创建并测试了以下脚本: from google.cloud import bigquery #construct BigQuery client object client = bigquery.Client() #select your da
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)))
我使用Jupyter笔记本和Python 3执行了上述代码。如果在cloud shell环境中运行它,请确保安装了所有依赖项
pip install——升级google cloud bigquery
我也遇到过类似情况,并使用以下Java代码删除了大量表
替换数据集和表前缀值
将服务帐户JSON密钥文件路径设置为GOOGLE_APPLICATION_CREDENTIALS环境变量
代码:
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
public class TableKiller {
String dataset = "MY_DATASET";
String table_prefix = "temp_table_";
public static void main(String[] args) throws ExecutionException, InterruptedException {
new TableKiller().deleteAll();
}
public void deleteAll() throws InterruptedException, ExecutionException {
ArrayList<String> tableNames = new ArrayList<>();
BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService();
int i = 1;
bigQuery.listTables(dataset, BigQuery.TableListOption.pageSize(1000))
.iterateAll()
.forEach(table -> {
String tableName = table.getTableId().getTable();
if (tableName.startsWith(table_prefix)) {
tableNames.add(tableName);
System.out.println("Added " + tableName + i);
}
});
ForkJoinPool forkJoinPool = new ForkJoinPool(200);
forkJoinPool.submit(() -> tableNames
.parallelStream()
.forEach(this::deleteTable)).get();
}
private void deleteTable(String tableName) {
BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService();
bigQuery.delete(TableId.of(dataset, tableName));
System.out.println("Deleted " + tableName);
}
}
import java.util.ArrayList;
导入java.util.concurrent.ExecutionException;
导入java.util.concurrent.ForkJoinPool;
公共类桌面杀手{
String dataset=“我的数据集”;
字符串table_prefix=“temp_table”;
公共静态void main(字符串[]args)引发ExecutionException、InterruptedException{
新建TableKiller().deleteAll();
}
public void deleteAll()引发InterruptedException、ExecutionException{
ArrayList tableNames=新的ArrayList();
BigQuery BigQuery=BigQueryOptions.getDefaultInstance().getService();
int i=1;
listTables(数据集,bigQuery.TableListOption.pageSize(1000))
.iterateAll()
.forEach(表->{
字符串tableName=table.getTableId().getTable();
if(tableName.startsWith(table_前缀)){
tableName.add(tableName);
System.out.println(“添加”+tableName+i);
}
});
ForkJoinPool ForkJoinPool=新的ForkJoinPool(200);
forkJoinPool.submit(()->表名
.parallelStream()
.forEach(this::deleteTable)).get();
}
私有void deleteTable(字符串tableName){
BigQuery BigQuery=BigQueryOptions.getDefaultInstance().getService();
delete(TableId.of(dataset,tableName));
System.out.println(“已删除”+表名);
}
}