我想要清除存储在bigquery的特定数据集中的表。
在控制台屏幕中,不能一次删除多个表。
也不能在bq中使用*删除。
有没有办法一次清除多个表?
发布于 2020-04-27 15:20:53
虽然在documentation中一次只能删除一个表,但是可以使用Python脚本发出一个请求,以便删除数据集中的所有表。
我创建并测试了以下脚本:
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 Notebook和Python3执行了上面的代码。如果您在您的cloud shell环境中运行它,请确保安装了所有依赖项pip install --upgrade google-cloud-bigquery。
发布于 2020-04-28 12:08:56
我遇到过类似的情况,并使用下面的Java代码删除了大量的no表。
替换dataset和table_prefix值。
将服务帐户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);
}
}https://stackoverflow.com/questions/61451540
复制相似问题