简体   繁体   English

编写 Google Apps 脚本以在 BigQuery“查询执行期间超出资源”时引发错误

[英]Program Google Apps Script to throw error when BigQuery "Resources exceeded during query execution"

I am using scripts to run views and use the query results to update tables in BigQuery.我正在使用脚本来运行视图并使用查询结果来更新 BigQuery 中的表。 I then use these tables in Data Studio for tables/visualizations.然后,我在 Data Studio 中将这些表用于表/可视化。

An issue I've noticed is that sometimes my views will suddenly throw an error "Resources exceeded during query execution".我注意到的一个问题是,有时我的视图会突然抛出一个错误“查询执行期间资源超出”。 My Google Apps script will run successfully, however, but the table will remain unchanged because the view couldn't be executed.但是,我的 Google Apps 脚本将成功运行,但表格将保持不变,因为无法执行视图。 As a result, the data in my Data Studio visualizations and tables will be stale.因此,我的 Data Studio 可视化和表格中的数据将是陈旧的。 And I will have no way to know this unless I go to edit the view and see that it is throwing the error.除非我 go 编辑视图并看到它抛出错误,否则我将无法知道这一点。

Is there any way to add something to my script so that the script will fail or notify me when Bigquery throws the "Resources exceeded during query execution" error?有什么方法可以在我的脚本中添加一些内容,以便当 Bigquery 抛出“查询执行期间超出资源”错误时脚本将失败或通知我? This way I won't unknowingly have stale data due to the error.这样我就不会因为错误而在不知不觉中拥有过时的数据。

  var configuration = {
    "query": {
    "useQueryCache": false,
    "destinationTable": {
          "projectId": "abc-123",
          "datasetId": "report_tables",
          "tableId": "dashboard_1"
        },
    "writeDisposition": "WRITE_TRUNCATE",
    "createDisposition": "CREATE_IF_NEEDED",
    "allowLargeResults": true,
    "useLegacySql": false,
    "query": "SELECT * FROM `abc-123.report_tables.dashboard_1_view`"
    }
  };

  var job = {
    "configuration": configuration
  };

  var jobResult = BigQuery.Jobs.insert(job, "abc-123");
  Logger.log(jobResult);
}

I'm hoping you can help me add some clause to the above script so that when resources are exceeded in the BigQuery view, this will fail to execute or notify me in some way so I know to go correct the issue in BigQuery.我希望你能帮我在上面的脚本中添加一些子句,这样当 BigQuery 视图中的资源超出时,这将无法执行或以某种方式通知我,所以我知道 go 更正 BigQuery 中的问题。

Thanks for the help!谢谢您的帮助!

Your function is succeeding because all jobs/queries on BigQuery are async. 您的函数成功了,因为BigQuery上的所有作业/查询都是异步的。 The line var jobResult = BigQuery.Jobs.insert(job, "abc-123"); var jobResult = BigQuery.Jobs.insert(job, "abc-123"); only submits the job/query to the BigQuery service to begin execution. 作业/查询提交给BigQuery服务以开始执行。 Then your function returns. 然后您的函数返回。

So, you need to wait and poll for the status of the job while it's executing and then deal with any errors if it fails with something like this: 因此,您需要等待并轮询作业的status ,然后再执行以下操作以处理任何错误:

  [..]
  var job = BigQuery.Jobs.insert(job, "abc-123");
  Logger.log(job.status.state);
  var jobId = job.jobReference.jobId;

  // Check on status of the Query Job.
  var sleepTimeMs = 500;
  while (job.status.state !== "DONE") {
    Utilities.sleep(sleepTimeMs);
    job = BigQuery.Jobs.get("abc-123", jobId);
    Logger.log(job.status.state);
  }
  if(job.status.errors != null && job.status.errors.length > 0) {
     Logger.log("FAILED:" + job.status.errors);
  } else {
     Logger.log("SUCCEEDED")
  }
  [..]

Sample output (failure): 样本输出(失败):

