在 Google BigQuery API 中为 PHP 捕获 "Response too large to return error" 的正确方法是什么?

What is the proper way to capture a "Response too large to return error" in Google BigQuery API for PHP?

在 Google BigQuery 网络界面中,如果我 运行 查询 return 的响应太大,我会收到消息:

错误:return 的响应太大。考虑在作业配置中将 allowLargeResults 设置为 true。

当运行在作业配置中未设置 allowLargeResults 的查询时,如何在 Google BigQuery API 界面中捕获此错误消息?

我们使用这个片段来处理错误,它帮了大忙:

当我们放置作业时,以及稍后在循环中检查作业状态时,我们也会遇到这种情况,因为作业完成时会弹出该错误。

try {
                try {
                    $job = $bq->jobs->insert(PROJECT_ID, $job);
                } catch (Google_IO_Exception $e) {
                    $this->e('Exception: ' . $e->getMessage(), 'red');
                    $this->e('Strace: ' . $e->getTraceAsString());
                    if ($e->getMessage() == 'SSL connect error') {
                        $this->clearTokenFile();
                        $this->releaseJob();
                    }
                    return false;
                }

                $status = new Google_Service_Bigquery_JobStatus();
                $status = $job->getStatus();

                if (0 != $status->count()) {
                    $err_res = $status->getErrorResult();
                    $this->e($err_res->getMessage(), 'red');

                    return false;
                }
            } catch (Google_Service_Exception $e) {
                $this->e('Exception: ' . $e->getMessage(), 'red');
                return false;
            }

on和insert我们都有,这里注意reason字段:

try {
            $resp = new Google_Service_Bigquery_TableDataInsertAllResponse();
            $resp = $bq->tabledata->insertAll($project_id, $dataset_id, static::tableId(), $request);
            $errors = new Google_Service_Bigquery_TableDataInsertAllResponseInsertErrors();
            $errors = @$resp->getInsertErrors();
            if (!empty($errors)) {
                $error_msg = "\r\nRequest Headers: \r\n" . json_encode($client->request->getRequestHeaders()) . "\r\nResponse Headers: \r\n" . json_encode($client->request->getResponseHeaders()) . "\r\nRequest Body:\r\n" . $client->request->getPostBody() . "\r\nResponse Body:\r\n" . $client->request->getResponseBody() . "\r\n";
                if (is_array($errors)) {
                    foreach ($errors as $eP) {
                        $arr = $eP->getErrors();
                        $line = $eP->getIndex();
                        if (is_array($arr)) {
                            foreach ($arr as $e) {
                                switch ($e->getReason()) {
                                    case "stopped":
                                        break;
                                    case "timeout":
                                        $failed_lines[] = $line;
                                        $last_reason = $e->getReason();
                                        $error_msg.= sprintf("Timeout on line %s, reason: %s, msg: %s\r\n", $line, $e->getReason(), $e->getMessage());
                                        break;
                                    default:
                                        $error_msg.= sprintf("Error on line %s, reason: %s, msg: %s\r\n", $line, $e->getReason(), $e->getMessage());
                                        break;
                                }
                            }
                        } else {
                            $error_msg.= json_encode($arr) . "\r\n";
                        }
                    }
                    $this->setErrorMessage($error_msg);
                } else {
                    $this->setErrorMessage($errors);
                }
                //print_r($errors);
                //exit;
                $success = false;
            }
            return $ret;
        } catch (Google_Service_Exception $e) {
            $this->setErrors($e->getErrors())->setErrorMessage($e->getMessage());
            throw $e;
        }

在各自的 BigQuery 中 API - Jobs: get this info is in status.errorResult
分别在
reason 属性: responseTooLarge
message 属性:对 return 的响应太大。考虑在作业配置中将 allowLargeResults 设置为 true。更多详情,...

您还可以检查 status.errors 以了解作业执行期间遇到的所有错误的更多详细信息

回答我自己的问题:以下是为我解决问题的总结。总之,我无法让它为同步查询抛出错误,但能够让它为异步查询抛出错误。

这是一个示例查询,其响应太大 return:

$query = "SELECT * FROM [publicdata:samples.github_timeline] LIMIT 1000000;"

同步查询

运行 同步查询 jobs.query:

try {
  $query_request = new Google_Service_Bigquery_QueryRequest();
  $query_request->setQuery($query);
  $res = $this->gbq_service->jobs->query($this->_project_id, $query_request);
  return $res;
} catch (Exception $e){
  echo $e->getMessage());
}

我收到的回复是:

{
  "cacheHit": null,
  "jobComplete": false,
  "kind": "bigquery#queryResponse",
  "pageToken": null,
  "totalBytesProcessed": null,
  "totalRows": null
}

所以在这种情况下,我仍然没有收到想要的 "response too large to return"。

异步查询

如果我 运行 作为异步查询的作业,作业状态最终设置为完成,并且在检查结果时,会抛出一条错误消息:

{
  "error": "Error calling GET https://www.googleapis.com/bigquery/v2/projects/.../queries/job_2VICoK6yX0YMM_zRkJ10hT9mom8?timeoutMs=1000000&maxResults=100: (403) Response too large to return. Consider setting allowLargeResults to true in your job configuration. For more information, see https://cloud.google.com/bigquery/troubleshooting-errors",
}

告诉我需要将结果保存为 table 并将 allowLargeResults 设置为 true。

所以,简短的回答是 - 运行 您的查询作为异步查询。

更新

我联系了 google,他们提到这可能是 php api 中的错误。他们说他们会把它转发给 php gbq API 人。