简体   繁体   English

在 BigQuery Google Cloud 中获取我所有计划的 SQL 查询

[英]Get all my scheduled SQL queries in BigQuery Google Cloud

Im trying to get SQL codes by command-line (CLI) of my scheduled queries in BigQuery.我正在尝试通过 BigQuery 中计划查询的命令行 (CLI) 获取 SQL 代码。 I'm also interested if there is a way to do that by the Google Cloud Platform user interface.如果有一种方法可以通过 Google Cloud Platform 用户界面来做到这一点,我也很感兴趣。

I have taken a quick look to this related post, but that's not the answer that I am looking for.我快速浏览了这篇相关文章,但这不是我要找的答案。

Thank you in advance for all your answers.预先感谢您的所有回答。

I found how to query the scheduled queries with the bq CLI.我找到了如何使用bq CLI 查询计划的查询。 You have to rely on the BigQuery Transfer API .您必须依靠BigQuery Transfer API Why?为什么? I don't know, but it's the right keyword here.我不知道,但这是正确的关键字。

For listing all your schedule query, perform this (change your location if you want:):要列出所有日程查询,请执行此操作(如果需要,请更改您的位置:):

bq ls --transfer_config --transfer_location=eu

# Result
                                             name                                               displayName    dataSourceId     state
 --------------------------------------------------------------------------------------------- ------------- ----------------- -------
  projects/763366003587/locations/europe/transferConfigs/5de1fc66-0000-20f2-bee7-089e082935bc   test          scheduled_query

For viewing the detail, copy the name and use bq show要查看详细信息,请复制名称并使用bq show

bq show --transfer_config  \
projects/763366003587/locations/europe/transferConfigs/5de1fc66-0000-20f2-bee7-089e082935bc

# Result
       updateTime            destinationDatasetId   displayName      schedule       datasetRegion          userId                                            scheduleOptions                                       dataSourceId                  
                                                                         params
 ----------------------------- ---------------------- ------------- ----------------- --------------- ---------------------- -------------------------------------------------------------------------------------- ----------------- --------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
  2019-11-18T20:20:22.279237Z   bi_data                test          every day 20:19   europe          -7444165337568771239   {u'endTime': u'2019-11-18T21:19:36.528Z', u'startTime': u'2019-11-18T20:19:36.497Z'}   scheduled_query   {u'query': u'
SELECT * FROM `gbl-imt-homerider-basguillaueb.bi_data.device_states`', u'write_disposition': u'WRITE_TRUNCATE', u'destination_table_name_template': u'test_schedule'}

You can use json format and jq for getting only the query like this您可以使用 json 格式和 jq 只获取这样的查询

bq show --format="json" --transfer_config  \
projects/763366003587/locations/europe/transferConfigs/5de1fc66-0000-20f2-bee7-089e082935bc \
 | jq '.params.query'

# Result
"SELECT * FROM `gbl-imt-homerider-basguillaueb.bi_data.device_states`"

I can explain how I found this unexpected solution that if you want, but it's not the topic here.如果您愿意,我可以解释我是如何找到这个意想不到的解决方案的,但这不是这里的主题。 I think it's not documented我认为它没有记录在案

On the GUI, it's easier.在 GUI 上,它更容易。

  • Go to BigQuery (new UI, in blue) Go 到 BigQuery(新 UI,蓝色)
  • Click on scheduled query on the left menu点击左侧菜单中的预定查询

在此处输入图像描述

  • Click on your scheduled query name点击您预定的查询名称
  • Click on configuration on the top on the screen点击屏幕顶部的配置在此处输入图像描述

To get your scheduled queries (= datatransfers), you can also use the python API:要获取计划查询(= 数据传输),您还可以使用 python API:

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) 

To get the actual query text from the scheduled query:要从预定查询中获取实际查询文本:

for datatransfer in datatransfers:
    print(datatransfer.display_name)
    print(datatransfer.params.get('query'))
    print('\n')

See also these SO questions:另请参阅这些 SO 问题:

Docs on this specific part of the python API: https://cloud.google.com/python/docs/reference/bigquerydatatransfer/latest/google.cloud.bigquery_datatransfer_v1.services.data_transfer_service.DataTransferServiceClient#google_cloud_bigquery_datatransfer_v1_services_data_transfer_service_DataTransferServiceClient_list_transfer_configs关于 python API 这个特定部分的文档: https ://cloud.google.com/python/docs/reference/bigquerydatatransfer/latest/google.cloud.bigquery_datatransfer_v1.services.data_transfer_service.DataTransferServiceClient#google_cloud_bigquery_datatransfer_v1_services_data_transfer_service_DataTransferServiceClient_list_transfer_configs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM