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

查看:33
本文介绍了在 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天全站免登陆