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;
}
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]
}
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);
}
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;
}
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()));
}
Aggregations