Skip to content

Commit

Permalink
Update BigQuery Samples.
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtisvg committed Jan 22, 2018
1 parent 819b008 commit 4455cb7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
import com.google.cloud.bigquery.FieldValue;
import com.google.cloud.bigquery.QueryJobConfiguration;
import com.google.cloud.bigquery.QueryParameterValue;
import com.google.cloud.bigquery.QueryResponse;
import com.google.cloud.bigquery.QueryResult;
import com.google.cloud.bigquery.TableResult;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -112,8 +111,7 @@ public static void main(final String[] args) throws IOException, InterruptedExce
// [START bigquery_query_params]
private static void runNamed(final String corpus, final long minWordCount)
throws InterruptedException {
BigQuery bigquery =
new BigQueryOptions.DefaultBigqueryFactory().create(BigQueryOptions.getDefaultInstance());
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();

String queryString =
"SELECT word, word_count\n"
Expand All @@ -131,25 +129,9 @@ private static void runNamed(final String corpus, final long minWordCount)
.build();

// Execute the query.
QueryResponse response = bigquery.query(queryRequest);

// Wait for the job to finish (if the query takes more than 10 seconds to complete).
while (!response.jobCompleted()) {
Thread.sleep(1000);
response = bigquery.getQueryResults(response.getJobId());
}

// Check for errors.
if (response.hasErrors()) {
String firstError = "";
if (response.getExecutionErrors().size() != 0) {
firstError = response.getExecutionErrors().get(0).getMessage();
}
throw new RuntimeException(firstError);
}
TableResult result = bigquery.query(queryRequest);

// Print all pages of the results.
QueryResult result = response.getResult();
while (result != null) {
for (List<FieldValue> row : result.iterateAll()) {
System.out.printf("%s: %d\n", row.get(0).getStringValue(), row.get(1).getLongValue());
Expand All @@ -165,8 +147,7 @@ private static void runNamed(final String corpus, final long minWordCount)
*/
// [START bigquery_query_params_arrays]
private static void runArray(String gender, String[] states) throws InterruptedException {
BigQuery bigquery =
new BigQueryOptions.DefaultBigqueryFactory().create(BigQueryOptions.getDefaultInstance());
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();

String queryString =
"SELECT name, sum(number) as count\n"
Expand All @@ -186,25 +167,9 @@ private static void runArray(String gender, String[] states) throws InterruptedE
.build();

// Execute the query.
QueryResponse response = bigquery.query(queryRequest);

// Wait for the job to finish (if the query takes more than 10 seconds to complete).
while (!response.jobCompleted()) {
Thread.sleep(1000);
response = bigquery.getQueryResults(response.getJobId());
}

// Check for errors.
if (response.hasErrors()) {
String firstError = "";
if (response.getExecutionErrors().size() != 0) {
firstError = response.getExecutionErrors().get(0).getMessage();
}
throw new RuntimeException(firstError);
}
TableResult result = bigquery.query(queryRequest);

// Print all pages of the results.
QueryResult result = response.getResult();
while (result != null) {
for (List<FieldValue> row : result.iterateAll()) {
System.out.printf("%s: %d\n", row.get(0).getStringValue(), row.get(1).getLongValue());
Expand All @@ -217,8 +182,7 @@ private static void runArray(String gender, String[] states) throws InterruptedE

// [START bigquery_query_params_timestamps]
private static void runTimestamp() throws InterruptedException {
BigQuery bigquery =
new BigQueryOptions.DefaultBigqueryFactory().create(BigQueryOptions.getDefaultInstance());
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();

DateTime timestamp = new DateTime(2016, 12, 7, 8, 0, 0, DateTimeZone.UTC);

Expand All @@ -236,25 +200,9 @@ private static void runTimestamp() throws InterruptedException {
.build();

// Execute the query.
QueryResponse response = bigquery.query(queryRequest);

// Wait for the job to finish (if the query takes more than 10 seconds to complete).
while (!response.jobCompleted()) {
Thread.sleep(1000);
response = bigquery.getQueryResults(response.getJobId());
}

// Check for errors.
if (response.hasErrors()) {
String firstError = "";
if (response.getExecutionErrors().size() != 0) {
firstError = response.getExecutionErrors().get(0).getMessage();
}
throw new RuntimeException(firstError);
}
TableResult result = bigquery.query(queryRequest);

// Print all pages of the results.
QueryResult result = response.getResult();
DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis().withZoneUTC();
while (result != null) {
for (List<FieldValue> row : result.iterateAll()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
import com.google.cloud.bigquery.JobId;
import com.google.cloud.bigquery.JobInfo;
import com.google.cloud.bigquery.QueryJobConfiguration;
import com.google.cloud.bigquery.QueryResponse;
import com.google.cloud.bigquery.QueryResult;
import com.google.cloud.bigquery.TableId;
import com.google.cloud.bigquery.TableResult;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
Expand Down Expand Up @@ -137,8 +136,7 @@ public static void runQuery(QueryJobConfiguration queryConfig)
}

// Get the results.
QueryResponse response = bigquery.getQueryResults(jobId);
QueryResult result = response.getResult();
TableResult result = queryJob.getQueryResults();

// Print all pages of the results.
while (result != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import com.google.cloud.bigquery.JobInfo;
import com.google.cloud.bigquery.QueryJobConfiguration;
import com.google.cloud.bigquery.QueryResponse;
import com.google.cloud.bigquery.QueryResult;
import com.google.cloud.bigquery.TableResult;
import java.util.UUID;
// [END bigquery_simple_app_deps]

Expand Down Expand Up @@ -71,7 +71,7 @@ public static void main(String... args) throws Exception {
// Get the results.
QueryResponse response = bigquery.getQueryResults(jobId);

QueryResult result = response.getResult();
TableResult result = queryJob.getQueryResults();

// Print all pages of the results.
for (FieldValueList row : result.iterateAll()) {
Expand Down

0 comments on commit 4455cb7

Please sign in to comment.