[19-06-03 19:54:48:557 AEST] RUNNING
[19-06-03 19:54:49:161 AEST] RUNNING
[19-06-03 19:54:49:789 AEST] RUNNING
[19-06-03 19:54:50:368 AEST] RUNNING
[19-06-03 19:54:51:147 AEST] RUNNING
[19-06-03 19:54:51:783 AEST] RUNNING
[19-06-03 19:54:52:356 AEST] RUNNING
[19-06-03 19:54:52:957 AEST] RUNNING
[19-06-03 19:54:53:564 AEST] RUNNING
[19-06-03 19:54:54:151 AEST] RUNNING
[19-06-03 19:54:54:748 AEST] RUNNING
[19-06-03 19:54:55:338 AEST] RUNNING
[19-06-03 19:54:55:954 AEST] RUNNING
[19-06-03 19:54:56:539 AEST] RUNNING
[19-06-03 19:54:57:107 AEST] RUNNING
[19-06-03 19:54:57:724 AEST] RUNNING
[19-06-03 19:54:58:513 AEST] RUNNING
[19-06-03 19:54:59:524 AEST] RUNNING
[19-06-03 19:55:00:144 AEST] RUNNING
[19-06-03 19:55:00:993 AEST] RUNNING
[19-06-03 19:55:01:613 AEST] RUNNING
[19-06-03 19:55:02:219 AEST] RUNNING
[19-06-03 19:55:02:989 AEST] RUNNING
[19-06-03 19:55:03:557 AEST] RUNNING
[19-06-03 19:55:04:123 AEST] RUNNING
[19-06-03 19:55:04:684 AEST] RUNNING
[19-06-03 19:55:05:408 AEST] RUNNING
[19-06-03 19:55:06:018 AEST] RUNNING
[19-06-03 19:55:06:603 AEST] RUNNING
[19-06-03 19:55:07:215 AEST] RUNNING
[19-06-03 19:55:07:789 AEST] RUNNING
[19-06-03 19:55:08:424 AEST] RUNNING
[19-06-03 19:55:08:985 AEST] RUNNING
[19-06-03 19:55:09:580 AEST] RUNNING
[19-06-03 19:55:10:184 AEST] RUNNING
[19-06-03 19:55:10:802 AEST] RUNNING
[19-06-03 19:55:11:391 AEST] RUNNING
[19-06-03 19:55:11:984 AEST] RUNNING
[19-06-03 19:55:12:564 AEST] RUNNING
[19-06-03 19:55:13:154 AEST] RUNNING
[19-06-03 19:55:13:752 AEST] RUNNING
[19-06-03 19:55:14:372 AEST] RUNNING
[19-06-03 19:55:14:974 AEST] RUNNING
[19-06-03 19:55:15:661 AEST] RUNNING
[19-06-03 19:55:16:227 AEST] RUNNING
[19-06-03 19:55:16:815 AEST] RUNNING
[19-06-03 19:55:17:416 AEST] RUNNING
[19-06-03 19:55:18:002 AEST] DONE
[19-06-03 19:55:18:003 AEST] FAILED:{"reason":"resourcesExceeded","message":"Resources exceeded during query execution: The query could not be executed in the allotted memory. Peak usage: 121% of limit.\nTop memory consumer(s):\n  ORDER BY operations: 99%\n  other/unattributed: 1%\n"}

Sample Output (success): 样本输出(成功):

[19-06-03 19:59:48:206 AEST] RUNNING
[19-06-03 19:59:48:820 AEST] RUNNING
[19-06-03 19:59:49:433 AEST] RUNNING
[19-06-03 19:59:50:015 AEST] DONE
[19-06-03 19:59:50:016 AEST] SUCCEEDED

Something to consider is that using Google Apps Script ties all this to your personal Google account ie not a service account. 需要考虑的是,使用Google Apps脚本将所有这些都与您的个人Google帐户(而不是服务帐户)相关联。 That might be ok for you, but in the enterprise it's a bit of a no-no. 这可能对您来说还可以,但是在企业中,这是不可以的。

Finally, I'd probably move your scripts into Cloud Build. 最后,我可能会将您的脚本移动到Cloud Build中。 I'd argue that it's much more powerful, maintainable, easier to use and flexible. 我认为它更强大,可维护,更易于使用和灵活。

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

相关问题 查询执行期间超出 BigQuery 问题资源 - BigQuery problem resources exceeded during query execution 查询执行期间超出了 Google BigQuery 资源。 如何在 SQL 中拆分带有分区的大 window 帧 - Google BigQuery Resources exceeded during query execution. How to split large window frames with partition in SQL 在 BigQuery SQL 中,如何跟踪资源使用情况以避免“查询执行期间超出资源”错误 - In BigQuery SQL, how to track resource usage to avoid "Resources exceeded during query execution" error 查询失败错误:查询执行期间资源超出:无法在分配的 memory 中执行查询 - Query Failed Error: Resources exceeded during query execution: The query could not be executed in the allotted memory BigQuery:超出资源 - Google 表格服务超载 - BigQuery: Resources exceeded - Google Sheets service overloaded Google BigQuery 查询超出资源限制 - Google BigQuery Query exceeded resource limits 资源超过 BigQuery - Resources exceeded BigQuery 在 Google Apps 脚本中定义 BigQuery 表位置 - Define BigQuery table location in Google Apps Script 为什么在 Google BigQuery 上安排查询时出现错误? - Why am I getting an error when scheduling a query on Google BigQuery? Big Query 作业在执行过程中遇到错误 - Big Query The job encountered an error during execution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM