首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有办法一次清除多个表?

问有没有办法一次清除多个表?
EN

Stack Overflow用户
提问于 2020-04-27 12:03:40
回答 2查看 107关注 0票数 0

我想要清除存储在bigquery的特定数据集中的表。

在控制台屏幕中,不能一次删除多个表。

也不能在bq中使用*删除。

有没有办法一次清除多个表?

EN

回答 2

Stack Overflow用户

发布于 2020-04-27 15:20:53

虽然在documentation中一次只能删除一个表,但是可以使用Python脚本发出一个请求,以便删除数据集中的所有表。

我创建并测试了以下脚本:

代码语言:javascript
复制
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。

票数 1
EN

Stack Overflow用户

发布于 2020-04-28 12:08:56

我遇到过类似的情况,并使用下面的Java代码删除了大量的no表。

替换dataset和table_prefix值。

将服务帐户JSON密钥文件路径设置为GOOGLE_APPLICATION_CREDENTIALS环境变量。

代码:

代码语言:javascript
复制
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);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61451540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档