使用 bq 工具載入及查詢資料

瞭解如何透過 bq 指令列工具建立資料集、載入範例資料,以及查詢資料表。


如要直接在 Google Cloud 控制台按照逐步指南操作,請按一下「Guide me」(逐步引導)

逐步引導


事前準備

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  4. Verify that billing is enabled for your Google Cloud project.

  5. 如果在本教學課程中使用的 Google Cloud 專案未啟用計費功能,您需要在 BigQuery 沙箱中載入及查詢資料。BigQuery 沙箱可讓您學習 BigQuery,但可免費使用的 BigQuery 功能有限。

  6. 確認已啟用 BigQuery API。

    啟用 API

    如果您建立新專案,系統會自動啟用 BigQuery API。

  7. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google 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.

  8. 下載含有來源資料的檔案

    您下載的檔案包含約 7 MB 的熱門新生兒命名資料,這項資料是由美國社會安全局提供。

    如要進一步瞭解資料,請參閱美國社會安全局的熱門名字的背景資訊

    1. 在新的瀏覽器分頁中開啟下列網址,下載美國社會安全局的資料:

      https://www.ssa.gov/OACT/babynames/names.zip
      
    2. 將檔案解壓縮。

      如要進一步瞭解資料集結構定義,請參閱解壓縮的 NationalReadMe.pdf 檔案。

    3. 如要查看資料樣貌,請開啟 yob2024.txt 檔案。這個逗號分隔值檔案內含名字、出生時判定的性別,以及同名的新生兒人數,這個檔案沒有標題列。

    4. 將該檔案移到工作目錄。

      • 如果使用 Cloud Shell,請依序點選 「More」(顯示更多項目) >「Upload」(上傳),按一下「Choose Files」(選擇檔案) 後再選取 yob2024.txt 檔案,最後點選「Upload」(上傳)

      • 如果使用本機殼層,請將 yob2024.txt 檔案複製或移到執行 bq 工具的目錄。

    建立資料集

    1. 如果您是從說明文件啟動 Cloud Shell,請輸入下列指令來設定專案 ID。這樣一來,您就不必在每個 CLI 指令中指定專案 ID。

      gcloud config set project PROJECT_ID
      

      PROJECT_ID 替換為您的專案 ID。

    2. 輸入下列指令,建立資料集 babynames

      bq mk --dataset babynames
      

      輸出結果大致如下:

      Dataset 'babynames' successfully created.
      
    3. 確認資料集 babynames 已顯示於專案:

      bq ls --datasets=true
      

      輸出結果會與下列內容相似:

        datasetId
      -------------
        babynames
      

    將資料載入資料表

    1. babynames 資料集中,將來源檔案 yob2024.txt 載入新資料表 names2024

      bq load babynames.names2024 yob2024.txt name:string,assigned_sex_at_birth:string,count:integer
      

      輸出結果大致如下:

      Upload complete.
      Waiting on bqjob_r3c045d7cbe5ca6d2_0000018292f0815f_1 ... (1s) Current status: DONE
      
    2. 確認資料表 names2024 已顯示於資料集 babynames

      bq ls --format=pretty babynames
      

      輸出結果大致如下。某些資料欄會省略,用以簡化輸出內容。

      +-----------+-------+
      |  tableId  | Type  |
      +-----------+-------+
      | names2024 | TABLE |
      +-----------+-------+
      
    3. 確認新資料表 names2024 的結構定義為 name: stringassigned_sex_at_birth: stringcount: integer

      bq show babynames.names2024
      

      輸出結果大致如下。某些資料欄會省略,用以簡化輸出內容。

        Last modified        Schema                      Total Rows   Total Bytes
      ----------------- ------------------------------- ------------ ------------
      14 Mar 17:16:45   |- name: string                    31904       607494
                        |- assigned_sex_at_birth: string
                        |- count: integer
      

    查詢資料表資料

    1. 判定資料中最常見的女生名字:

      bq query \
          'SELECT
            name,
            count
          FROM
            babynames.names2024
          WHERE
            assigned_sex_at_birth = "F"
          ORDER BY
            count DESC
          LIMIT 5'
      

      輸出結果大致如下:

      +-----------+-------+
      |   name    | count |
      +-----------+-------+
      | Olivia    | 14718 |
      | Emma      | 13485 |
      | Amelia    | 12740 |
      | Charlotte | 12552 |
      | Mia       | 12113 |
      +-----------+-------+
      
    2. 判定資料中最少見的男生名字:

      bq query \
          'SELECT
            name,
            count
          FROM
            babynames.names2024
          WHERE
            assigned_sex_at_birth = "M"
          ORDER BY
            count ASC
          LIMIT 5'
      

      輸出結果大致如下:

      +---------+-------+
      |  name   | count |
      +---------+-------+
      | Aaran   |     5 |
      | Aadiv   |     5 |
      | Aadarsh |     5 |
      | Aarash  |     5 |
      | Aadrik  |     5 |
      +---------+-------+
      

      來源資料會省略出現少於 5 次的名字,因此最少次數是 5。

    清除所用資源

    如要避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請刪除含有這些資源的 Google Cloud 專案。

    刪除專案

    如果您使用 BigQuery 沙箱查詢公開資料集,則專案不會啟用帳單功能,因此您不需要刪除專案。

    如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。

    如要刪除專案:

    1. In the Google 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.

    刪除資源

    如果使用現有專案,請刪除稍早建立的資源:

    1. 刪除資料集 babynames

      bq rm --recursive=true babynames
      

      旗標 --recursive 會刪除資料集內的所有資料表,包括資料表 names2024

      輸出結果大致如下:

      rm: remove dataset 'myproject:babynames'? (y/N)
      
    2. 輸入 y 來確認刪除指令。

    後續步驟