bq ツールを使用して一般公開データセットにクエリを実行する

bq コマンドライン ツールを使用して一般公開データセットを調べてクエリを実行する方法を学習します。

始める前に

  1. In the Trusted Cloud console, on the project selector page, select or create a Trusted Cloud project.

    Go to project selector

  2. Verify that billing is enabled for your Trusted Cloud project.

  3. このチュートリアルで使用する Trusted Cloud プロジェクトの課金を有効にしない場合は、BigQuery サンドボックスでデータを操作します。BigQuery サンドボックスを使用すると、一部の BigQuery 機能を無料で使用して、BigQuery を試すことができます。

  4. BigQuery API が有効になっていることを確認します。

    API を有効にする

    新しいプロジェクトを作成している場合は、BigQuery API が自動的に有効になっています。

  5. In the Trusted Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Trusted Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  6. 一般公開データセットを調べる

    BigQuery には、クエリで使用可能な bigquery-public-data.samples データセットのサンプル テーブルがいくつか用意されています。このチュートリアルでは、shakespeare テーブルに対してクエリを実行します。このテーブルには、シェイクスピア劇のすべての単語のエントリが含まれています。

    samples データセットの shakespeare テーブルを調べます。

    bq show bigquery-public-data:samples.shakespeare
    

    出力は次のようになります。出力を簡素化するために、一部の列は省略されています。

      Last modified                  Schema                 Total Rows   Total Bytes
    ----------------- ------------------------------------ ------------ ------------
     14 Mar 17:16:45   |- word: string (required)           164656       6432064
                       |- word_count: integer (required)
                       |- corpus: string (required)
                       |- corpus_date: integer (required)
    

    一般公開データセットに対してクエリを実行する

    bq query コマンドを使用して、データに対して SQL クエリを実行します。

    1. シェイクスピアの作品に出現する部分文字列 raisin の回数を指定します。

      bq query --use_legacy_sql=false \
          'SELECT
            word,
            SUM(word_count) AS count
          FROM
            `bigquery-public-data.samples.shakespeare`
          WHERE
            word LIKE "%raisin%"
          GROUP BY
            word;'
      

      出力は次のようになります。

      +---------------+-------+
      |     word      | count |
      +---------------+-------+
      | praising      |     8 |
      | Praising      |     4 |
      | raising       |     5 |
      | dispraising   |     2 |
      | dispraisingly |     1 |
      | raisins       |     1 |
      +---------------+-------+
      
    2. シェイクスピアの作品で部分文字列 huzzah を検索します。

      bq query --use_legacy_sql=false \
          'SELECT
            word
          FROM
            `bigquery-public-data.samples.shakespeare`
          WHERE
            word = "huzzah";'
      

      この部分文字列はシェイクスピアの作品には出現しないため、結果は返されません。

    クリーンアップ

    このページで使用したリソースについて、 Trusted Cloud アカウントに課金されないようにするには、リソースを含む Trusted Cloud プロジェクトを削除します。

    プロジェクトの削除

    BigQuery サンドボックスを使用して一般公開データセットにクエリを実行した場合、プロジェクトに対する課金は有効になりません。

    課金をなくす最も簡単な方法は、チュートリアル用に作成したプロジェクトを削除することです。

    プロジェクトを削除するには:

    1. In the Trusted Cloud console, go to the Manage resources page.

      Go to Manage resources

    2. In the project list, select the project that you want to delete, and then click Delete.
    3. In the dialog, type the project ID, and then click Shut down to delete the project.

    次のステップ