首页 > 解决方案 > 当 bigquery 的查询结果为空(使用 pthon)时,关于如何使用 gcp stackdriver 收集日志的任何想法?

问题描述

我有一个云功能,它正在执行查询并将作业结果存储到新的 bigquery 表中。每当我得到查询作业结果为空时,我想将日志存储到堆栈驱动程序中,这意味着找不到该特定查询执行的记录。谁能建议我如何完成这项任务。

云函数代码:

def main(request):

    query = "select * from `myproject.mydataset.mytable`"
    client = bigquery.Client()
    job_config = bigquery.QueryJobConfig()
    dest_dataset = client.dataset(destination_dataset, destination_project)
    dest_table = dest_dataset.table(destination_table)
    job_config.destination = dest_table
    job_config.create_disposition = 'CREATE_IF_NEEDED'
    job_config.write_disposition = 'WRITE_APPEND'
    job = client.query(query, location='US', job_config=job_config)
    job.result()

标签: python-3.xgoogle-cloud-platformgoogle-bigquerygoogle-cloud-functionsgoogle-cloud-stackdriver

解决方案


  • 转到 GCP 控制台中的 Stackdriver 日志记录部分。
  • 激活高级过滤器(单击过滤器字段右侧的箭头)
  • 用这个填充自定义字段:
resource.type="bigquery_resource"
"device_states"
protoPayload.methodName="jobservice.getqueryresults"
NOT protoPayload.serviceData.jobGetQueryResultsResponse.totalResults>"0"

这样,您只选择等于 0 的总结果。诀窍是缺少总结果,结果为 0。使用这种语法,它可以工作。

然后,您可以创建导出到 BigQuery、Storage 或 PubSub,或创建指标。如果您这样做,那么您可以在 Stackdriver 监控和警报中使用此指标。一切都取决于你想做什么。


推荐阅读