Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set timeout for BigQuery API request in Java

Sometimes when we poll for a BigQuery job, our request ends up with 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?

like image 880
pgiecek Avatar asked Aug 19 '14 14:08

pgiecek


2 Answers

You can wrap the credential object in a HTTP initializer that disables (or extends) timeouts. 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.

like image 175
Jordan Tigani Avatar answered Oct 17 '22 04:10

Jordan Tigani


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 ;)

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.

like image 25
auracool Avatar answered Oct 17 '22 04:10

auracool