rest - 如何检索与在公共数据集上执行 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.
谁能告诉我这里出了什么问题以及如何解决?非常感谢。
解决方案
有几件事在这里看起来不正确:
- 虽然您可以使用
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,因此根据您要检查的作业,最后一部分也应根据您的需要进行修改。
推荐阅读
- android - java.lang.IllegalStateException:在workmanager中测试房间dao时非法连接指针1
- javascript - 使用 onSnapshot 触发器在 firebase 9.0.0 中获取以前的数据
- php - 注意:ob_end_flush(): /home/mysite/domains/mysite.com/public_html/wp-includes/functions.php中zlib输出压缩(1)的缓冲区发送失败
- mongodb - updateOne - 过滤一个字段,推送或更新子字段,添加/更新计数器
- python - 如何修复 cv2.error:OpenCV 代码中的未知 C++ 异常?
- firebase - 如果用户文档在 Firestore 上发生更改,则更新提供者中的用户数据
- python - Selenium 问题 - 无法定位元素:{"method":"css selector","selector" - 需要帮助解决此问题
- python - pygame 中的错误,找不到对象“船”
- python - 向箱线图添加图像注释
- neo4j - 基于 where not in 的密码过滤关系