Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BigQuery: Standardizes region tags and adds query snippets from java-docs-samples #3180

Merged
merged 11 commits into from
Apr 24, 2018
Prev Previous commit
Next Next commit
adds sample for large query results
  • Loading branch information
alixhami committed Apr 23, 2018
commit 1e4dceda5284def80343ffb43751ffcd56f0ec2f
Original file line number Diff line number Diff line change
Expand Up @@ -767,12 +767,38 @@ public void runQueryPermanentTable(String destinationDataset, String destination
QueryJobConfiguration queryConfig =
// Note that setUseLegacySql is set to false by default
QueryJobConfiguration.newBuilder(query)
// Save the results of the query to a permanent table. See:
// https://cloud.google.com/bigquery/docs/writing-results#permanent-table
// Save the results of the query to a permanent table.
.setDestinationTable(TableId.of(destinationDataset, destinationTable))
.build();

// Print the results.
for (FieldValueList row : bigquery.query(queryConfig).iterateAll()) {
for (FieldValue val : row) {
System.out.printf("%s,", val.toString());
}
System.out.printf("\n");
}
// [END bigquery_query_destination_table]
}

/**
* Example of running a query and saving the results to a table.
*/
// [TARGET query(QueryJobConfiguration, JobOption...)]
public void runQueryLargeResults(String destinationDataset, String destinationTable) throws InterruptedException {
// [START bigquery_query_legacy_large_results]
// BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
// String destinationDataset = 'my_destination_dataset';
// String destinationTable = 'my_destination_table';
String query =
"SELECT corpus FROM [bigquery-public-data:samples.shakespeare] GROUP BY corpus;";
QueryJobConfiguration queryConfig =
// To use legacy SQL syntax, set useLegacySql to true.
QueryJobConfiguration.newBuilder(query).setUseLegacySql(true)
// Save the results of the query to a permanent table.
.setDestinationTable(TableId.of(destinationDataset, destinationTable))
// Allow results larger than the maximum response size.
// If true, a destination table must be set. See:
// https://cloud.google.com/bigquery/docs/writing-results#large-results
// If true, a destination table must be set.
.setAllowLargeResults(true)

This comment was marked as spam.

.build();

Expand All @@ -783,7 +809,7 @@ public void runQueryPermanentTable(String destinationDataset, String destination
}
System.out.printf("\n");
}
// [END bigquery_query_destination_table]
// [END bigquery_query_legacy_large_results]
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,14 @@ public void testRunQueryPermanentTable() throws InterruptedException {
assertTrue(got.contains("romeoandjuliet"));
}

@Test
public void testRunQueryLargeResults() throws InterruptedException {
String tableName = "test_large_results";
bigquerySnippets.runQueryLargeResults(DATASET, tableName);
String got = bout.toString();
assertTrue(got.contains("romeoandjuliet"));
}

@Test
public void testRunUncachedQuery() throws TimeoutException, InterruptedException {
bigquerySnippets.runUncachedQuery();
Expand Down