BigQuery データを別プロジェクトに移行する最小手順

今回、むかーしから使っているGoogleアカウントから法人のGoogleアカウントに変えまして、その際GCPまわりをいろいろ移行したんですが、BigQueryも移行したので、その時の手順になります。
ざっとした内容はGA4のデータと、システム側の統計データを突合したものをBQで扱っていて、それの移行でした。

GCP プロジェクト(以下、A)から別のプロジェクト(以下、B)へ
BigQuery テーブルを安全かつシンプルに移行する 手順です。

今回は一時的に Google Cloud Storage (GCS) を経由して
CSV ファイルとしてエクスポート → 別アカウントで再インポート します。

役割 アカウント例 プロジェクトID 用途
A側(旧) old-admin@example.com old-pjt-xxxx エクスポート元
B側(新) new-admin@example.com new-pjt-yyyy インポート先

BigQuery(旧PJT)
↓ bq extract
Google Cloud Storage(CSV)
↓ gsutil iam 一時公開
BigQuery(新PJT)
↓ bq load
Google Cloud Storage(非公開に戻す)

① アカウント確認

② BigQuery テーブルを CSV でエクスポート

bq extract --destination_format=CSV old-pjt-xxxx:ga4.init gs://my-export-bucket/ga4_init_*.csv
bq extract --destination_format=CSV old-pjt-xxxx:base.ana gs://my-export-bucket/base_ana_*.csv

出力後の確認

gsutil ls gs://my-export-bucket/

③ 一時的に全公開(objectViewer)

gsutil iam ch allUsers:objectViewer gs://my-export-bucket

① アカウント切り替え

gcloud config set account new-admin@example.com

②BigQuery にデータセットを作成

bq mk new-pjt-yyyy:ga4
bq mk new-pjt-yyyy:base

③CSV ファイルをインポート

bq load --source_format=CSV --autodetect new-pjt-yyyy:ga4.init gs://my-export-bucket/ga4_init_*.csv
bq load --source_format=CSV --autodetect new-pjt-yyyy:base.ana gs://my-export-bucket/base_ana_*.csv

④テーブル確認

bq show new-pjt-yyyy:ga4.init
bq show new-pjt-yyyy:base.ana

① アカウント戻し

gcloud config set account old-admin@example.com
gcloud auth list

② 公開権限の削除

gsutil iam ch -d allUsers:objectViewer gs://my-export-bucket

③ 現在の IAM 設定確認

gsutil iam get gs://my-export-bucket

今回は、そこまでの容量ではないのでこうしました。ビックリするほどビックデータの場合は、コスト面要注意です。


元の記事を確認する

関連記事