在筆記本中探索查詢結果


您可以在 BigQuery 中使用 Colab Enterprise 筆記本,探索 BigQuery 查詢結果。

在本教學課程中,您將查詢 BigQuery 公開資料集中的資料,並在筆記本中探索查詢結果。

目標

  • 在 BigQuery 中建立及執行查詢。
  • 在筆記本中探索查詢結果。

費用

本教學課程使用Google Cloud 公開資料集計畫提供的資料集。這些資料集的儲存空間費用由 Google 支付,Google 也將這些資料集提供給大眾存取。您需要支付資料查詢費用 詳情請參閱 BigQuery 定價

事前準備

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

    Go to project selector

  2. Make sure that billing is enabled for your Google Cloud project.

  3. Enable the BigQuery API.

    Enable the API

    新專案會自動啟用 BigQuery。

設定程式碼資產的預設區域

如果您是第一次建立程式碼資產,請設定程式碼資產的預設區域。程式碼資產建立後,就無法變更區域。

BigQuery Studio 中的所有程式碼資產都使用相同的預設區域。如要設定程式碼資產的預設區域,請按照下列步驟操作:

  1. 前往「BigQuery」頁面

    前往 BigQuery

  2. 在「Explorer」窗格中,找出已啟用程式碼資產的專案。

  3. 按一下專案旁的 「查看動作」,然後按一下「變更預設程式碼區域」

  4. 「區域」請選取要用於程式碼資產的區域。

  5. 按一下 [選取]。

如需可用區域清單,請參閱 BigQuery Studio 位置

所需權限

如要建立及執行 Notebook,您需要下列 Identity and Access Management (IAM) 角色:

在筆記本中開啟查詢結果

您可以執行 SQL 查詢,然後使用筆記本探索資料。如果您想先修改 BigQuery 中的資料再進行處理,或是只需要表格中的部分欄位,這個方法就非常實用。

  1. 前往 Google Cloud 控制台的「BigQuery」頁面。

    前往 BigQuery

  2. 在「Type to search」(輸入要搜尋的字詞) 欄位中輸入 bigquery-public-data

    如果未顯示該專案,請在搜尋欄位中輸入 bigquery,然後按一下「Search to all projects」(將搜尋範圍擴及所有專案),將搜尋字串與現有專案進行比對。

  3. 依序選取「bigquery-public-data」>「ml_datasets」>「penguins」

  4. 針對 penguins 表格,依序按一下 「View actions」(查看動作) 和「Query」(查詢)

  5. 在產生的查詢中加入星號 (*) 以選取欄位,如下列範例所示:

    SELECT * FROM `bigquery-public-data.ml_datasets.penguins` LIMIT 1000;
  6. 按一下「執行」

  7. 在「查詢結果」部分,依序點選「探索資料」和「使用 Python 筆記本探索」

準備使用筆記本

連線至執行階段並設定應用程式預設值,準備使用筆記本。

  1. 在筆記本標頭中,按一下「連線」連線至預設執行階段
  2. 在「設定」程式碼區塊中,按一下 「執行儲存格」

探索資料

  1. 如要將 penguins 資料載入 BigQuery DataFrame 並顯示結果,請按一下「Result set loaded from BigQuery job as a DataFrame」(從 BigQuery 工作載入的結果集做為 DataFrame) 區塊中程式碼區塊的「Run cell」(執行儲存格)
  2. 如要取得資料的描述性指標,請在「Show descriptive statistics using describe()」(使用 describe() 顯示描述性統計資料) 部分的程式碼區塊中,點選「Run cell」(執行儲存格)
  3. 選用:使用其他 Python 函式或套件探索及分析資料。

下列程式碼範例顯示如何使用 bigframes.pandas 分析資料,以及如何使用 bigframes.ml 從 BigQuery DataFrame 中的 penguins 資料建立線性迴歸模型:

import bigframes.pandas as bpd

# Load data from BigQuery
query_or_table = "bigquery-public-data.ml_datasets.penguins"
bq_df = bpd.read_gbq(query_or_table)

# Inspect one of the columns (or series) of the DataFrame:
bq_df["body_mass_g"]

# Compute the mean of this series:
average_body_mass = bq_df["body_mass_g"].mean()
print(f"average_body_mass: {average_body_mass}")

# Find the heaviest species using the groupby operation to calculate the
# mean body_mass_g:
(
    bq_df["body_mass_g"]
    .groupby(by=bq_df["species"])
    .mean()
    .sort_values(ascending=False)
    .head(10)
)

# Create the Linear Regression model
from bigframes.ml.linear_model import LinearRegression

# Filter down to the data we want to analyze
adelie_data = bq_df[bq_df.species == "Adelie Penguin (Pygoscelis adeliae)"]

# Drop the columns we don't care about
adelie_data = adelie_data.drop(columns=["species"])

# Drop rows with nulls to get our training data
training_data = adelie_data.dropna()

# Pick feature columns and label column
X = training_data[
    [
        "island",
        "culmen_length_mm",
        "culmen_depth_mm",
        "flipper_length_mm",
        "sex",
    ]
]
y = training_data[["body_mass_g"]]

model = LinearRegression(fit_intercept=False)
model.fit(X, y)
model.score(X, y)

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的相關費用,請刪除含有該項資源的專案,或者保留專案但刪除個別資源。

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

  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.

後續步驟