首页 > 解决方案 > 如何检索与在公共数据集上执行 Google BigQuery 相关的作业信息?

问题描述

我从 Cloud Shell 对 BigQuery 进行了查询:

xenonxie@cloudshell:~ (welynx)$ bq query --dry_run "SELECT COUNT(1) as rowcount, COUNTIF(corpus = 'hamlet') as rowcount_hamlet FROM publicdata.samples.shakespeare order by rowcount_hamlet desc"
Query successfully validated. Assuming the tables are not modified, running this query will process 2464625 bytes of data.
xenonxie@cloudshell:~ (welynx)$ bq query "SELECT COUNT(1) as rowcount, COUNTIF(corpus = 'hamlet') as rowcount_hamlet FROM publicdata.samples.shakespeare order by rowcount_hamlet desc"
Waiting on bqjob_r152b89ff4ea17df1_0000016faa8d1546_1 ... (0s) Current status: DONE   
+----------+-----------------+
| rowcount | rowcount_hamlet |
+----------+-----------------+
|   164656 |            5318 |
+----------+-----------------+

我可以看到有一个与之相关的工作:

xenonxie@cloudshell:~ (welynx)$ bq ls -j -a
                    jobId                      Job Type    State      Start Time         Duration
 -------------------------------------------- ---------- --------- ----------------- ----------------
  bqjob_r152b89ff4ea17df1_0000016faa8d1546_1   query      SUCCESS   15 Jan 13:52:50   0:00:00.886000

现在,我想按照此处的 BigQuery REST API 文档中的说明检索作业的详细信息:

https://bigquery.googleapis.com/bigquery/v2/projects/bqjob_r152b89ff4ea17df1_0000016faa8d1546_1/jobs

但是,我收到以下错误:

xenonxie@cloudshell:~ (welynx)$ wget https://bigquery.googleapis.com/bigquery/v2/projects/bqjob_r152b89ff4ea17df1_0000016faa8d1546_1/jobs
--2020-01-15 15:10:23--  https://bigquery.googleapis.com/bigquery/v2/projects/bqjob_r152b89ff4ea17df1_0000016faa8d1546_1/jobs
Resolving bigquery.googleapis.com (bigquery.googleapis.com)... 173.194.217.95, 2607:f8b0:400c:c0d::5f
Connecting to bigquery.googleapis.com (bigquery.googleapis.com)|173.194.217.95|:443... connected.
HTTP request sent, awaiting response... 401 Unauthorized

Username/Password Authentication Failed.

谁能告诉我这里出了什么问题以及如何解决?非常感谢。

标签: restgoogle-cloud-platformgoogle-bigquery

解决方案


有几件事在这里看起来不正确:

  • 虽然您可以使用wget来调用 BigQuery REST API,但我不推荐使用该方法 - 首选、最常用且最简单的方法是使用cURL
  • 您链接的 BigQuery REST API 方法用于列出所有BigQuery 作业,而不是您要求的单个作业的详细信息。此外,请求的 URL 必须采用 https://bigquery.googleapis.com/bigquery/v2/projects/{projectId}/jobs 此处提到的形式 - 您已省略{projectId}并将作业 ID 放在其位置。
  • 无论如何,要查看单个作业 ID 的详细信息,应改用 BigQuery REST API 方法 jobs.get,其中 URL 必须采用 形式 https://bigquery.googleapis.com/bigquery/v2/projects/{projectId}/jobs/{jobId},替换{projectId}为您的项目 ID 和{jobId}BigQuery 作业 ID。
  • 要使用 REST API 调用,cURL您必须提供某种形式的身份验证。一种方法是使用访问令牌,可以通过gcloud auth print-access-token从即 Cloud Shell 运行来获得。

鉴于我之前的观点,总结一下,应该给你你正在寻找的东西(使用cURL)的 REST API 调用最终应该像这样:

curl -H "Authorization: Bearer "$(gcloud auth print-access-token) \
https://bigquery.googleapis.com/bigquery/v2/projects/{projectId}/jobs/bqjob_r152b89ff4ea17df1_0000016faa8d1546_1

重要提示:确保您提供项目 ID作为参数,{projectId}相应地替换(包括大括号)创建 BigQuery 作业的位置。另外,我从那里复制粘贴了您问题中的作业 ID,因此根据您要检查的作业,最后一部分也应根据您的需要进行修改。


推荐阅读