使用 BigQuery C# 用戶端程式庫查詢公開資料集

使用 BigQuery C# 用戶端程式庫查詢公開資料集

您可以瞭解如何執行下列作業:

  1. 在 Google Cloud 專案啟用 Cloud Shell。
  2. 開啟 Cloud Shell 編輯器。
  3. 準備查詢所需的檔案。
  4. 在 BigQuery 中查詢公開資料集。
  5. 清除所用資源。

預計所需時間:

點選「Start」(啟動) 即可開始。

在 Google Cloud 專案啟用 Cloud Shell

  1. 如果專案未啟用計費功能,會自動前往 BigQuery 沙箱讓您執行操作。沙箱可供您免付費使用部分 BigQuery 功能,協助熟悉 BigQuery。如果您只打算將專案用於本文練習,建議使用 BigQuery 沙箱。

  2. 點選「Activate Cloud Shell」(啟用 Cloud Shell)操作示範

接下來將說明如何開啟 Cloud Shell 編輯器,請點選「Next」(下一步)

開啟 Cloud Shell 編輯器

  1. 在 Cloud Shell 中,建立新的 C# 專案和檔案:

    dotnet new console -n BigQueryCsharpDemo
    

    輸出結果大致如下。為簡化輸出結果,這裡省略了幾行內容。

    Welcome to .NET 6.0!
    ---------------------
    SDK Version: 6.0.407
    ...
    The template "Console App" was created successfully.
    ...
    

    這個指令會建立名為 BigQueryCsharpDemo 的 C# 專案和 Program.cs 檔案。

  2. 開啟 Cloud Shell 編輯器:

    cloudshell workspace BigQueryCsharpDemo
    

接下來要準備查詢所需的檔案,請點選「Next」(下一步)

準備查詢所需檔案

  1. 接著要在 Cloud Shell 編輯器中開啟終端機,請點選「Open Terminal」(開啟終端機)

  2. 開啟專案目錄:

    cd BigQueryCsharpDemo
    
  3. 安裝 C# 專用的 BigQuery 用戶端程式庫:

    dotnet add package Google.Cloud.BigQuery.V2
    

    輸出結果大致如下。為簡化輸出結果,這裡省略了幾行內容。

    Determining projects to restore...
    Writing /tmp/tmpF7EKSd.tmp
    ...
    info : Writing assets file to disk.
    ...
    

接下來將說明如何在 BigQuery 中查詢公開資料集,請點選「Next」(下一步)

在 BigQuery 中查詢公開資料集

  1. 將變數 GOOGLE_PROJECT_ID 設成 GOOGLE_CLOUD_PROJECT 這個值,然後匯出變數:

    export GOOGLE_PROJECT_ID=$GOOGLE_CLOUD_PROJECT
    
  2. 點選「Open Editor」(開啟編輯器)

  3. 在「Explorer」窗格中,找出 BIGQUERYCSHARPDEMO 專案。

  4. 點選 Program.cs 檔案即可開啟。

  5. 如要依據 bigquery-public-data.stackoverflow 資料集建立查詢,讓系統傳回瀏覽量最高的前 10 個 Stack Overflow 頁面和相應瀏覽次數,請將檔案內容改成下列程式碼:

    
    using System;
    using Google.Cloud.BigQuery.V2;
    
    namespace GoogleCloudSamples
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                string projectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");
                var client = BigQueryClient.Create(projectId);
                string query = @"SELECT
                    CONCAT(
                        'https://stackoverflow.com/questions/',
                        CAST(id as STRING)) as url, view_count
                    FROM `bigquery-public-data.stackoverflow.posts_questions`
                    WHERE tags like '%google-bigquery%'
                    ORDER BY view_count DESC
                    LIMIT 10";
                var result = client.ExecuteQuery(query, parameters: null);
                Console.Write("\nQuery Results:\n------------\n");
                foreach (var row in result)
                {
                    Console.WriteLine($"{row["url"]}: {row["view_count"]} views");
                }
            }
        }
    }
    

  6. 點選「Open Terminal」(開啟終端機)

  7. 在終端機中執行 Program.cs 指令碼。如果系統提示您授權 Cloud Shell 並同意條款,請點選「Authorize」(授權)

    dotnet run
    

    結果大致如下:

    Query Results:
    ------------
    https://stackoverflow.com/questions/35159967: 170023 views
    https://stackoverflow.com/questions/22879669: 142581 views
    https://stackoverflow.com/questions/10604135: 132406 views
    https://stackoverflow.com/questions/44564887: 128781 views
    https://stackoverflow.com/questions/27060396: 127008 views
    https://stackoverflow.com/questions/12482637: 120766 views
    https://stackoverflow.com/questions/20673986: 115720 views
    https://stackoverflow.com/questions/39109817: 108368 views
    https://stackoverflow.com/questions/11057219: 105175 views
    https://stackoverflow.com/questions/43195143: 101878 views
    

您已成功使用 BigQuery C# 用戶端程式庫查詢公開資料集。

如要查看後續步驟,並避免系統向您的帳戶收費,請點選「Next」(下一步)

後續步驟

保留已建立的資源並運用 BigQuery 執行更多工作,或是清除所用資源,避免產生帳單費用。

運用 BigQuery 執行更多工作

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收費,請刪除 Google Cloud 專案,或刪除您在本逐步操作說明課程中建立的資源。

刪除專案

如果您為了學習 BigQuery 而建立新專案,但現在不再需要,請刪除專案。請注意,刪除專案也會一併刪除當中的內容,自訂專案 ID 也會消失。

刪除資源

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

  1. 在 Cloud Shell 中,移至上一層目錄:

    cd ..
    
  2. 刪除您建立的資源:

    rm -R BigQueryCsharpDemo
    

    -R 旗標會刪除資料夾中的所有資產。