简体   繁体   English

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

[英]How to set timeout for BigQuery API request in Java

Sometimes when we poll for a BigQuery job, our request ends up with SocketTimeoutException . 有时,当我们轮询BigQuery作业时,我们的请求最终会导致SocketTimeoutException You can see the code raising the exception below. 您可以在下面看到引发异常的代码。

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? 有谁知道默认超时是什么?

You can wrap the credential object in a HTTP initializer that disables (or extends) timeouts. 您可以将凭证对象包装在禁用(或扩展)超时的HTTP初始化程序中。 That is, where you currently have this: 也就是说,你现在有这个:

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超载。

If it is the request only that you want to set a timeout to then in request body: 如果只是要设置超时则在请求主体的请求

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超时行为相同的超时以允许等待如果工作尚未完成。

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

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