Easily renaming multiple tables in BigQuery. Is it possible?

老子叫甜甜 提交于 2021-02-05 11:33:05

问题


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.

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


回答1:


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.

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.

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).



来源:https://stackoverflow.com/questions/41888723/easily-renaming-multiple-tables-in-bigquery-is-it-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!