Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

@YumaInaura

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

  • パーティションの単位ごとに、一時テーブルに対して、元テーブルのデータを流し込む。
  • 全単位のコピーが終わったら、元テーブルを一時テーブルで置き換える。 ( 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
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
1
Help us understand the problem. What are the problem?