![](/img/trans.png)
[英]Get all my scheduled SQL queries in BigQuery Google Cloud
[英]How do I list my scheduled queries via the Python google client API?
是的,您可以使用 API。 当您不知道使用哪一个时,我有一个提示。 使用@Yev 提出的命令
bq ls --transfer_config --transfer_location=US --format=prettyjson
但记录 API 调用。 为此使用--apilog <logfile name>
参数
bq --apilog./log ls --transfer_config --transfer_location=US --format=prettyjson
而且,神奇的是,您可以找到命令调用的 API: https://bigquerydatatransfer.googleapis.com/v1/projects/<PROJECT-ID>/locations/US/transferConfigs?alt=json
://bigquerydatatransfer.googleapis.com/v1/projects/<PROJECT-ID>/locations/US/transferConfigs?alt=json
然后,一个简单的谷歌搜索会引导您找到正确的文档
在 python 中,在您的requirements.txt
中添加该依赖项: google-cloud-bigquery-datatransfer
并使用该代码
from google.cloud import bigquery_datatransfer
client = bigquery_datatransfer.DataTransferServiceClient()
parent = client.common_project_path("<PROJECT-ID>")
resp = client.list_transfer_configs(parent=parent)
print(resp)
使用云 SDK 有一个命令可以让你得到你想要的和更多: bq ls --transfer_config --transfer_location=US --format=prettyjson
,更多关于这里 - List Scheduled Queries in BigQuery
在命令提示符下执行此操作(假设已安装谷歌云 SKD)将为您提供以下内容(红色部分 = 计划查询 sql):
之后,您可以在 Python 中将其作为 shell 子进程运行并将其解析出来:
import pandas as pd
import json
from subprocess import PIPE, run, call
response = run('bq ls --transfer_config --transfer_location=US --format=prettyjson',
stdout=PIPE,
stderr=PIPE,
universal_newlines=True,
shell=True)
response
这是上面产生的前几行:
CompletedProcess(args='bq ls --transfer_config --transfer_location=US --format=prettyjson', returncode=0, stdout='[\n {\n "dataSourceId": "scheduled_query",\...
然后要获取 sql,您可以通过response.stdout
访问输出并解析为 json,然后将您的方式字典化为所需的结果或将其转换为 pandas 数据帧格式并从那里开始,如下所示:
data = json.loads(response.stdout)
df = pd.json_normalize(data)
df.columns =
dataSourceId
datasetRegion
destinationDatasetId
disabled
displayName
name
schedule
state
updateTime
userId
emailPreferences.enableFailureEmail
params.destination_table_name_template
### sql located in this one
params.query
params.write_disposition
scheduleOptions.startTime
params.overwrite_destination_table
params.source_dataset_id
params.source_project_id
scheduleOptions.endTime
nextRunTime
可能是我,但我很难找到有关使用 bigquery 数据传输 API(= 计划查询)的 python 文档。
在 python 中你可以这样做:
from google.cloud import bigquery_datatransfer
bq_datatransfer_client = bigquery_datatransfer.DataTransferServiceClient()
request_datatransfers = bigquery_datatransfer.ListTransferConfigsRequest(
# if US, you can just do parent='projects/YOUR_PROJECT_ID'
parent='projects/YOUR_PROJECT_ID/locations/EU',
)
# this method will also deal with pagination
response_datatransfers = bq_datatransfer_client.list_transfer_configs(
request=request_datatransfers)
# to convert the response to a list of scheduled queries
datatransfers = list(response_datatransfers)
以下是有关 API 的一些有用资源:
具体在.list_transfer_configs()
方法上:
在ListTransferConfigsRequest
类上:
关于如何使用 python API 的代码片段和示例:
API使用的一些官方文档:
使用 python 你可以这样做:
from google.cloud import bigquery_datatransfer
bq_datatransfer_client = bigquery_datatransfer.DataTransferServiceClient()
parent = 'projects/YOUR_PROJECT_ID/locations/EU'
resp_datatransfers = bq_datatransfer_client.list_transfer_configs(parent=parent)
datatransfers = list(resp_datatransfers)
文档链接: .list_transfer_configs()
获取任何特定列:
bq ls --transfer_config --transfer_location=US --format=prettyjson|jq -r ".[]|[.name,.displayName,.dataSourceId,.state,.userId]|@csv"|tr -d "\""
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.