轻松重命名BigQuery中的多个表.是否可以? [英] Easily renaming multiple tables in BigQuery. Is it possible?

查看:48
本文介绍了轻松重命名BigQuery中的多个表.是否可以?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在BigQuery中有一堆表要重命名.这样做的主要原因是因为我想使用通配符表功能在我的查询中.表名的原始格式不适用于此功能.

I've got a bunch of tables in BigQuery that I'd like to rename. The primary reason for this is because I'd like to avail of the wildcard table feature in my queries. The original format of my table names are not designed to work well with this feature.

是否可以通过编程/轻松地在BigQuery中重命名很多表?

Is it possible to programmatically/easily rename a lot of tables in BigQuery?

推荐答案

当前,无法在BigQuery中重命名表.实现此目的的方法是运行BigQuery复制作业,该作业将复制具有新名称的表,完成后再删除原始表.运行复制作业不会产生处理费用.

Currently, there is no way to rename a table in BigQuery. The way to achieve this is by running a BigQuery copy job that will copy the table with the new name, and when it's finished then delete the original table. Running copy jobs do not incur processing charges.

使用 bq 命令行工具,一些简单的bash就可以解决问题:

Using the bq command line tool, some simple bash would do the trick:

#!/usr/bin/env bash
SRC="<project-id>:<dataset>"
bq ls --max_results=500 --format=csv $SRC | awk '{if(NR>1)print}' | awk -F, '{print $1}' | while read -r TABLE; do bq cp -f "${SRC}.${TABLE}" "${SRC}.${TABLE}_transformed_${RANDOM}" && bq rm -f "${SRC}.${TABLE}";done

警告:此操作随后将删除原始表!小心点.

Warning: this deletes the original table afterwards! Be careful.

这里要注意的一件事是-max_results 参数.这很重要,因为默认情况下, bq ls 命令仅列出数据集中的50个表.因此需要此参数来拉回数据集中的所有表(进行相应调整).

One thing to note here is the --max_results parameter. This is important because by default the bq ls command only lists 50 tables in the dataset. So this parameter is needed to pull back all the tables in the dataset (adjust accordingly).

这篇关于轻松重命名BigQuery中的多个表.是否可以?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