4

I am querying data from BigQuery

here is my code:

import com.google.cloud.bigquery.*;

 public static JSONArray query(String tableId, String field, String val) throws Exception{

    String queryString = "SELECT * FROM `" + tableId +"` where " + field + "=\"" + val + "\";";
    BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
    // Create a query request
    QueryRequest queryRequest =
            QueryRequest.newBuilder(queryString)
                    .setMaxWaitTime(100000L)
                    .setPageSize(1000L)
                    .setUseLegacySql(false)
                    .build();
    // Request query to be executed and wait for results
    QueryResponse queryResponse = bigquery.query(queryRequest);
    while (!queryResponse.jobCompleted()) {
        Thread.sleep(1000L);
        queryResponse = bigquery.getQueryResults(queryResponse.getJobId());
    }
    // Read rows
    Iterator<List<FieldValue>> rowIterator = queryResponse.getResult().iterateAll();
    while (rowIterator.hasNext()) {
        System.out.println(rowIterator.next());
    }
}

here is the data I'm trying to get which is in tableA and tableB:

{id: "111", data: {a: "222", b: "333"}}

when I run the query on first table

query("dataset.tableA", "data.a",  "222")

I get the results well

but when I run the query on second table

query("dataset.tableB", "data.a",  "222")

I get an error - even though when I query threw the webUI I get the results well:

here is the error:

Exception in thread "main" com.google.cloud.bigquery.BigQueryException: Read timed out
    at com.google.cloud.bigquery.spi.DefaultBigQueryRpc.translate(DefaultBigQueryRpc.java:93)
    at com.google.cloud.bigquery.spi.DefaultBigQueryRpc.query(DefaultBigQueryRpc.java:408)
    at com.google.cloud.bigquery.BigQueryImpl$21.call(BigQueryImpl.java:584)
    at com.google.cloud.bigquery.BigQueryImpl$21.call(BigQueryImpl.java:581)
    at com.google.cloud.RetryHelper.doRetry(RetryHelper.java:179)
    at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:244)
    at com.google.cloud.bigquery.BigQueryImpl.query(BigQueryImpl.java:580)
    at com.example.helloworld.integration.helpers.BigQueryDownload.query(BigQueryDownload.java:59)
    at com.example.helloworld.integration.helpers.BigQueryDownload.main(BigQueryDownload.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
    at java.net.SocketInputStream.read(SocketInputStream.java:170)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
    at sun.security.ssl.InputRecord.read(InputRecord.java:503)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)
    at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:930)
    at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:704)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1569)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)
    at com.google.api.client.http.javanet.NetHttpResponse.<init>(NetHttpResponse.java:37)
    at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:94)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:981)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
    at com.google.cloud.bigquery.spi.DefaultBigQueryRpc.query(DefaultBigQueryRpc.java:406)
Graham Polley
  • 14,393
  • 4
  • 44
  • 80
dina
  • 4,039
  • 6
  • 39
  • 67
  • no - this is totally a different issue - my response isn't too large - contains one row only I get a `Read timed out` error, not `Response is too large` – dina Jan 25 '17 at 10:36
  • How long does your query take to run in the UI? It might be timing out (try setting the `timeoutMs` field - http://stackoverflow.com/questions/41494059/bigquery-synchronous-query-is-not-returning-any-results/41495950#41495950 – Graham Polley Jan 27 '17 at 00:34
  • 1 ms. not long at all, my table doesnt contain many records – dina Jan 29 '17 at 15:12
  • _"1ms"_ - that's not possible! Do you mean 1s or 1m? – Graham Polley Jan 29 '17 at 22:31
  • `Query complete (1.9s elapsed, 4.01 MB processed)` also I dont understand why I get the timeout in one table and in the other I dont, I tried setting the `timeoutMs` field to be longer, but didnt help – dina Jan 30 '17 at 10:18

1 Answers1

2

You can configure the timeouts when building the BigQuery client:

BigQuery bigquery = BigQueryOptions.getDefaultInstance()
     .setRetrySettings(RetrySettings.newBuilder()
     .setMaxAttempts(10)
     .setRetryDelayMultiplier(1.5)
     .setTotalTimeout(Duration.ofMinutes(5))
.build()).getService();
Benoît Sauvère
  • 701
  • 7
  • 23