Rest 如何在公共数据集上检索与Google BigQuery执行关联的作业的信息?

Rest 如何在公共数据集上检索与Google BigQuery执行关联的作业的信息?,rest,google-cloud-platform,google-bigquery,Rest,Google Cloud Platform,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 valida

我从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文档中描述的作业详细信息:

但是,我收到了以下错误:

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.

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

这里有两件事情看起来不太对劲:

  • 虽然您可以使用
    wget
    调用BigQueryRESTAPI, 我不推荐这种方法——首选的、最常用的方法 简单的方法是使用
    cURL
  • BigQuery REST API方法用于列出allBigQuery 工作,而不是你所要求的一个工作的细节。此外 请求的URL的格式必须为
    https://bigquery.googleapis.com/bigquery/v2/projects/{projectId}/jobs
    如前所述,您省略了
    {projectId}
    并放置了作业 取而代之的是身份证
  • 无论如何,要查看单个作业ID的详细信息,请使用BigQuery REST API方法 应改为使用,其中URL必须位于 形式
    https://bigquery.googleapis.com/bigquery/v2/projects/{projectId}/jobs/{jobId}
    , 将
    {projectId}
    替换为项目ID,将
    {jobId}
    替换为 BigQuery作业ID
  • 要使用
    cURL
    调用RESTAPI,必须提供某种形式的 认证。一种方法是使用访问令牌,该令牌可以通过从 云壳
综上所述,REST API调用(使用
cURL
)最终应该是这样的:

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

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

非常感谢Maxim的帮助,我通过您总结的命令获得了所需的结果。虽然您可以直接调用API,但Google建议您使用提供的用于各种语言(例如python、go)的客户端库之一。通常,这些库简化了身份验证和与API的交互。
curl -H "Authorization: Bearer "$(gcloud auth print-access-token) \
https://bigquery.googleapis.com/bigquery/v2/projects/{projectId}/jobs/bqjob_r152b89ff4ea17df1_0000016faa8d1546_1