google-apps-script - 列出 BigQuery 中的计划查询

标签 google-apps-script google-bigquery

我需要(以编程方式)分析 BigQuery 中计划查询的详细信息(例如更新了哪些表以及在 SQL 中访问了哪些表)。我已经使用 Apps 脚本 BigQuery.Tables.list() 对 BQ 表/ View 执行了类似的操作,但我找不到用于访问计划查询的 API。

用户界面能够列出它们,所以我觉得这应该可以通过编程方式实现,例如通过 REST API。有谁知道这是否可行,支持什么接口(interface)(Apps Script,REST ...),以及可能如何使用它的示例。

最佳答案

计划查询是 BigQuery 数据传输服务的一部分,因此您必须使用其 API。特别是projects.transferConfigs.list方法。在 dataSourceIds 字段中填写 scheduled_query,在 parent 中填写 projects/PROJECT_ID。正如评论中所讨论的,如果您使用的是 europe-west2 等区域位置而不是多区域位置(欧盟或美国),您应该使用 projects.locations.transferConfigs.list反而。现在,父资源将采用 projects/PROJECT_ID/locations/REGIONAL_LOCATION 的形式。

此外,对于其他传输,您可以使用 projects.dataSources.list 获取相应的 dataSourceIds方法。这就是我获得 scheduled_query 的方式。

响应将是一系列预定的查询,例如:

{
  "name": "projects/<PROJECT_NUMBER>/locations/us/transferConfigs/<TRANSFER_CONFIG_ID>",
  "destinationDatasetId": "<DATASET>",
  "displayName": "hacker-news",
  "updateTime": "2018-11-14T15:39:18.897911Z",
  "dataSourceId": "scheduled_query",
  "schedule": "every 24 hours",
  "nextRunTime": "2019-04-19T15:39:00Z",
  "params": {
    "write_disposition": "WRITE_APPEND",
    "query": "SELECT @run_time AS time,\n  title,\n  author,\n  text\nFROM `bigquery-public-data.hacker_news.stories`\nLIMIT\n  1000",
    "destination_table_name_template": "hacker_daily_news"
  },
  "state": "SUCCEEDED",
  "userId": "<USER_ID>",
  "datasetRegion": "us"
}

使用 bash 和 curl 进行 API 调用的示例:

#!/bin/bash

# parameter(s)
location=europe-west2

authToken="$(gcloud auth print-access-token)"
projectId=$(gcloud config get-value project 2>\dev\null)

# API call
scheduled_queries=$(curl  -H "Authorization: Bearer $authToken" \
https://bigquerydatatransfer.googleapis.com/v1/projects/$projectId/locations/$location/transferConfigs?dataSourceIds=scheduled_query)

# pretty print results
echo $scheduled_queries | python -m json.tool

关于google-apps-script - 列出 BigQuery 中的计划查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55745763/

相关文章:

performance - 多个 OnEdit 函数的最佳实践

javascript - 防止谷歌表格中的重复数据

google-bigquery - BQ STRUCT 与 RECORD 类型?

google-bigquery - Bigquery 中销量排名前 n 的供应商按月的销售额

java - Bigquery Java API 以 BigDecimal 形式返回计费层级

google-apps-script - 一个脚本可以触发另一个脚本中的函数吗?

google-apps-script - 在 Google Docs 中以编程方式创建锚定评论

google-apps-script - 如何在谷歌自定义菜单中制作图标

timestamp - 从 Bigquery : a preferable method 中的时间戳中提取日期

sql - Google BigQuery 标准 SQL - IN 子句在 BigQuery 中工作但不在命令行中工作