Search in sources :

Example 6 with QueryResponse

use of com.google.cloud.bigquery.QueryResponse 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 7 with QueryResponse

use of com.google.cloud.bigquery.QueryResponse in project java-docs-samples by GoogleCloudPlatform.

the class SimpleApp method main.

public static void main(String... args) throws Exception {
    // [START bigquery_simple_app_client]
    BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
    // [END bigquery_simple_app_client]
    // [START bigquery_simple_app_query]
    QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder("SELECT " + "CONCAT('https://stackoverflow.com/questions/', CAST(id as STRING)) as url, " + "view_count " + "FROM `bigquery-public-data.stackoverflow.posts_questions` " + "WHERE tags like '%google-bigquery%' " + "ORDER BY favorite_count DESC LIMIT 10").setUseLegacySql(false).build();
    // Create a job ID so that we can safely retry.
    JobId jobId = JobId.of(UUID.randomUUID().toString());
    Job queryJob = bigquery.create(JobInfo.newBuilder(queryConfig).setJobId(jobId).build());
    // Wait for the query to complete.
    queryJob = queryJob.waitFor();
    // Check for errors
    if (queryJob == null) {
        throw new RuntimeException("Job no longer exists");
    } else if (queryJob.getStatus().getError() != null) {
        // errors, not just the latest one.
        throw new RuntimeException(queryJob.getStatus().getError().toString());
    }
    // [END bigquery_simple_app_query]
    // [START bigquery_simple_app_print]
    // Get the results.
    QueryResponse response = bigquery.getQueryResults(jobId);
    TableResult result = queryJob.getQueryResults();
    // Print all pages of the results.
    for (FieldValueList row : result.iterateAll()) {
        String url = row.get("url").getStringValue();
        long viewCount = row.get("view_count").getLongValue();
        System.out.printf("url: %s views: %d%n", url, viewCount);
    }
// [END bigquery_simple_app_print]
}
Also used : BigQuery(com.google.cloud.bigquery.BigQuery) TableResult(com.google.cloud.bigquery.TableResult) QueryResponse(com.google.cloud.bigquery.QueryResponse) FieldValueList(com.google.cloud.bigquery.FieldValueList) Job(com.google.cloud.bigquery.Job) QueryJobConfiguration(com.google.cloud.bigquery.QueryJobConfiguration) JobId(com.google.cloud.bigquery.JobId)

Example 8 with QueryResponse

use of com.google.cloud.bigquery.QueryResponse in project components by Talend.

the class BigQueryDatasetRuntime method getSchema.

/**
 * Get the schema by table name or query. This method also needed for read and write, because we can not get schema
 * from the ongoing data. BigQueryIO.Read return TableRow, which do not include schema in itself. So use BigQuery
 * client to get it before read and write.
 *
 * @return
 */
@Override
public Schema getSchema() {
    BigQuery bigquery = BigQueryConnection.createClient(properties.getDatastoreProperties());
    com.google.cloud.bigquery.Schema bqRowSchema = null;
    switch(properties.sourceType.getValue()) {
        case TABLE_NAME:
            {
                TableId tableId = TableId.of(properties.getDatastoreProperties().projectName.getValue(), properties.bqDataset.getValue(), properties.tableName.getValue());
                Table table = bigquery.getTable(tableId);
                if (table == null) {
                    ComponentException.build(CommonErrorCodes.UNEXPECTED_EXCEPTION).setAndThrow("Table not found:" + tableId.toString());
                }
                bqRowSchema = table.getDefinition().getSchema();
                break;
            }
        case QUERY:
            {
                QueryRequest queryRequest = QueryRequest.newBuilder(properties.query.getValue()).setUseLegacySql(properties.useLegacySql.getValue()).build();
                QueryResponse queryResponse = bigquery.query(queryRequest);
                bqRowSchema = queryResponse.getResult().getSchema();
                break;
            }
        default:
            throw new RuntimeException("To be implemented: " + properties.sourceType.getValue());
    }
    return BigQueryAvroRegistry.get().inferSchema(bqRowSchema);
}
Also used : TableId(com.google.cloud.bigquery.TableId) BigQuery(com.google.cloud.bigquery.BigQuery) Table(com.google.cloud.bigquery.Table) QueryRequest(com.google.cloud.bigquery.QueryRequest) QueryResponse(com.google.cloud.bigquery.QueryResponse)

Example 9 with QueryResponse

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

the class ITBigQueryTest method queryAndWaitForResponse.

private QueryResponse queryAndWaitForResponse(QueryRequest request) throws InterruptedException {
    QueryResponse response = bigquery.query(request);
    while (!response.jobCompleted()) {
        Thread.sleep(1000);
        response = bigquery.getQueryResults(response.getJobId());
    }
    return response;
}
Also used : QueryResponse(com.google.cloud.bigquery.QueryResponse)

Example 10 with QueryResponse

use of com.google.cloud.bigquery.QueryResponse 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)16 QueryRequest (com.google.cloud.bigquery.QueryRequest)11 FieldValue (com.google.cloud.bigquery.FieldValue)9 Test (org.junit.Test)9 QueryResult (com.google.cloud.bigquery.QueryResult)5 TableId (com.google.cloud.bigquery.TableId)5 BigQuery (com.google.cloud.bigquery.BigQuery)3 Job (com.google.cloud.bigquery.Job)3 QueryParameterValue (com.google.cloud.bigquery.QueryParameterValue)3 Table (com.google.cloud.bigquery.Table)3 JobStatistics (com.google.cloud.bigquery.JobStatistics)2 QueryJobConfiguration (com.google.cloud.bigquery.QueryJobConfiguration)2 Schema (com.google.cloud.bigquery.Schema)2 TableInfo (com.google.cloud.bigquery.TableInfo)2 ExternalTableDefinition (com.google.cloud.bigquery.ExternalTableDefinition)1 Field (com.google.cloud.bigquery.Field)1 FieldValueList (com.google.cloud.bigquery.FieldValueList)1 InsertAllRequest (com.google.cloud.bigquery.InsertAllRequest)1 InsertAllResponse (com.google.cloud.bigquery.InsertAllResponse)1 JobId (com.google.cloud.bigquery.JobId)1