Search in sources :

Example 1 with QueryResult

use of com.google.cloud.bigquery.QueryResult in project google-cloud-java by GoogleCloudPlatform.

the class ITBigQuerySnippets method testRunQuery.

@Test
public void testRunQuery() throws InterruptedException {
    QueryResponse queryResponse = bigquerySnippets.runQuery(QUERY);
    assertNotNull(queryResponse);
    assertTrue(queryResponse.jobCompleted());
    assertFalse(queryResponse.hasErrors());
    QueryResult result = queryResponse.getResult();
    assertNotNull(result);
    assertTrue(bigquerySnippets.cancelJob(queryResponse.getJobId().getJob()));
    queryResponse = bigquerySnippets.queryResults(QUERY);
    assertNotNull(queryResponse);
    assertTrue(queryResponse.jobCompleted());
    assertFalse(queryResponse.hasErrors());
    result = queryResponse.getResult();
    assertNotNull(result);
    assertTrue(bigquerySnippets.cancelJobFromId(queryResponse.getJobId().getJob()));
}
Also used : QueryResult(com.google.cloud.bigquery.QueryResult) QueryResponse(com.google.cloud.bigquery.QueryResponse) Test(org.junit.Test)

Example 2 with QueryResult

use of com.google.cloud.bigquery.QueryResult in project google-cloud-java by GoogleCloudPlatform.

the class BigQuerySnippets method runQueryWithParameters.

/**
   * Example of running a query with query parameters.
   */
// [TARGET query(QueryRequest)]
// [VARIABLE "SELECT distinct(corpus) FROM `bigquery-public-data.samples.shakespeare` where word_count > @wordCount"]
public QueryResponse runQueryWithParameters(String query) throws InterruptedException {
    // [START runQueryWithParameters]
    QueryRequest request = QueryRequest.newBuilder(query).setUseLegacySql(// standard SQL is required to use query parameters
    false).addNamedParameter("wordCount", QueryParameterValue.int64(5)).build();
    QueryResponse response = bigquery.query(request);
    // Wait for things to finish
    while (!response.jobCompleted()) {
        Thread.sleep(1000);
        response = bigquery.getQueryResults(response.getJobId());
    }
    if (response.hasErrors()) {
    // handle errors
    }
    QueryResult result = response.getResult();
    for (List<FieldValue> row : result.iterateAll()) {
    // do something with the data
    }
    // [END runQueryWithParameters]
    return response;
}
Also used : QueryResult(com.google.cloud.bigquery.QueryResult) QueryRequest(com.google.cloud.bigquery.QueryRequest) QueryResponse(com.google.cloud.bigquery.QueryResponse) FieldValue(com.google.cloud.bigquery.FieldValue)

Example 3 with QueryResult

use of com.google.cloud.bigquery.QueryResult in project google-cloud-java by GoogleCloudPlatform.

the class BigQuerySnippets method queryResults.

/**
   * Example of getting the results of query.
   */
// [TARGET getQueryResults(JobId, QueryResultsOption...)]
// [VARIABLE "SELECT unique(corpus) FROM [bigquery-public-data:samples.shakespeare]"]
public QueryResponse queryResults(final String query) throws InterruptedException {
    // [START queryResults]
    QueryRequest request = QueryRequest.of(query);
    QueryResponse response = bigquery.query(request);
    // Wait for things to finish
    while (!response.jobCompleted()) {
        Thread.sleep(1000);
        response = bigquery.getQueryResults(response.getJobId());
    }
    if (response.hasErrors()) {
    // handle errors
    }
    QueryResult result = response.getResult();
    for (List<FieldValue> row : result.iterateAll()) {
    // do something with the data
    }
    // [END queryResults]
    return response;
}
Also used : QueryResult(com.google.cloud.bigquery.QueryResult) QueryRequest(com.google.cloud.bigquery.QueryRequest) QueryResponse(com.google.cloud.bigquery.QueryResponse) FieldValue(com.google.cloud.bigquery.FieldValue)

Example 4 with QueryResult

use of com.google.cloud.bigquery.QueryResult in project google-cloud-java by GoogleCloudPlatform.

the class BigQuerySnippets method runQuery.

/**
   * Example of running a query.
   */
// [TARGET query(QueryRequest)]
// [VARIABLE "SELECT unique(corpus) FROM [bigquery-public-data:samples.shakespeare]"]
public QueryResponse runQuery(String query) throws InterruptedException {
    // [START runQuery]
    QueryRequest request = QueryRequest.of(query);
    QueryResponse response = bigquery.query(request);
    // Wait for things to finish
    while (!response.jobCompleted()) {
        Thread.sleep(1000);
        response = bigquery.getQueryResults(response.getJobId());
    }
    if (response.hasErrors()) {
    // handle errors
    }
    QueryResult result = response.getResult();
    for (List<FieldValue> row : result.iterateAll()) {
    // do something with the data
    }
    // [END runQuery]
    return response;
}
Also used : QueryResult(com.google.cloud.bigquery.QueryResult) QueryRequest(com.google.cloud.bigquery.QueryRequest) QueryResponse(com.google.cloud.bigquery.QueryResponse) FieldValue(com.google.cloud.bigquery.FieldValue)

Example 5 with QueryResult

use of com.google.cloud.bigquery.QueryResult in project google-cloud-java by GoogleCloudPlatform.

the class ITBigQuerySnippets method testRunQueryWithParameters.

@Test
public void testRunQueryWithParameters() throws InterruptedException {
    QueryResponse queryResponse = bigquerySnippets.runQueryWithParameters(QUERY_WITH_PARAMETERS);
    assertNotNull(queryResponse);
    assertTrue(queryResponse.jobCompleted());
    assertFalse(queryResponse.hasErrors());
    QueryResult result = queryResponse.getResult();
    assertNotNull(result);
    assertTrue(bigquerySnippets.cancelJob(queryResponse.getJobId().getJob()));
}
Also used : QueryResult(com.google.cloud.bigquery.QueryResult) QueryResponse(com.google.cloud.bigquery.QueryResponse) Test(org.junit.Test)

Aggregations

QueryResponse (com.google.cloud.bigquery.QueryResponse)5 QueryResult (com.google.cloud.bigquery.QueryResult)5 FieldValue (com.google.cloud.bigquery.FieldValue)3 QueryRequest (com.google.cloud.bigquery.QueryRequest)3 Test (org.junit.Test)2