使用 BigQuery 用戶端程式庫查詢公開資料集
瞭解如何使用 BigQuery 用戶端程式庫查詢公開資料集。
如要直接在Google Cloud 控制台按照逐步指南操作,請選取偏好的程式設計語言:
C#
Go
Java
Node.js
PHP
Python
Ruby
事前準備
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
選擇免費使用 BigQuery 沙箱,或為專案啟用計費功能。 Google Cloud
如果專案未啟用計費功能,會自動前往 BigQuery 沙箱讓您執行操作。沙箱可供您免付費使用部分 BigQuery 功能,藉此熟悉 BigQuery。如果您只打算將專案用於本文的練習,建議使用 BigQuery 沙箱。
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/serviceusage.serviceUsageAdmin, roles/bigquery.jobUser
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
- Replace
PROJECT_ID
with your project ID. -
Replace
USER_IDENTIFIER
with the identifier for your user account. For example,user:myemail@example.com
. - Replace
ROLE
with each individual role.
- Replace
-
Enable the BigQuery API:
gcloud services enable bigquery
新專案會自動啟用 BigQuery API。
-
In the Google Cloud console, activate Cloud Shell.
在 Cloud Shell 中啟用 Google Cloud 專案:
gcloud config set project PROJECT_ID
將 PROJECT_ID 替換為您為本逐步導覽選取的專案。
輸出結果會與下列內容相似:
Updated property [core/project].
在 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
檔案。開啟 Cloud Shell 編輯器:
cloudshell workspace BigQueryCsharpDemo
如要在 Cloud Shell 編輯器中開啟終端機,請點選「Open Terminal」(開啟終端機)。
開啟專案目錄:
cd BigQueryCsharpDemo
安裝 C# 專用的 BigQuery 用戶端程式庫:
dotnet add package Google.Cloud.BigQuery.V2
輸出結果大致如下。為簡化輸出結果,這裡省略了幾行內容。
Determining projects to restore... Writing /tmp/tmpF7EKSd.tmp ... info : Writing assets file to disk. ...
將變數
GOOGLE_PROJECT_ID
設成GOOGLE_CLOUD_PROJECT
這個值,然後匯出變數:export GOOGLE_PROJECT_ID=$GOOGLE_CLOUD_PROJECT
點選「Open Editor」(開啟編輯器)。
在「Explorer」窗格中,找出
BIGQUERYCSHARPDEMO
專案。點選
Program.cs
檔案即可開啟。如要依據
bigquery-public-data.stackoverflow
資料集建立查詢,讓系統傳回瀏覽量最高的前 10 個 Stack Overflow 頁面和相應瀏覽次數,請將檔案內容改成下列程式碼:點選「Open Terminal」(開啟終端機)。
在終端機中執行
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
在 Cloud Shell 中,建立新的 Go 專案和檔案:
mkdir bigquery-go-quickstart \ && touch \ bigquery-go-quickstart/app.go
這個指令會建立名為
bigquery-go-quickstart
的 Go 專案及名為app.go
的檔案。開啟 Cloud Shell 編輯器:
cloudshell workspace bigquery-go-quickstart
如要在 Cloud Shell 編輯器中開啟終端機,請點選「Open Terminal」(開啟終端機)。
開啟專案目錄:
cd bigquery-go-quickstart
建立
go.mod
檔案:go mod init quickstart
輸出結果會與下列內容相似:
go: creating new go.mod: module quickstart go: to add module requirements and sums: go mod tidy
安裝 Go 專用的 BigQuery 用戶端程式庫:
go get cloud.google.com/go/bigquery
輸出結果會與下列內容相似:為簡化輸出結果,這裡省略了幾行內容。
go: downloading cloud.google.com/go/bigquery v1.49.0 go: downloading cloud.google.com/go v0.110.0 ... go: added cloud.google.com/go/bigquery v1.49.0 go: added cloud.google.com/go v0.110.0
點選「Open Editor」(開啟編輯器)。
在「Explorer」窗格中,找出
BIGQUERY-GO-QUICKSTART
專案。點選
app.go
,開啟這個檔案。如要依據
bigquery-public-data.stackoverflow
資料集建立查詢,讓系統傳回瀏覽量最高的前 10 個 Stack Overflow 頁面和相應瀏覽次數,請將下列程式碼複製到app.go
檔案中:點選「Open Terminal」(開啟終端機)。
在終端機中執行
app.go
指令碼。如果系統提示您授權 Cloud Shell 並同意條款,請按一下「Authorize」(授權)。go run app.go
結果大致如下:
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
在 Cloud Shell 中,使用 Apache Maven 建立新的 Java 專案:
mvn archetype:generate \ -DgroupId=com.google.app \ -DartifactId=bigquery-java-quickstart \ -DinteractiveMode=false
這個指令會建立名為
bigquery-java-quickstart
的 Maven 專案。輸出結果大致如下。為簡化輸出結果,這裡省略了幾行內容。
[INFO] Scanning for projects... ... [INFO] Building Maven Stub Project (No POM) 1 ... [INFO] BUILD SUCCESS ...
除了 Maven,還有許多依附元件管理系統可以使用。詳情請參閱相關說明,瞭解如何設定 Java 開發環境,以便與用戶端程式庫搭配使用。
重新命名 Maven 預設建立的
App.java
檔案:mv \ bigquery-java-quickstart/src/main/java/com/google/app/App.java \ bigquery-java-quickstart/src/main/java/com/google/app/SimpleApp.java
開啟 Cloud Shell 編輯器:
cloudshell workspace bigquery-java-quickstart
如果出現提示,詢問是否要同步處理 Java 類別路徑或設定,請點選「Always」(一律)。
若在本逐步操作說明課程中,未看到提示或未發生類別路徑相關錯誤,請按照下列步驟操作:
- 依序點選「File」>「Preferences」>「Open Settings (UI)」。
- 依序點選「Extensions」>「Java」。
- 捲動至「Configuration: Update Build Configuration」,然後選取「automatic」。
在「Explorer」窗格中,找出
BIGQUERY-JAVA-QUICKSTART
專案。點選
pom.xml
,開啟這個檔案。找到
<dependencies>
標記,在現有依附元件的後方新增下列依附元件。請勿替換任何現有的依附元件。<dependency> <groupId>com.google.cloud</groupId> <artifactId>google-cloud-bigquery</artifactId> </dependency>
在結尾標記
</dependencies>
的後面那行新增下列內容:<dependencyManagement> <dependencies> <dependency> <groupId>com.google.cloud</groupId> <artifactId>libraries-bom</artifactId> <version>26.1.5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
在「Explorer」窗格中,找到
BIGQUERY-JAVA-QUICKSTART
專案,然後依序點選「src」>「main/java/com/google/app」>「SimpleApp.java」。檔案會隨即開啟。如要針對
bigquery-public-data.stackoverflow
資料集建立查詢,請保留檔案的第一行package com.google.app;
,然後將其他內容改成下列程式碼:此查詢會傳回前 10 個最常觀看的 Stack Overflow 頁面及其觀看次數。
對「SimpleApp.java」按一下滑鼠右鍵,然後點選「Run Java」。如果系統提示您授權 Cloud Shell 並同意條款,請點選「Authorize」(授權)。
結果大致如下:
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
在 Cloud Shell 中,建立新的 Node.js 專案和檔案:
mkdir bigquery-node-quickstart \ && touch \ bigquery-node-quickstart/app.js
以下指令會建立名為
bigquery-node-quickstart
的 Node.js 專案和app.js
檔案。開啟 Cloud Shell 編輯器:
cloudshell workspace bigquery-node-quickstart
如要在 Cloud Shell 編輯器中開啟終端機,請點選「Open Terminal」(開啟終端機)。
開啟專案目錄:
cd bigquery-node-quickstart
安裝 Node.js 專用的 BigQuery 用戶端程式庫:
npm install @google-cloud/bigquery
輸出結果會與下列內容相似:
added 63 packages in 2s
點選「Open Editor」(開啟編輯器)。
在「Explorer」窗格中,找出
BIGQUERY-NODE-QUICKSTART
專案。點選
app.js
,開啟這個檔案。如要依據
bigquery-public-data.stackoverflow
資料集建立查詢,讓系統傳回瀏覽量最高的前 10 個 Stack Overflow 頁面和相應瀏覽次數,請將下列程式碼複製到app.js
檔案中:點選「Open Terminal」(開啟終端機)。
在終端機中執行
app.js
指令碼。如果系統提示您授權 Cloud Shell 並同意條款,請按一下「Authorize」(授權)。node app.js
結果大致如下:
Query Results: url: https://stackoverflow.com/questions/35159967, 170023 views url: https://stackoverflow.com/questions/22879669, 142581 views url: https://stackoverflow.com/questions/10604135, 132406 views url: https://stackoverflow.com/questions/44564887, 128781 views url: https://stackoverflow.com/questions/27060396, 127008 views url: https://stackoverflow.com/questions/12482637, 120766 views url: https://stackoverflow.com/questions/20673986, 115720 views url: https://stackoverflow.com/questions/39109817, 108368 views url: https://stackoverflow.com/questions/11057219, 105175 views url: https://stackoverflow.com/questions/43195143, 101878 views
在 Cloud Shell 中,建立新的 PHP 專案和檔案:
mkdir bigquery-php-quickstart \ && touch \ bigquery-php-quickstart/app.php
這個指令會建立名為
bigquery-php-quickstart
的 PHP 專案和app.php
檔案。開啟 Cloud Shell 編輯器:
cloudshell workspace bigquery-php-quickstart
如要在 Cloud Shell 編輯器中開啟終端機,請點選「Open Terminal」(開啟終端機)。
開啟專案目錄:
cd bigquery-php-quickstart
安裝 PHP 專用的 BigQuery 用戶端程式庫:
composer require google/cloud-bigquery
輸出內容大致如下。為簡化輸出結果,這裡省略了幾行內容。
Running composer update google/cloud-bigquery Loading composer repositories with package information Updating dependencies ... No security vulnerability advisories found Using version ^1.24 for google/cloud-bigquery
點選「Open Editor」(開啟編輯器)。
在「Explorer」窗格中,找出
BIGQUERY-PHP-QUICKSTART
專案。點選
app.php
,開啟這個檔案。如要依據
bigquery-public-data.stackoverflow
資料集建立查詢,讓系統傳回瀏覽量最高的前 10 個 Stack Overflow 頁面和相應瀏覽次數,請將下列程式碼複製到app.php
檔案中:點選「Open Terminal」(開啟終端機)。
在終端機中執行
app.php
指令碼。如果系統提示您授權 Cloud Shell 並同意條款,請按一下「Authorize」(授權)。php app.php
結果大致如下:
--- Row 1 --- url: https://stackoverflow.com/questions/35159967, 170023 views --- Row 2 --- url: https://stackoverflow.com/questions/22879669, 142581 views --- Row 3 --- url: https://stackoverflow.com/questions/10604135, 132406 views --- Row 4 --- url: https://stackoverflow.com/questions/44564887, 128781 views --- Row 5 --- url: https://stackoverflow.com/questions/27060396, 127008 views --- Row 6 --- url: https://stackoverflow.com/questions/12482637, 120766 views --- Row 7 --- url: https://stackoverflow.com/questions/20673986, 115720 views --- Row 8 --- url: https://stackoverflow.com/questions/39109817, 108368 views --- Row 9 --- url: https://stackoverflow.com/questions/11057219, 105175 views --- Row 10 --- url: https://stackoverflow.com/questions/43195143, 101878 views Found 10 row(s)
在 Cloud Shell 中,建立新的 Python 專案和檔案:
mkdir bigquery-python-quickstart \ && touch \ bigquery-python-quickstart/app.py
這個指令會建立名為
bigquery-python-quickstart
的 Python 專案和app.py
檔案。開啟 Cloud Shell 編輯器:
cloudshell workspace bigquery-python-quickstart
如要在 Cloud Shell 編輯器中開啟終端機,請點選「Open Terminal」(開啟終端機)。
開啟專案目錄:
cd bigquery-python-quickstart
安裝 Python 專用的 BigQuery 用戶端程式庫:
pip install --upgrade google-cloud-bigquery
系統會輸出類似如下內容。為簡化輸出結果,這裡省略了幾行內容。
Installing collected packages: google-cloud-bigquery ... Successfully installed google-cloud-bigquery-3.9.0 ...
點選「Open Editor」(開啟編輯器)。
在「Explorer」窗格中,找出
BIGQUERY-PYTHON-QUICKSTART
專案。點選
app.py
,開啟這個檔案。如要依據
bigquery-public-data.stackoverflow
資料集建立查詢,讓系統傳回瀏覽量最高的前 10 個 Stack Overflow 頁面和相應瀏覽次數,請將下列程式碼複製到app.py
檔案中:點選「Open Terminal」(開啟終端機)。
在終端機中執行
app.py
指令碼。如果系統提示您授權 Cloud Shell 並同意條款,請按一下「Authorize」(授權)。python app.py
結果大致如下:
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
在 Cloud Shell 中,建立新的 Ruby 專案和檔案:
mkdir bigquery-ruby-quickstart \ && touch \ bigquery-ruby-quickstart/app.rb
這個指令會建立名為
bigquery-ruby-quickstart
的 Ruby 專案和app.rb
檔案。開啟 Cloud Shell 編輯器:
cloudshell workspace bigquery-ruby-quickstart
如要在 Cloud Shell 編輯器中開啟終端機,請點選「Open Terminal」(開啟終端機)。
開啟專案目錄:
cd bigquery-ruby-quickstart
安裝 Ruby 專用的 BigQuery 用戶端程式庫:
gem install google-cloud-bigquery
輸出結果會與下列內容相似:為簡化輸出結果,這裡省略了幾行內容。
23 gems installed
點選「Open Editor」(開啟編輯器)。
在「Explorer」窗格中,找出
BIGQUERY-RUBY-QUICKSTART
專案。點選
app.rb
,開啟這個檔案。如要依據
bigquery-public-data.stackoverflow
資料集建立查詢,讓系統傳回瀏覽量最高的前 10 個 Stack Overflow 頁面和相應瀏覽次數,請將下列程式碼複製到app.rb
檔案中:點選「Open Terminal」(開啟終端機)。
在終端機中執行
app.rb
指令碼。如果系統提示您授權 Cloud Shell 並同意條款,請按一下「Authorize」(授權)。ruby app.rb
結果大致如下:
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
- 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.
在 Cloud Shell 中,移至上一層目錄:
cd ..
刪除您建立的
BigQueryCsharpDemo
資料夾:rm -R BigQueryCsharpDemo
-R
旗標會刪除資料夾中的所有資產。在 Cloud Shell 中,移至上一層目錄:
cd ..
刪除您建立的
bigquery-go-quickstart
資料夾:rm -R bigquery-go-quickstart
-R
旗標會刪除資料夾中的所有資產。在 Cloud Shell 中,移至上一層目錄:
cd ..
刪除您建立的
bigquery-java-quickstart
資料夾:rm -R bigquery-java-quickstart
-R
旗標會刪除資料夾中的所有資產。在 Cloud Shell 中,移至上一層目錄:
cd ..
刪除您建立的
bigquery-node-quickstart
資料夾:rm -R bigquery-node-quickstart
-R
旗標會刪除資料夾中的所有資產。在 Cloud Shell 中,移至上一層目錄:
cd ..
刪除您建立的
bigquery-php-quickstart
資料夾:rm -R bigquery-php-quickstart
-R
旗標會刪除資料夾中的所有資產。在 Cloud Shell 中,移至上一層目錄:
cd ..
刪除您建立的
bigquery-python-quickstart
資料夾:rm -R bigquery-python-quickstart
-R
旗標會刪除資料夾中的所有資產。在 Cloud Shell 中,移至上一層目錄:
cd ..
刪除您建立的
bigquery-ruby-quickstart
資料夾:rm -R bigquery-ruby-quickstart
-R
旗標會刪除資料夾中的所有資產。- 進一步瞭解如何使用 BigQuery 用戶端程式庫。
- 進一步瞭解 BigQuery 公開資料集。
- 瞭解如何將資料載入 BigQuery。
- 進一步瞭解如何在 BigQuery 查詢資料。
- 掌握 BigQuery 最新消息。
- 瞭解 BigQuery 定價。
- 瞭解 BigQuery 配額與限制。
查詢公開資料集
選取下列其中一種語言:
C#
您已成功使用 BigQuery C# 用戶端程式庫查詢公開資料集。
Go
您已成功使用 BigQuery Go 用戶端程式庫查詢公開資料集。
Java
您已成功使用 BigQuery Java 用戶端程式庫查詢公開資料集。
Node.js
您已成功使用 BigQuery Node.js 用戶端程式庫查詢公開資料集。
PHP
您已成功使用 BigQuery PHP 用戶端程式庫查詢公開資料集。
Python
您已成功使用 BigQuery Python 用戶端程式庫查詢公開資料集。
Ruby
您已成功使用 BigQuery Ruby 用戶端程式庫查詢公開資料集。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取費用,請刪除 Google Cloud 專案,或刪除您在本逐步操作說明課程中建立的資源。
刪除專案
如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。
如要刪除專案:
刪除資源
如果使用現有專案,請刪除稍早建立的資源: