Help us understand the problem. What is going on with this article?

BigQuery | bq コマンド でパーティション情報を保ったままカラム名を変更する

More than 1 year has passed since last update.
  • パーティションの単位ごとに、一時テーブルに対して、元テーブルのデータを流し込む。
  • 全単位のコピーが終わったら、元テーブルを一時テーブルで置き換える。 ( bq mv 的なコマンドがないため、 bq cp bq rm などで代用する )

コマンド例

bq query でクエリを発行して、その結果を一時テーブルに流し込んでいく。

bq query \
  --allow_large_results \
  --use_legacy_sql=false
  --time_partitioning_type=DAY \
  --destination_table=dataset.temporary_table\$20180401 \
    "SELECT * EXCEPT (original_column_name), original_column_name AS replaced_column_name FROM dataset.original_table WHERE _PARTITIONTIME = '2018-04-01"

これを日付分繰り返す。

シェルで無理やり日付分を回した例

雑いので参考にする場合はご注意。

sql=$(cat << EOS
SELECT
 CAST(DATE(_PARTITIONTIME) AS STRING) AS date
FROM
  dataset.original_table
GROUP BY
  DATE
ORDER BY
  DATE ASC
EOS
)

sql=$(echo "$sql" | tr '\n' ' ')

bq query "$sql" | grep -E "[0-9]" | sed "s/|//g" | sed "s/ //g" > ./table_list.tmp
while read date; do
  partition_decorator=$(echo $date | tr -d "-")
  table=dataset.original_table\$"${partition_decorator}"
  echo "$table"
  bq query --allow_large_results --use_legacy_sql=false --replace --time_partitioning_type=DAY \
    --destination_table=dataset.temporary_table\$"$partition_decorator" \
    "SELECT * EXCEPT (original_column_name), original_column_name AS replaced_column_name FROM dataset.original_table WHERE _PARTITIONTIME = '${date}'"
done <table_list.tmp

rm table_list.tmp

環境

  • BigQuery CLI 2.0.30
YumaInaura
Ruby on Rails 業務経験 約5年 / Perl PHP Python Golang Linux Apache MySQL BigQuery Jenkins ansible AWS など / いなうらゆうま / YumaInaura / 稲浦悠馬
http://twitter.com/yumainaura
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした