如何在Java中为BigQuery API请求设置超时

时间:2022-02-09 15:22:52

Sometimes when we poll for a BigQuery job, our request ends up with SocketTimeoutException. You can see the code raising the exception below.

有时,当我们轮询BigQuery作业时,我们的请求最终会出现SocketTimeoutException。您可以在下面看到引发异常的代码。

this.bigquery.jobs().get(projectNumber, jobId).execute();

And here is the error message we get.

这是我们得到的错误消息。

...
Caused by: java.net.SocketTimeoutException:
Timeout while fetching URL: https://www.googleapis.com/bigquery/v2/projects/######/jobs/######
...

My question is if there is a way to extend the timeout. And does anyone know what the default timeout is?

我的问题是,是否有办法延长超时。有谁知道默认超时是什么?

2 个解决方案

#1


7  

You can wrap the credential object in a HTTP initializer that disables (or extends) timeouts. That is, where you currently have this:

您可以将凭证对象包装在禁用(或扩展)超时的HTTP初始化程序中。也就是说,你现在有这个:

Credential credential = ...
Bigquery bigquery = new Bigquery(HTTP_TRANSPORT, JSON_FACTORY, credential);

you could do

你能做到的

final Credential credential = ...
HttpRequestInitializer initializer = new HttpRequestInitializer() {
  public void initialize(HttpRequest request) {
    credential.initialize(request);
    request.connectTimeout = request.readTimeout = 0;
  }
}
Bigquery bigquery = new Bigquery(HTTP_TRANSPORT, JSON_FACTORY, initializer);

See this for BigQuery object javadoc, this for BigQuery object creation example, and this for HttpRequestInitializer overloading.

有关BigQuery对象javadoc的信息,请参阅此内容,这适用于BigQuery对象创建示例,这适用于HttpRequestInitializer重载。

#2


1  

If it is the request only that you want to set a timeout to then in request body:

如果只是您要在请求正文中设置超时的请求:

query_config = { 'timeoutMs': 1000,

query_config = {'timeoutMs':1000,

set timeoutsMs to whatever you like withing reason ;)

设置timeoutsMs到你喜欢的任何理由;)

Hope that helps the documentation is here

希望有助于文档在这里

Edit

To get the results of the query even if it hasn't obeyed the timeout Call jobs.getQueryResults taken from the site you must specify a start row, and this also takes a timeout that behaves the same as the jobs.query timeout to allow waiting if the job is not yet complete.

要获得查询结果,即使它没有服从超时调用jobs.getQueryResults取自站点,您必须指定一个起始行,这也需要一个与jobs.query超时行为相同的超时以允许等待如果工作尚未完成。

#1


7  

You can wrap the credential object in a HTTP initializer that disables (or extends) timeouts. That is, where you currently have this:

您可以将凭证对象包装在禁用(或扩展)超时的HTTP初始化程序中。也就是说,你现在有这个:

Credential credential = ...
Bigquery bigquery = new Bigquery(HTTP_TRANSPORT, JSON_FACTORY, credential);

you could do

你能做到的

final Credential credential = ...
HttpRequestInitializer initializer = new HttpRequestInitializer() {
  public void initialize(HttpRequest request) {
    credential.initialize(request);
    request.connectTimeout = request.readTimeout = 0;
  }
}
Bigquery bigquery = new Bigquery(HTTP_TRANSPORT, JSON_FACTORY, initializer);

See this for BigQuery object javadoc, this for BigQuery object creation example, and this for HttpRequestInitializer overloading.

有关BigQuery对象javadoc的信息,请参阅此内容,这适用于BigQuery对象创建示例,这适用于HttpRequestInitializer重载。

#2


1  

If it is the request only that you want to set a timeout to then in request body:

如果只是您要在请求正文中设置超时的请求:

query_config = { 'timeoutMs': 1000,

query_config = {'timeoutMs':1000,

set timeoutsMs to whatever you like withing reason ;)

设置timeoutsMs到你喜欢的任何理由;)

Hope that helps the documentation is here

希望有助于文档在这里

Edit

To get the results of the query even if it hasn't obeyed the timeout Call jobs.getQueryResults taken from the site you must specify a start row, and this also takes a timeout that behaves the same as the jobs.query timeout to allow waiting if the job is not yet complete.

要获得查询结果,即使它没有服从超时调用jobs.getQueryResults取自站点,您必须指定一个起始行,这也需要一个与jobs.query超时行为相同的超时以允许等待如果工作尚未完成。