BigQuery は、クエリ対象として一般公開できるいくつかの一般公開データセットをホストしています。このチュートリアルでは、複数の BigQuery クエリジョブを同時に実行するワークフローを作成して、ジョブを順次実行する場合に比べてパフォーマンスが向上していることを示します。
目標
このチュートリアルの内容は次のとおりです。- Wikipedia 一般公開データセットに対してクエリを実行し、特定の月における閲覧回数が最も多いタイトルを特定します。
- 複数の BigQuery クエリジョブを順次実行するワークフローをデプロイして実行します。
- 並列イテレーションを使用して BigQuery ジョブを実行するワークフローをデプロイして実行し、通常の
for
ループが並列実行されます。
Google Cloud コンソールで次のコマンドを実行するか、ターミナルまたは Cloud Shell で Google Cloud CLI を使用できます。
費用
このドキュメントでは、Google Cloud の次の課金対象のコンポーネントを使用します。
料金計算ツールを使うと、予想使用量に基づいて費用の見積もりを生成できます。
始める前に
組織で定義されているセキュリティの制約により、次の手順を完了できない場合があります。トラブルシューティング情報については、制約のある Google Cloud 環境でアプリケーションを開発するをご覧ください。
コンソール
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
Google Cloud Console の [プロジェクト セレクタ] ページで、Google Cloud プロジェクトを選択または作成します。
-
Workflows API を有効にします。
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
Grant the following roles to the service account: BigQuery > BigQuery Job User, Logging > Logs Writer.
To grant a role, find the Select a role list, then select the role.
To grant additional roles, click
Add another role and add each additional role. - Click Continue.
-
Click Done to finish creating the service account.
-
-
Google Cloud Console の [プロジェクト セレクタ] ページで、Google Cloud プロジェクトを選択または作成します。
-
Workflows API を有効にします。
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
Grant the following roles to the service account: BigQuery > BigQuery Job User, Logging > Logs Writer.
To grant a role, find the Select a role list, then select the role.
To grant additional roles, click
Add another role and add each additional role. - Click Continue.
-
Click Done to finish creating the service account.
-
gcloud
-
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
- Google Cloud CLI をインストールします。
-
gcloud CLI を初期化するには:
gcloud init
-
Google Cloud プロジェクトを作成または選択します。
-
Google Cloud プロジェクトを作成します。
gcloud projects create PROJECT_ID
PROJECT_ID
は、作成する Google Cloud プロジェクトの名前に置き換えます。 -
作成した Google Cloud プロジェクトを選択します。
gcloud config set project PROJECT_ID
PROJECT_ID
は、実際の Google Cloud プロジェクト名に置き換えます。
-
-
Workflows API を有効にします。
gcloud services enable workflows.googleapis.com
-
Set up authentication:
-
Create the service account:
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
Replace
SERVICE_ACCOUNT_NAME
with a name for the service account. -
Grant roles to the service account. Run the following command once for each of the following IAM roles:
roles/bigquery.jobUser, roles/logging.logWriter
:gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=ROLE
Replace the following:
SERVICE_ACCOUNT_NAME
: the name of the service accountPROJECT_ID
: the project ID where you created the service accountROLE
: the role to grant
-
- Google Cloud CLI をインストールします。
-
gcloud CLI を初期化するには:
gcloud init
-
Google Cloud プロジェクトを作成または選択します。
-
Google Cloud プロジェクトを作成します。
gcloud projects create PROJECT_ID
PROJECT_ID
は、作成する Google Cloud プロジェクトの名前に置き換えます。 -
作成した Google Cloud プロジェクトを選択します。
gcloud config set project PROJECT_ID
PROJECT_ID
は、実際の Google Cloud プロジェクト名に置き換えます。
-
-
Workflows API を有効にします。
gcloud services enable workflows.googleapis.com
-
Set up authentication:
-
Create the service account:
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
Replace
SERVICE_ACCOUNT_NAME
with a name for the service account. -
Grant roles to the service account. Run the following command once for each of the following IAM roles:
roles/bigquery.jobUser, roles/logging.logWriter
:gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=ROLE
Replace the following:
SERVICE_ACCOUNT_NAME
: the name of the service accountPROJECT_ID
: the project ID where you created the service accountROLE
: the role to grant
-
BigQuery クエリジョブを実行する
BigQuery では、インタラクティブ(オンデマンド)クエリジョブを実行できます。詳細については、インタラクティブ クエリとバッチクエリのジョブの実行をご覧ください。
コンソール
Google Cloud コンソールで [BigQuery] ページに移動します。
[クエリエディタ] テキスト領域に、次の BigQuery SQL クエリを入力します。
SELECT TITLE, SUM(views) FROM `bigquery-samples.wikipedia_pageviews.201207h` GROUP BY TITLE ORDER BY SUM(views) DESC LIMIT 100
[実行] をクリックします。
bq
ターミナルで、次の bq query
コマンドを入力して、標準 SQL 構文を使用してインタラクティブ クエリを実行します。
bq query \
--use_legacy_sql=false \
'SELECT
TITLE, SUM(views)
FROM
`bigquery-samples.wikipedia_pageviews.201207h`
GROUP BY
TITLE
ORDER BY
SUM(views) DESC
LIMIT 100'
特定の月のビューが最も多い上位 100 件の Wikipedia タイトルを返し、出力を一時テーブルに書き込むクエリを実行します。
クエリの実行に要した時間をメモします。
複数のクエリを順次実行するワークフローをデプロイする
ワークフロー定義は、ワークフロー構文を使用して説明した一連のステップで構成されています。ワークフローを作成したら、デプロイして実行できるようにします。デプロイの手順では、ソースファイルを実行できることも検証されます。
次のワークフローでは、Workflows の BigQuery コネクタを使用して、クエリを実行する 5 つのテーブルのリストを定義しています。クエリは順次実行され、各テーブルで最も閲覧されたタイトルが結果マップに保存されます。
コンソール
Google Cloud コンソールの [ワークフロー] ページに移動します。
[作成] をクリックします。
新しいワークフローの名前を入力します(例:
workflow-serial-bqjobs
)。適切なリージョンを選択します(例: us-central1)。
先ほど作成したサービス アカウントを選択します。
BigQuery > BigQuery ジョブユーザーと Logging > ログ書き込み IAM ロールの両方がすでにサービス アカウントに付与されている必要があります。
[Next] をクリックします。
ワークフロー エディタで、次のワークフローの定義を入力します。
[Deploy] をクリックします。
gcloud
ターミナルを開いて、ワークフローのソースコード ファイルを作成します。
touch workflow-serial-bqjobs.yaml
次のワークフローをソースコード ファイルにコピーします。
次のコマンドを入力してワークフローをデプロイします。
gcloud workflows deploy workflow-serial-bqjobs \ --source=workflow-serial-bqjobs.yaml \ --service-account=MY_SERVICE_ACCOUNT@MY_PROJECT.IAM.GSERVICEACCOUNT.COM
MY_SERVICE_ACCOUNT@MY_PROJECT.IAM.GSERVICEACCOUNT.COM
は、先ほど作成したサービス アカウントのメールアドレスに置き換えます。すでにサービス アカウントに
roles/bigquery.jobUser
とroles/logging.logWriter
の両方の IAM ロールを付与している必要があります。
ワークフローを実行し、複数のクエリを順次実行する
ワークフローを実行すると、そのワークフローに関連付けられた現在のワークフロー定義が実行されます。
コンソール
Google Cloud コンソールの [ワークフロー] ページに移動します。
[Workflows] ページで、[workflow-serial-bqjobs] ワークフローを選択して、詳細ページに移動します。
[ワークフローの詳細] ページで [play_arrow 実行] を選択します。
もう一度 [Execute] をクリックします。
ワークフローの結果が [出力] ペインに表示されます。
gcloud
ターミナルを開きます。
ワークフローを実行します。
gcloud workflows run workflow-serial-bqjob
ワークフローの実行には、約 1 分すなわち前回の実行時間の 5 倍ほどの時間を要します。結果には、各テーブルが含まれ、次のようになります。
{
"201201h": {
"title": "Special:Search",
"views": "14591339"
},
"201202h": {
"title": "Special:Search",
"views": "132765420"
},
"201203h": {
"title": "Special:Search",
"views": "123316818"
},
"201204h": {
"title": "Special:Search",
"views": "116830614"
},
"201205h": {
"title": "Special:Search",
"views": "131357063"
}
}
複数のクエリを並列に実行するワークフローをデプロイして実行する
5 つのクエリを順次実行する代わりに、いくつかの変更を行うことで並行してクエリを実行できます。
- runQueries:
parallel:
shared: [results]
for:
value: table
in: ${tables}
parallel
ステップを使用すると、for
ループの各イテレーションを並行して実行できます。results
変数はshared
として宣言されます。これにより、ブランチによる書き込みが可能になり、各ブランチの結果を追加できます。
コンソール
Google Cloud コンソールの [ワークフロー] ページに移動します。
[作成] をクリックします。
新しいワークフローの名前を入力します(例:
workflow-parallel-bqjobs
)。適切なリージョンを選択します(例: us-central1)。
先ほど作成したサービス アカウントを選択します。
[Next] をクリックします。
ワークフロー エディタで、次のワークフローの定義を入力します。
[Deploy] をクリックします。
[ワークフローの詳細] ページで [play_arrow 実行] を選択します。
もう一度 [Execute] をクリックします。
ワークフローの結果が [出力] ペインに表示されます。
gcloud
ターミナルを開いて、ワークフローのソースコード ファイルを作成します。
touch workflow-parallel-bqjobs.yaml
次のワークフローをソースコード ファイルにコピーします。
次のコマンドを入力してワークフローをデプロイします。
gcloud workflows deploy workflow-parallell-bqjobs \ --source=workflow-parallel-bqjobs.yaml \ --service-account=MY_SERVICE_ACCOUNT@MY_PROJECT.IAM.GSERVICEACCOUNT.COM
MY_SERVICE_ACCOUNT@MY_PROJECT.IAM.GSERVICEACCOUNT.COM
は、先ほど作成したサービス アカウントのメールアドレスに置き換えます。ワークフローを実行します。
gcloud workflows run workflow-serial-bqjob
結果は先ほどの出力と類似していますが、ワークフローの実行は約 20 秒以内に完了します。
クリーンアップ
このチュートリアル用に新規プロジェクトを作成した場合は、そのプロジェクトを削除します。既存のプロジェクトを使用し、このチュートリアルで変更を加えずに残す場合は、チュートリアル用に作成したリソースを削除します。
プロジェクトの削除
課金をなくす最も簡単な方法は、チュートリアル用に作成したプロジェクトを削除することです。
プロジェクトを削除するには:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
チュートリアル リソースの削除
このチュートリアルで作成したワークフローを削除します。
gcloud workflows delete WORKFLOW_NAME
次のステップ
- 並列ステップの詳細については、並列ステップの実行をご覧ください。
- Workflows コネクタの詳細については、コネクタについてをご覧ください。
- Workflows について詳しくは、Workflows の概要をご覧ください。