diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java index 907b75cff549..d229a26b5bae 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java @@ -633,6 +633,7 @@ public int hashCode() { * *

Example of listing datasets, specifying the page size. *

 {@code
+   * // List datasets in the default project
    * Page datasets = bigquery.listDatasets(DatasetListOption.pageSize(100));
    * for (Dataset dataset : datasets.iterateAll()) {
    *   // do something with the dataset
@@ -653,6 +654,7 @@ public int hashCode() {
    * 

Example of listing datasets in a project, specifying the page size. *

 {@code
    * String projectId = "my_project_id";
+   * // List datasets in a specified project
    * Page datasets = bigquery.listDatasets(projectId, DatasetListOption.pageSize(100));
    * for (Dataset dataset : datasets.iterateAll()) {
    *   // do something with the dataset
@@ -748,12 +750,12 @@ public int hashCode() {
   /**
    * Updates dataset information.
    *
-   * 

Example of updating a dataset by changing its friendly name. + *

Example of updating a dataset by changing its description. *

 {@code
    * String datasetName = "my_dataset_name";
-   * String newFriendlyName = "some_new_friendly_name";
+   * String newDescription = "some_new_description";
    * Dataset oldDataset = bigquery.getDataset(datasetName);
-   * DatasetInfo datasetInfo = oldDataset.toBuilder().setFriendlyName(newFriendlyName).build();
+   * DatasetInfo datasetInfo = oldDataset.toBuilder().setDescription(newDescription).build();
    * Dataset newDataset = bigquery.update(datasetInfo);
    * }
* @@ -764,13 +766,13 @@ public int hashCode() { /** * Updates table information. * - *

Example of updating a table by changing its friendly name. + *

Example of updating a table by changing its description. *

 {@code
    * String datasetName = "my_dataset_name";
    * String tableName = "my_table_name";
-   * String newFriendlyName = "new_friendly_name";
+   * String newDescription = "new_description";
    * Table oldTable = bigquery.getTable(datasetName, tableName);
-   * TableInfo tableInfo = oldTable.toBuilder().setFriendlyName(newFriendlyName).build();
+   * TableInfo tableInfo = oldTable.toBuilder().setDescription(newDescription).build();
    * Table newTable = bigquery.update(tableInfo);
    * }
* @@ -974,8 +976,7 @@ TableResult listTableData( * or "EU", {@link #getJob(JobId, JobOption...)} must be used instead. * *

Example of getting a job. - * - *

{@code
+   * 
 {@code
    * String jobName = "my_job_name";
    * Job job = bigquery.getJob(jobName);
    * if (job == null) {
@@ -992,8 +993,7 @@ TableResult listTableData(
    * or "EU", the {@code jobId} must specify the job location.
    *
    * 

Example of getting a job. - * - *

{@code
+   * 
 {@code
    * String jobName = "my_job_name";
    * JobId jobIdObject = JobId.of(jobName);
    * Job job = bigquery.getJob(jobIdObject);
@@ -1029,8 +1029,7 @@ TableResult listTableData(
    * 

If the location of the job is not "US" or "EU", {@link #cancel(JobId)} must be used instead. * *

Example of cancelling a job. - * - *

{@code
+   * 
 {@code
    * String jobName = "my_job_name";
    * boolean success = bigquery.cancel(jobName);
    * if (success) {
@@ -1055,8 +1054,7 @@ TableResult listTableData(
    * location.
    *
    * 

Example of cancelling a job. - * - *

{@code
+   * 
 {@code
    * String jobName = "my_job_name";
    * JobId jobId = JobId.of(jobName);
    * boolean success = bigquery.cancel(jobId);
@@ -1083,26 +1081,19 @@ TableResult listTableData(
    * queries. Since dry-run queries are not actually executed, there's no way to retrieve results.
    *
    * 

Example of running a query. - * - *

{@code
-   * String query = "SELECT unique(corpus) FROM [bigquery-public-data:samples.shakespeare]";
+   * 
 {@code
+   * // BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
+   * String query =
+   *     "SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;";
    * QueryJobConfiguration queryConfig =
-   *     QueryJobConfiguration.newBuilder(query).setUseLegacySql(true).build();
+   *     QueryJobConfiguration.newBuilder(query).build();
+   * 
+   * // Print the results.
    * for (FieldValueList row : bigquery.query(queryConfig).iterateAll()) {
-   *   // do something with the data
-   * }
-   * }
- * - *

Example of running a query with query parameters. - * - *

{@code
-   * String query = "SELECT distinct(corpus) FROM `bigquery-public-data.samples.shakespeare` where word_count > @wordCount";
-   * // Note, standard SQL is required to use query parameters. Legacy SQL will not work.
-   * QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query)
-   *     .addNamedParameter("wordCount", QueryParameterValue.int64(5))
-   *     .build();
-   * for (FieldValueList row : bigquery.query(queryConfig).iterateAll()) {
-   *   // do something with the data
+   *   for (FieldValue val : row) {
+   *     System.out.printf("%s,", val.toString());
+   *   }
+   *   System.out.printf("\n");
    * }
    * }
* @@ -1148,8 +1139,7 @@ TableResult query(QueryJobConfiguration configuration, JobId jobId, JobOption... * not in "US" or "EU", {@link #writer(JobId, WriteChannelConfiguration)} must be used instead. * *

Example of creating a channel with which to write to a table. - * - *

{@code
+   * 
 {@code
    * String datasetName = "my_dataset_name";
    * String tableName = "my_table_name";
    * String csvData = "StringValue1\nStringValue2\n";
@@ -1159,31 +1149,33 @@ TableResult query(QueryJobConfiguration configuration, JobId jobId, JobOption...
    *         .setFormatOptions(FormatOptions.csv())
    *         .build();
    * TableDataWriteChannel writer = bigquery.writer(writeChannelConfiguration);
-   *   // Write data to writer
-   *  try {
-   *     writer.write(ByteBuffer.wrap(csvData.getBytes(Charsets.UTF_8)));
-   *   } finally {
-   *     writer.close();
-   *   }
-   *   // Get load job
-   *   Job job = writer.getJob();
-   *   job = job.waitFor();
-   *   LoadStatistics stats = job.getStatistics();
-   *   return stats.getOutputRows();
+   * // Write data to writer
+   * try {
+   *   writer.write(ByteBuffer.wrap(csvData.getBytes(Charsets.UTF_8)));
+   * } finally {
+   *   writer.close();
+   * }
+   * // Get load job
+   * Job job = writer.getJob();
+   * job = job.waitFor();
+   * LoadStatistics stats = job.getStatistics();
+   * return stats.getOutputRows();
    * }
* *

Example of writing a local file to a table. - * - *

{@code
+   * 
 {@code
    * String datasetName = "my_dataset_name";
    * String tableName = "my_table_name";
    * Path csvPath = FileSystems.getDefault().getPath(".", "my-data.csv");
+   * String location = "us";
    * TableId tableId = TableId.of(datasetName, tableName);
    * WriteChannelConfiguration writeChannelConfiguration =
    *     WriteChannelConfiguration.newBuilder(tableId)
    *         .setFormatOptions(FormatOptions.csv())
    *         .build();
-   * TableDataWriteChannel writer = bigquery.writer(writeChannelConfiguration);
+   * // The location must be specified; other fields can be auto-detected.
+   * JobId jobId = JobId.newBuilder().setLocation(location).build();
+   * TableDataWriteChannel writer = bigquery.writer(jobId, writeChannelConfiguration);
    * // Write data to writer
    * try (OutputStream stream = Channels.newOutputStream(writer)) {
    *   Files.copy(csvPath, stream);
@@ -1205,13 +1197,11 @@ TableResult query(QueryJobConfiguration configuration, JobId jobId, JobOption...
    * not in "US" or "EU", the {@code jobId} must contain the location of the job.
    *
    * 

Example of creating a channel with which to write to a table. - * - *

{@code
+   * 
 {@code
    * String datasetName = "my_dataset_name";
    * String tableName = "my_table_name";
    * String csvData = "StringValue1\nStringValue2\n";
-   * String csvData = "StringValue1\nStringValue2\n";
-   * String location = "asia-northeast1";
+   * String location = "us";
    * TableId tableId = TableId.of(datasetName, tableName);
    * WriteChannelConfiguration writeChannelConfiguration =
    *     WriteChannelConfiguration.newBuilder(tableId).setFormatOptions(FormatOptions.csv()).build();
@@ -1230,6 +1220,7 @@ TableResult query(QueryJobConfiguration configuration, JobId jobId, JobOption...
    * LoadStatistics stats = job.getStatistics();
    * return stats.getOutputRows();
    * }
+ * */ TableDataWriteChannel writer(JobId jobId, WriteChannelConfiguration writeChannelConfiguration); } diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Dataset.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Dataset.java index fda29cf96354..12208a645faf 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Dataset.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Dataset.java @@ -240,9 +240,7 @@ public boolean delete(DatasetDeleteOption... options) { *

Example of listing tables in the dataset. *

 {@code
    * Page tables = dataset.list();
-   * Iterator
tableIterator = tables.iterateAll(); - * while (tableIterator.hasNext()) { - * Table table = tableIterator.next(); + * for (Table table : tables.iterateAll()) { * // do something with the table * } * } @@ -278,7 +276,7 @@ public Table get(String tableId, TableOption... options) { *
 {@code
    * String tableName = “my_table”;
    * String fieldName = “my_field”;
-   * Schema schema = Schema.of(Field.of(fieldName, Type.string()));
+   * Schema schema = Schema.of(Field.of(fieldName, LegacySQLTypeName.STRING));
    * StandardTableDefinition definition = StandardTableDefinition.newBuilder()
    *     .setSchema(schema)
    *     .setTimePartitioning(TimePartitioning.of(TimePartitioning.Type.DAY))
diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Job.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Job.java
index 738d5d575303..9f85ffc73449 100644
--- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Job.java
+++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Job.java
@@ -154,8 +154,7 @@ public Job build() {
    * Checks if this job exists.
    *
    * 

Example of checking that a job exists. - * - *

{@code
+   * 
 {@code
    * if (!job.exists()) {
    *   // job doesn't exist
    * }
@@ -174,8 +173,7 @@ public boolean exists() {
    * not exist this method returns {@code true}.
    *
    * 

Example of waiting for a job until it reports that it is done. - * - *

{@code
+   * 
 {@code
    * while (!job.isDone()) {
    *   Thread.sleep(1000L);
    * }
@@ -198,8 +196,7 @@ public boolean isDone() {
    * 12 hours as a total timeout and unlimited number of attempts.
    *
    * 

Example usage of {@code waitFor()}. - * - *

{@code
+   * 
 {@code
    * Job completedJob = job.waitFor();
    * if (completedJob == null) {
    *   // job no longer exists
@@ -210,14 +207,12 @@ public boolean isDone() {
    * }
    * }
* - *

Example usage of {@code waitFor()} with non-jittered custom max delay and total timeout. - * - *

{@code
+   * 

Example usage of {@code waitFor()} with checking period and timeout. + *

 {@code
    * Job completedJob =
    *     job.waitFor(
-   *         RetryOption.maxRetryDelay(Duration.ofSeconds(30)),
-   *         RetryOption.totalTimeout(Duration.ofMinutes(1)),
-   *         RetryOption.jittered(false));
+   *         RetryOption.initialRetryDelay(Duration.ofSeconds(1)),
+   *         RetryOption.totalTimeout(Duration.ofMinutes(1)));
    * if (completedJob == null) {
    *   // job no longer exists
    * } else if (completedJob.getStatus().getError() != null) {
@@ -257,16 +252,6 @@ public Job waitFor(RetryOption... waitOptions) throws InterruptedException {
    * the current {@code Job} instance is not updated. To get the new state, call {@link
    * #waitFor(RetryOption...)} or {@link #reload(JobOption...)}.
    *
-   * 

Example of getting the results of a query job. - * - *

{@code
-   * Job job = bigquery.create(queryJobInfo);
-   * TableResult result = job.getQueryResults();
-   * for (FieldValueList row : result.iterateAll()) {
-   *   // do something with the data
-   * }
-   * }
- * * @throws BigQueryException upon failure */ public TableResult getQueryResults(QueryResultsOption... options) @@ -371,8 +356,7 @@ public boolean shouldRetry(Throwable prevThrowable, Job prevResponse) { * Fetches current job's latest information. Returns {@code null} if the job does not exist. * *

Example of reloading all fields until job status is DONE. - * - *

{@code
+   * 
 {@code
    * while (job.getStatus().getState() != JobStatus.State.DONE) {
    *   Thread.sleep(1000L);
    *   job = job.reload();
@@ -380,8 +364,7 @@ public boolean shouldRetry(Throwable prevThrowable, Job prevResponse) {
    * }
* *

Example of reloading status field until job status is DONE. - * - *

{@code
+   * 
 {@code
    * while (job.getStatus().getState() != JobStatus.State.DONE) {
    *   Thread.sleep(1000L);
    *   job = job.reload(BigQuery.JobOption.fields(BigQuery.JobField.STATUS));
@@ -401,8 +384,7 @@ public Job reload(JobOption... options) {
    * Sends a job cancel request.
    *
    * 

Example of cancelling a job. - * - *

{@code
+   * 
 {@code
    * if (job.cancel()) {
    *   return true; // job successfully cancelled
    * } else {
diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Table.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Table.java
index fc43df9377bf..ba45b7da56ab 100644
--- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Table.java
+++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Table.java
@@ -301,11 +301,10 @@ public InsertAllResponse insert(Iterable rows,
    * Returns the paginated list rows in this table.
    *
    * 

Example of listing rows in the table. - * - *

{@code
+   * 
 {@code
    * // This example reads the result 100 rows per RPC call. If there's no need to limit the number,
    * // simply omit the option.
-   * TableResult page = table.list(TableDataListOption.pageSize(100));
+   * Page page = table.list(TableDataListOption.pageSize(100));
    * for (FieldValueList row : page.iterateAll()) {
    *   // do something with the row
    * }
@@ -321,12 +320,11 @@ public TableResult list(TableDataListOption... options) throws BigQueryException
   /**
    * Returns the paginated list rows in this table.
    *
-   * 

Example of listing rows in the table. - * - *

{@code
+   * 

Example of listing rows in the table given a schema. + *

 {@code
    * Schema schema = ...;
    * String field = "my_field";
-   * TableResult page = table.list(schema);
+   * Page page = table.list(schema);
    * for (FieldValueList row : page.iterateAll()) {
    *   row.get(field);
    * }
@@ -351,14 +349,14 @@ public TableResult list(Schema schema, TableDataListOption... options)
    * Job job = table.copy(datasetName, tableName);
    * // Wait for the job to complete.
    * try {
-   *   Job completedJob = job.waitFor(RetryOption.checkEvery(1, TimeUnit.SECONDS),
-   *       RetryOption.timeout(3, TimeUnit.MINUTES));
+   *   Job completedJob = job.waitFor(RetryOption.initialRetryDelay(Duration.ofSeconds(1)),
+   *       RetryOption.totalTimeout(Duration.ofMinutes(3)));
    *   if (completedJob != null && completedJob.getStatus().getError() == null) {
    *     // Job completed successfully
    *   } else {
    *     // Handle error case
    *   }
-   * } catch (InterruptedException | TimeoutException e) {
+   * } catch (InterruptedException e) {
    *   // Handle interrupted wait
    * }
    * }
@@ -386,14 +384,14 @@ public Job copy(String destinationDataset, String destinationTable, JobOption... * Job job = table.copy(destinationId, options); * // Wait for the job to complete. * try { - * Job completedJob = job.waitFor(RetryOption.checkEvery(1, TimeUnit.SECONDS), - * RetryOption.timeout(3, TimeUnit.MINUTES)); + * Job completedJob = job.waitFor(RetryOption.initialRetryDelay(Duration.ofSeconds(1)), + * RetryOption.totalTimeout(Duration.ofMinutes(3))); * if (completedJob != null && completedJob.getStatus().getError() == null) { * // Job completed successfully. * } else { * // Handle error case. * } - * } catch (InterruptedException | TimeoutException e) { + * } catch (InterruptedException e) { * // Handle interrupted wait * } * }
@@ -419,14 +417,14 @@ public Job copy(TableId destinationTable, JobOption... options) * Job job = table.extract(format, gcsUrl); * // Wait for the job to complete * try { - * Job completedJob = job.waitFor(RetryOption.checkEvery(1, TimeUnit.SECONDS), - * RetryOption.timeout(3, TimeUnit.MINUTES)); + * Job completedJob = job.waitFor(RetryOption.initialRetryDelay(Duration.ofSeconds(1)), + * RetryOption.totalTimeout(Duration.ofMinutes(3))); * if (completedJob != null && completedJob.getStatus().getError() == null) { * // Job completed successfully * } else { * // Handle error case * } - * } catch (InterruptedException | TimeoutException e) { + * } catch (InterruptedException e) { * // Handle interrupted wait * } * }
@@ -457,14 +455,14 @@ public Job extract(String format, String destinationUri, JobOption... options) * Job job = table.extract(format, destinationUris); * // Wait for the job to complete * try { - * Job completedJob = job.waitFor(RetryOption.checkEvery(1, TimeUnit.SECONDS), - * RetryOption.timeout(3, TimeUnit.MINUTES)); + * Job completedJob = job.waitFor(RetryOption.initialRetryDelay(Duration.ofSeconds(1)), + * RetryOption.totalTimeout(Duration.ofMinutes(3))); * if (completedJob != null && completedJob.getStatus().getError() == null) { * // Job completed successfully * } else { * // Handle error case * } - * } catch (InterruptedException | TimeoutException e) { + * } catch (InterruptedException e) { * // Handle interrupted wait * } * }
@@ -492,14 +490,14 @@ public Job extract(String format, List destinationUris, JobOption... opt * Job job = table.load(FormatOptions.csv(), sourceUri); * // Wait for the job to complete * try { - * Job completedJob = job.waitFor(RetryOption.checkEvery(1, TimeUnit.SECONDS), - * RetryOption.timeout(3, TimeUnit.MINUTES)); + * Job completedJob = job.waitFor(RetryOption.initialRetryDelay(Duration.ofSeconds(1)), + * RetryOption.totalTimeout(Duration.ofMinutes(3))); * if (completedJob != null && completedJob.getStatus().getError() == null) { * // Job completed successfully * } else { * // Handle error case * } - * } catch (InterruptedException | TimeoutException e) { + * } catch (InterruptedException e) { * // Handle interrupted wait * } * }
@@ -529,14 +527,14 @@ public Job load(FormatOptions format, String sourceUri, JobOption... options) * Job job = table.load(FormatOptions.csv(), sourceUris); * // Wait for the job to complete * try { - * Job completedJob = job.waitFor(RetryOption.checkEvery(1, TimeUnit.SECONDS), - * RetryOption.timeout(3, TimeUnit.MINUTES)); + * Job completedJob = job.waitFor(RetryOption.initialRetryDelay(Duration.ofSeconds(1)), + * RetryOption.totalTimeout(Duration.ofMinutes(3))); * if (completedJob != null && completedJob.getStatus().getError() == null) { * // Job completed successfully * } else { * // Handle error case * } - * } catch (InterruptedException | TimeoutException e) { + * } catch (InterruptedException e) { * // Handle interrupted wait * } * }
diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/BigQuerySnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/BigQuerySnippets.java index 6b9f29a142ff..2763493c1a3a 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/BigQuerySnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/BigQuerySnippets.java @@ -36,6 +36,7 @@ import com.google.cloud.bigquery.DatasetId; import com.google.cloud.bigquery.DatasetInfo; import com.google.cloud.bigquery.Field; +import com.google.cloud.bigquery.FieldValue; import com.google.cloud.bigquery.FieldValueList; import com.google.cloud.bigquery.FormatOptions; import com.google.cloud.bigquery.InsertAllRequest; @@ -49,7 +50,6 @@ import com.google.cloud.bigquery.LegacySQLTypeName; import com.google.cloud.bigquery.LoadJobConfiguration; import com.google.cloud.bigquery.QueryJobConfiguration; -import com.google.cloud.bigquery.QueryParameterValue; import com.google.cloud.bigquery.Schema; import com.google.cloud.bigquery.StandardTableDefinition; import com.google.cloud.bigquery.Table; @@ -88,7 +88,7 @@ public BigQuerySnippets(BigQuery bigquery) { // [TARGET create(DatasetInfo, DatasetOption...)] // [VARIABLE "my_dataset_name"] public Dataset createDataset(String datasetName) { - // [START createDataset] + // [START bigquery_create_dataset] Dataset dataset = null; DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetName).build(); try { @@ -97,38 +97,38 @@ public Dataset createDataset(String datasetName) { } catch (BigQueryException e) { // the dataset was not created } - // [END createDataset] + // [END bigquery_create_dataset] return dataset; } /** - * Example of updating a dataset by changing its friendly name. + * Example of updating a dataset by changing its description. */ // [TARGET update(DatasetInfo, DatasetOption...)] // [VARIABLE "my_dataset_name"] - // [VARIABLE "some_new_friendly_name"] - public Dataset updateDataset(String datasetName, String newFriendlyName) { - // [START updateDataset] + // [VARIABLE "some_new_description"] + public Dataset updateDataset(String datasetName, String newDescription) { + // [START bigquery_update_dataset_description] Dataset oldDataset = bigquery.getDataset(datasetName); - DatasetInfo datasetInfo = oldDataset.toBuilder().setFriendlyName(newFriendlyName).build(); + DatasetInfo datasetInfo = oldDataset.toBuilder().setDescription(newDescription).build(); Dataset newDataset = bigquery.update(datasetInfo); - // [END updateDataset] + // [END bigquery_update_dataset_description] return newDataset; } /** - * Example of updating a table by changing its friendly name. + * Example of updating a table by changing its description. */ // [TARGET update(TableInfo, TableOption...)] // [VARIABLE "my_dataset_name"] // [VARIABLE "my_table_name"] - // [VARIABLE "new_friendly_name"] - public Table updateTable(String datasetName, String tableName, String newFriendlyName) { - // [START updateTable] + // [VARIABLE "new_description"] + public Table updateTable(String datasetName, String tableName, String newDescription) { + // [START bigquery_update_table_description] Table oldTable = bigquery.getTable(datasetName, tableName); - TableInfo tableInfo = oldTable.toBuilder().setFriendlyName(newFriendlyName).build(); + TableInfo tableInfo = oldTable.toBuilder().setDescription(newDescription).build(); Table newTable = bigquery.update(tableInfo); - // [END updateTable] + // [END bigquery_update_table_description] return newTable; } @@ -137,12 +137,13 @@ public Table updateTable(String datasetName, String tableName, String newFriendl */ // [TARGET listDatasets(DatasetListOption...)] public Page listDatasets() { - // [START listDatasets] + // [START bigquery_list_datasets] + // List datasets in the default project Page datasets = bigquery.listDatasets(DatasetListOption.pageSize(100)); for (Dataset dataset : datasets.iterateAll()) { // do something with the dataset } - // [END listDatasets] + // [END bigquery_list_datasets] return datasets; } @@ -152,12 +153,13 @@ public Page listDatasets() { // [TARGET listDatasets(String, DatasetListOption...)] // [VARIABLE "my_project_id"] public Page listDatasets(String projectId) { - // [START listDatasets] + // [START bigquery_list_datasets] + // List datasets in a specified project Page datasets = bigquery.listDatasets(projectId, DatasetListOption.pageSize(100)); for (Dataset dataset : datasets.iterateAll()) { // do something with the dataset } - // [END listDatasets] + // [END bigquery_list_datasets] return datasets; } @@ -167,14 +169,14 @@ public Page listDatasets(String projectId) { // [TARGET delete(String, DatasetDeleteOption...)] // [VARIABLE "my_dataset_name"] public Boolean deleteDataset(String datasetName) { - // [START deleteDataset] + // [START ] Boolean deleted = bigquery.delete(datasetName, DatasetDeleteOption.deleteContents()); if (deleted) { // the dataset was deleted } else { // the dataset was not found } - // [END deleteDataset] + // [END ] return deleted; } @@ -185,7 +187,7 @@ public Boolean deleteDataset(String datasetName) { // [VARIABLE "my_project_id"] // [VARIABLE "my_dataset_name"] public Boolean deleteDatasetFromId(String projectId, String datasetName) { - // [START deleteDatasetFromId] + // [START bigquery_delete_dataset] DatasetId datasetId = DatasetId.of(projectId, datasetName); Boolean deleted = bigquery.delete(datasetId, DatasetDeleteOption.deleteContents()); if (deleted) { @@ -193,7 +195,7 @@ public Boolean deleteDatasetFromId(String projectId, String datasetName) { } else { // the dataset was not found } - // [END deleteDatasetFromId] + // [END bigquery_delete_dataset] return deleted; } @@ -204,14 +206,14 @@ public Boolean deleteDatasetFromId(String projectId, String datasetName) { // [VARIABLE "my_dataset_name"] // [VARIABLE "my_table_name"] public Boolean deleteTable(String datasetName, String tableName) { - // [START deleteTable] + // [START ] Boolean deleted = bigquery.delete(datasetName, tableName); if (deleted) { // the table was deleted } else { // the table was not found } - // [END deleteTable] + // [END ] return deleted; } @@ -223,7 +225,7 @@ public Boolean deleteTable(String datasetName, String tableName) { // [VARIABLE "my_dataset_name"] // [VARIABLE "my_table_name"] public Boolean deleteTableFromId(String projectId, String datasetName, String tableName) { - // [START deleteTableFromId] + // [START bigquery_delete_table] TableId tableId = TableId.of(projectId, datasetName, tableName); Boolean deleted = bigquery.delete(tableId); if (deleted) { @@ -231,7 +233,7 @@ public Boolean deleteTableFromId(String projectId, String datasetName, String ta } else { // the table was not found } - // [END deleteTableFromId] + // [END bigquery_delete_table] return deleted; } @@ -241,16 +243,15 @@ public Boolean deleteTableFromId(String projectId, String datasetName, String ta // [TARGET listTables(String, TableListOption...)] // [VARIABLE "my_dataset_name"] public Page
listTables(String datasetName) { - // [START listTables] + // [START ] Page
tables = bigquery.listTables(datasetName, TableListOption.pageSize(100)); for (Table table : tables.iterateAll()) { // do something with the table } - // [END listTables] + // [END ] return tables; } - /** * Example of listing the tables in a dataset. */ @@ -258,13 +259,13 @@ public Page
listTables(String datasetName) { // [VARIABLE "my_project_id"] // [VARIABLE "my_dataset_name"] public Page
listTablesFromId(String projectId, String datasetName) { - // [START listTablesFromId] + // [START bigquery_list_tables] DatasetId datasetId = DatasetId.of(projectId, datasetName); Page
tables = bigquery.listTables(datasetId, TableListOption.pageSize(100)); for (Table table : tables.iterateAll()) { // do something with the table } - // [END listTablesFromId] + // [END bigquery_list_tables] return tables; } @@ -274,9 +275,9 @@ public Page
listTablesFromId(String projectId, String datasetName) { // [TARGET getDataset(String, DatasetOption...)] // [VARIABLE "my_dataset"] public Dataset getDataset(String datasetName) { - // [START getDataset] + // [START ] Dataset dataset = bigquery.getDataset(datasetName); - // [END getDataset] + // [END ] return dataset; } @@ -287,10 +288,10 @@ public Dataset getDataset(String datasetName) { // [VARIABLE "my_project_id"] // [VARIABLE "my_dataset_name"] public Dataset getDatasetFromId(String projectId, String datasetName) { - // [START getDatasetFromId] + // [START bigquery_get_dataset] DatasetId datasetId = DatasetId.of(projectId, datasetName); Dataset dataset = bigquery.getDataset(datasetId); - // [END getDatasetFromId] + // [END bigquery_get_dataset] return dataset; } @@ -301,9 +302,9 @@ public Dataset getDatasetFromId(String projectId, String datasetName) { // [VARIABLE "my_dataset_name"] // [VARIABLE "my_table_name"] public Table getTable(String datasetName, String tableName) { - // [START getTable] + // [START ] Table table = bigquery.getTable(datasetName, tableName); - // [END getTable] + // [END ] return table; } @@ -315,10 +316,10 @@ public Table getTable(String datasetName, String tableName) { // [VARIABLE "my_dataset_name"] // [VARIABLE "my_table_name"] public Table getTableFromId(String projectId, String datasetName, String tableName) { - // [START getTableFromId] + // [START bigquery_get_table] TableId tableId = TableId.of(projectId, datasetName, tableName); Table table = bigquery.getTable(tableId); - // [END getTableFromId] + // [END bigquery_get_table] return table; } @@ -331,37 +332,37 @@ public Table getTableFromId(String projectId, String datasetName, String tableNa // [VARIABLE "StringValue1\nStringValue2\n"] public long writeToTable(String datasetName, String tableName, String csvData) throws IOException, InterruptedException, TimeoutException { - // [START writeToTable] + // [START ] TableId tableId = TableId.of(datasetName, tableName); WriteChannelConfiguration writeChannelConfiguration = - WriteChannelConfiguration.newBuilder(tableId) - .setFormatOptions(FormatOptions.csv()) - .build(); + WriteChannelConfiguration.newBuilder(tableId).setFormatOptions(FormatOptions.csv()).build(); TableDataWriteChannel writer = bigquery.writer(writeChannelConfiguration); - // Write data to writer - try { - writer.write(ByteBuffer.wrap(csvData.getBytes(Charsets.UTF_8))); - } finally { - writer.close(); - } - // Get load job - Job job = writer.getJob(); - job = job.waitFor(); - LoadStatistics stats = job.getStatistics(); - return stats.getOutputRows(); - // [END writeToTable] + // Write data to writer + try { + writer.write(ByteBuffer.wrap(csvData.getBytes(Charsets.UTF_8))); + } finally { + writer.close(); } + // Get load job + Job job = writer.getJob(); + job = job.waitFor(); + LoadStatistics stats = job.getStatistics(); + return stats.getOutputRows(); + // [END ] + } - /** Example of creating a channel with which to write to a table. */ - // [TARGET writer(WriteChannelConfiguration)] + /** + * Example of creating a channel with which to write to a table. + */ + // [TARGET writer(JobId, WriteChannelConfiguration)] // [VARIABLE "my_dataset_name"] // [VARIABLE "my_table_name"] // [VARIABLE "StringValue1\nStringValue2\n"] - // [VARIABLE "asia-northeast1"] + // [VARIABLE "us"] public long writeToTableLocation( String datasetName, String tableName, String csvData, String location) throws IOException, InterruptedException, TimeoutException { - // [START writeToTableLocation] + // [START ] TableId tableId = TableId.of(datasetName, tableName); WriteChannelConfiguration writeChannelConfiguration = WriteChannelConfiguration.newBuilder(tableId).setFormatOptions(FormatOptions.csv()).build(); @@ -379,7 +380,7 @@ public long writeToTableLocation( job = job.waitFor(); LoadStatistics stats = job.getStatistics(); return stats.getOutputRows(); - // [END writeToTableLocation] + // [END ] } /** @@ -389,15 +390,16 @@ public long writeToTableLocation( // [VARIABLE "my_dataset_name"] // [VARIABLE "my_table_name"] // [VARIABLE FileSystems.getDefault().getPath(".", "my-data.csv")] - public long writeFileToTable(String datasetName, String tableName, Path csvPath) + // [VARIABLE "us"] + public long writeFileToTable(String datasetName, String tableName, Path csvPath, String location) throws IOException, InterruptedException, TimeoutException { - // [START writeFileToTable] + // [START bigquery_load_from_file] TableId tableId = TableId.of(datasetName, tableName); WriteChannelConfiguration writeChannelConfiguration = - WriteChannelConfiguration.newBuilder(tableId) - .setFormatOptions(FormatOptions.csv()) - .build(); - TableDataWriteChannel writer = bigquery.writer(writeChannelConfiguration); + WriteChannelConfiguration.newBuilder(tableId).setFormatOptions(FormatOptions.csv()).build(); + // The location must be specified; other fields can be auto-detected. + JobId jobId = JobId.newBuilder().setLocation(location).build(); + TableDataWriteChannel writer = bigquery.writer(jobId, writeChannelConfiguration); // Write data to writer try (OutputStream stream = Channels.newOutputStream(writer)) { Files.copy(csvPath, stream); @@ -407,7 +409,7 @@ public long writeFileToTable(String datasetName, String tableName, Path csvPath) job = job.waitFor(); LoadStatistics stats = job.getStatistics(); return stats.getOutputRows(); - // [END writeFileToTable] + // [END bigquery_load_from_file] } /** @@ -422,17 +424,19 @@ public Long writeRemoteFileToTable(String datasetName, String tableName) String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.json"; TableId tableId = TableId.of(datasetName, tableName); // Table field definition - Field[] fields = new Field[] { - Field.of("name", LegacySQLTypeName.STRING), - Field.of("post_abbr", LegacySQLTypeName.STRING) - }; + Field[] fields = + new Field[] { + Field.of("name", LegacySQLTypeName.STRING), + Field.of("post_abbr", LegacySQLTypeName.STRING) + }; // Table schema definition Schema schema = Schema.of(fields); - LoadJobConfiguration configuration = LoadJobConfiguration.builder(tableId, sourceUri) - .setFormatOptions(FormatOptions.json()) - .setCreateDisposition(CreateDisposition.CREATE_IF_NEEDED) - .setSchema(schema) - .build(); + LoadJobConfiguration configuration = + LoadJobConfiguration.builder(tableId, sourceUri) + .setFormatOptions(FormatOptions.json()) + .setCreateDisposition(CreateDisposition.CREATE_IF_NEEDED) + .setSchema(schema) + .build(); // Load the table Job remoteLoadJob = bigquery.create(JobInfo.of(configuration)); remoteLoadJob = remoteLoadJob.waitFor(); @@ -449,7 +453,7 @@ public Long writeRemoteFileToTable(String datasetName, String tableName) // [VARIABLE "my_dataset_name"] // [VARIABLE "my_table_name"] public InsertAllResponse insertAll(String datasetName, String tableName) { - // [START insertAll] + // [START bigquery_table_insert_rows] TableId tableId = TableId.of(datasetName, tableName); // Values of the row to insert Map rowContent = new HashMap<>(); @@ -460,17 +464,19 @@ public InsertAllResponse insertAll(String datasetName, String tableName) { Map recordsContent = new HashMap<>(); recordsContent.put("stringField", "Hello, World!"); rowContent.put("recordField", recordsContent); - InsertAllResponse response = bigquery.insertAll(InsertAllRequest.newBuilder(tableId) - .addRow("rowId", rowContent) - // More rows can be added in the same RPC by invoking .addRow() on the builder - .build()); + InsertAllResponse response = + bigquery.insertAll( + InsertAllRequest.newBuilder(tableId) + .addRow("rowId", rowContent) + // More rows can be added in the same RPC by invoking .addRow() on the builder + .build()); if (response.hasErrors()) { // If any of the insertions failed, this lets you inspect the errors for (Entry> entry : response.getInsertErrors().entrySet()) { // inspect row error } } - // [END insertAll] + // [END bigquery_table_insert_rows] return response; } @@ -482,7 +488,7 @@ public InsertAllResponse insertAll(String datasetName, String tableName) { // [VARIABLE "my_table_name"] // [VARIABLE "string_field"] public Table createTable(String datasetName, String tableName, String fieldName) { - // [START createTable] + // [START bigquery_create_table] TableId tableId = TableId.of(datasetName, tableName); // Table field definition Field field = Field.of(fieldName, LegacySQLTypeName.STRING); @@ -491,7 +497,7 @@ public Table createTable(String datasetName, String tableName, String fieldName) TableDefinition tableDefinition = StandardTableDefinition.of(schema); TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build(); Table table = bigquery.create(tableInfo); - // [END createTable] + // [END bigquery_create_table] return table; } @@ -502,7 +508,7 @@ public Table createTable(String datasetName, String tableName, String fieldName) // [VARIABLE "my_dataset_name"] // [VARIABLE "my_table_name"] public TableResult listTableData(String datasetName, String tableName) { - // [START listTableData] + // [START ] // This example reads the result 100 rows per RPC call. If there's no need to limit the number, // simply omit the option. TableResult tableData = @@ -510,7 +516,7 @@ public TableResult listTableData(String datasetName, String tableName) { for (FieldValueList row : tableData.iterateAll()) { // do something with the row } - // [END listTableData] + // [END ] return tableData; } @@ -521,7 +527,7 @@ public TableResult listTableData(String datasetName, String tableName) { // [VARIABLE "my_dataset_name"] // [VARIABLE "my_table_name"] public TableResult listTableDataFromId(String datasetName, String tableName) { - // [START listTableDataFromId] + // [START bigquery_browse_table] TableId tableIdObject = TableId.of(datasetName, tableName); // This example reads the result 100 rows per RPC call. If there's no need to limit the number, // simply omit the option. @@ -530,7 +536,7 @@ public TableResult listTableDataFromId(String datasetName, String tableName) { for (FieldValueList row : tableData.iterateAll()) { // do something with the row } - // [END listTableDataFromId] + // [END bigquery_browse_table] return tableData; } @@ -544,13 +550,12 @@ public TableResult listTableDataFromId(String datasetName, String tableName) { // [VARIABLE "field"] public TableResult listTableDataSchema( String datasetName, String tableName, Schema schema, String field) { - // [START listTableDataSchema] - TableResult tableData = - bigquery.listTableData(datasetName, tableName, schema); + // [START ] + TableResult tableData = bigquery.listTableData(datasetName, tableName, schema); for (FieldValueList row : tableData.iterateAll()) { row.get(field); } - // [END listTableDataSchema] + // [END ] return tableData; } @@ -559,7 +564,7 @@ public TableResult listTableDataSchema( */ // [TARGET listTableData(TableId, Schema, TableDataListOption...)] public FieldValueList listTableDataSchemaId() { - // [START listTableDataSchemaId] + // [START ] Schema schema = Schema.of( Field.of("word", LegacySQLTypeName.STRING), @@ -571,7 +576,7 @@ public FieldValueList listTableDataSchemaId() { TableId.of("bigquery-public-data", "samples", "shakespeare"), schema); FieldValueList row = tableData.getValues().iterator().next(); System.out.println(row.get("word").getStringValue()); - // [END listTableDataSchemaId] + // [END ] return row; } @@ -581,7 +586,7 @@ public FieldValueList listTableDataSchemaId() { // [TARGET create(JobInfo, JobOption...)] // [VARIABLE "SELECT field FROM my_dataset_name.my_table_name"] public Job createJob(String query) { - // [START createJob] + // [START ] Job job = null; JobConfiguration jobConfiguration = QueryJobConfiguration.of(query); JobInfo jobInfo = JobInfo.of(jobConfiguration); @@ -590,7 +595,7 @@ public Job createJob(String query) { } catch (BigQueryException e) { // the job was not created } - // [END createJob] + // [END ] return job; } @@ -599,12 +604,12 @@ public Job createJob(String query) { */ // [TARGET listJobs(JobListOption...)] public Page listJobs() { - // [START listJobs] + // [START bigquery_list_jobs] Page jobs = bigquery.listJobs(JobListOption.pageSize(100)); for (Job job : jobs.iterateAll()) { // do something with the job } - // [END listJobs] + // [END bigquery_list_jobs] return jobs; } @@ -614,12 +619,12 @@ public Page listJobs() { // [TARGET getJob(String, JobOption...)] // [VARIABLE "my_job_name"] public Job getJob(String jobName) { - // [START getJob] + // [START ] Job job = bigquery.getJob(jobName); if (job == null) { // job was not found } - // [END getJob] + // [END ] return job; } @@ -629,31 +634,30 @@ public Job getJob(String jobName) { // [TARGET getJob(JobId, JobOption...)] // [VARIABLE "my_job_name"] public Job getJobFromId(String jobName) { - // [START getJobFromId] + // [START ] JobId jobIdObject = JobId.of(jobName); Job job = bigquery.getJob(jobIdObject); if (job == null) { // job was not found } - // [END getJobFromId] + // [END ] return job; } - /** * Example of cancelling a job. */ // [TARGET cancel(String)] // [VARIABLE "my_job_name"] public boolean cancelJob(String jobName) { - // [START cancelJob] + // [START ] boolean success = bigquery.cancel(jobName); if (success) { // job was cancelled } else { // job was not found } - // [END cancelJob] + // [END ] return success; } @@ -663,7 +667,7 @@ public boolean cancelJob(String jobName) { // [TARGET cancel(JobId)] // [VARIABLE "my_job_name"] public boolean cancelJobFromId(String jobName) { - // [START cancelJobFromId] + // [START ] JobId jobId = JobId.of(jobName); boolean success = bigquery.cancel(jobId); if (success) { @@ -671,7 +675,7 @@ public boolean cancelJobFromId(String jobName) { } else { // job was not found } - // [END cancelJobFromId] + // [END ] return success; } @@ -679,31 +683,19 @@ public boolean cancelJobFromId(String jobName) { * Example of running a query. */ // [TARGET query(QueryJobConfiguration, JobOption...)] - // [VARIABLE "SELECT unique(corpus) FROM [bigquery-public-data:samples.shakespeare]"] - public void runQuery(String query) throws InterruptedException { - // [START runQuery] - QueryJobConfiguration queryConfig = - QueryJobConfiguration.newBuilder(query).setUseLegacySql(true).build(); - for (FieldValueList row : bigquery.query(queryConfig).iterateAll()) { - // do something with the data - } - // [END runQuery] - } + public void runQuery() throws InterruptedException { + // [START bigquery_query] + // BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + String query = "SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;"; + QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query).build(); - /** - * Example of running a query with query parameters. - */ - // [TARGET query(QueryJobConfiguration, JobOption...)] - // [VARIABLE "SELECT distinct(corpus) FROM `bigquery-public-data.samples.shakespeare` where word_count > @wordCount"] - public void runQueryWithParameters(String query) throws InterruptedException { - // [START runQueryWithParameters] - // Note, standard SQL is required to use query parameters. Legacy SQL will not work. - QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query) - .addNamedParameter("wordCount", QueryParameterValue.int64(5)) - .build(); + // Print the results. for (FieldValueList row : bigquery.query(queryConfig).iterateAll()) { - // do something with the data + for (FieldValue val : row) { + System.out.printf("%s,", val.toString()); + } + System.out.printf("\n"); } - // [END runQueryWithParameters] + // [END bigquery_query] } } diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/CloudSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/CloudSnippets.java new file mode 100644 index 000000000000..3d19d7561813 --- /dev/null +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/CloudSnippets.java @@ -0,0 +1,286 @@ +/* + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.examples.bigquery.cloudsnippets; + +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.FieldValue; +import com.google.cloud.bigquery.FieldValueList; +import com.google.cloud.bigquery.QueryJobConfiguration; +import com.google.cloud.bigquery.QueryParameterValue; +import com.google.cloud.bigquery.TableId; +import java.util.concurrent.TimeoutException; +import org.joda.time.DateTime; +import org.joda.time.DateTimeZone; +import org.joda.time.format.DateTimeFormatter; +import org.joda.time.format.ISODateTimeFormat; + +/** + * This class contains snippets for cloud.google.com documentation. + */ +public class CloudSnippets { + + private final BigQuery bigquery; + + public CloudSnippets(BigQuery bigquery) { + this.bigquery = bigquery; + } + + /** + * Example of running a Legacy SQL query. + */ + public void runLegacySqlQuery() throws InterruptedException { + // [START bigquery_query_legacy] + // BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + 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).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_legacy] + } + + /** + * Example of running a query with Standard SQL explicitly set. + */ + public void runStandardSqlQuery() throws InterruptedException { + // [START bigquery_query_standard] + // BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + String query = "SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;"; + QueryJobConfiguration queryConfig = + // Note that setUseLegacySql is set to false by default + QueryJobConfiguration.newBuilder(query).setUseLegacySql(false).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_standard] + } + + /** + * Example of running a query and saving the results to a table. + */ + public void runQueryPermanentTable(String destinationDataset, String destinationTable) + throws InterruptedException { + // [START bigquery_query_destination_table] + // 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 = + // Note that setUseLegacySql is set to false by default + QueryJobConfiguration.newBuilder(query) + // 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. + */ + 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. + .setAllowLargeResults(true) + .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_legacy_large_results] + } + + /** + * Example of running a query with the cache disabled. + */ + public void runUncachedQuery() throws TimeoutException, InterruptedException { + // [START bigquery_query_no_cache] + // BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + String query = "SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;"; + QueryJobConfiguration queryConfig = + QueryJobConfiguration.newBuilder(query) + // Disable the query cache to force live query evaluation. + .setUseQueryCache(false) + .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_no_cache] + } + + /** + * Example of running a batch query. + */ + public void runBatchQuery() throws TimeoutException, InterruptedException { + // [START bigquery_query_batch] + // BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + String query = "SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;"; + QueryJobConfiguration queryConfig = + QueryJobConfiguration.newBuilder(query) + // Run at batch priority, which won't count toward concurrent rate + // limit. See: + // https://cloud.google.com/bigquery/docs/running-queries#batch + .setPriority(QueryJobConfiguration.Priority.BATCH) + .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_batch] + } + + /** + * Example of running a query with named query parameters. + */ + public void runQueryWithNamedParameters() throws InterruptedException { + // [START bigquery_query_params_named] + // BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + String corpus = "romeoandjuliet"; + long minWordCount = 250; + String query = + "SELECT word, word_count\n" + + "FROM `bigquery-public-data.samples.shakespeare`\n" + + "WHERE corpus = @corpus\n" + + "AND word_count >= @min_word_count\n" + + "ORDER BY word_count DESC"; + // Note: Standard SQL is required to use query parameters. + QueryJobConfiguration queryConfig = + QueryJobConfiguration.newBuilder(query) + .addNamedParameter("corpus", QueryParameterValue.string(corpus)) + .addNamedParameter("min_word_count", QueryParameterValue.int64(minWordCount)) + .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_params_named] + } + + /** + * Example of running a query with array query parameters. + */ + public void runQueryWithArrayParameters() throws InterruptedException { + // [START bigquery_query_params_arrays] + // BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + String gender = "M"; + String[] states = {"WA", "WI", "WV", "WY"}; + String query = + "SELECT name, sum(number) as count\n" + + "FROM `bigquery-public-data.usa_names.usa_1910_2013`\n" + + "WHERE gender = @gender\n" + + "AND state IN UNNEST(@states)\n" + + "GROUP BY name\n" + + "ORDER BY count DESC\n" + + "LIMIT 10;"; + // Note: Standard SQL is required to use query parameters. + QueryJobConfiguration queryConfig = + QueryJobConfiguration.newBuilder(query) + .addNamedParameter("gender", QueryParameterValue.string(gender)) + .addNamedParameter("states", QueryParameterValue.array(states, String.class)) + .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_params_arrays] + } + + /** + * Example of running a query with timestamp query parameters. + */ + public void runQueryWithTimestampParameters() throws InterruptedException { + // [START bigquery_query_params_timestamps] + // BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + DateTime timestamp = new DateTime(2016, 12, 7, 8, 0, 0, DateTimeZone.UTC); + String query = "SELECT TIMESTAMP_ADD(@ts_value, INTERVAL 1 HOUR);"; + // Note: Standard SQL is required to use query parameters. + QueryJobConfiguration queryConfig = + QueryJobConfiguration.newBuilder(query) + .addNamedParameter( + "ts_value", + QueryParameterValue.timestamp( + // Timestamp takes microseconds since 1970-01-01T00:00:00 UTC + timestamp.getMillis() * 1000)) + .build(); + + // Print the results. + DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis().withZoneUTC(); + for (FieldValueList row : bigquery.query(queryConfig).iterateAll()) { + System.out.printf( + "%s\n", + formatter.print( + new DateTime( + // Timestamp values are returned in microseconds since 1970-01-01T00:00:00 UTC, + // but org.joda.time.DateTime constructor accepts times in milliseconds. + row.get(0).getTimestampValue() / 1000, DateTimeZone.UTC))); + System.out.printf("\n"); + } + // [END bigquery_query_params_timestamps] + } +} diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/DatasetSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/DatasetSnippets.java index b797bb29dec6..01fdd5b0486b 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/DatasetSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/DatasetSnippets.java @@ -18,7 +18,7 @@ * EDITING INSTRUCTIONS * This file is referenced in Dataset’s javadoc. Any change to this file should be reflected in * Dataset’s javadoc. -*/ + */ package com.google.cloud.examples.bigquery.snippets; @@ -32,7 +32,6 @@ import com.google.cloud.bigquery.Table; import com.google.cloud.bigquery.TimePartitioning; - /** * This class contains a number of snippets for the {@link Dataset} interface. */ @@ -43,20 +42,20 @@ public class DatasetSnippets { public DatasetSnippets(Dataset dataset) { this.dataset = dataset; } - + /** * Example of checking whether a dataset exists. */ // [TARGET exists()] public boolean doesDatasetExist() { - // [START exists] + // [START ] boolean exists = dataset.exists(); if (exists) { // the dataset exists } else { // the dataset was not found } - // [END exists] + // [END ] return exists; } @@ -65,12 +64,12 @@ public boolean doesDatasetExist() { */ // [TARGET reload(DatasetOption...)] public Dataset reloadDataset() { - // [START reload] + // [START ] Dataset latestDataset = dataset.reload(); if (latestDataset == null) { // The dataset was not found } - // [END reload] + // [END ] return latestDataset; } @@ -80,11 +79,11 @@ public Dataset reloadDataset() { // [TARGET update(DatasetOption...)] // [VARIABLE "my_friendly_name"] public Dataset updateDataset(String friendlyName) { - // [START update] + // [START ] Builder builder = dataset.toBuilder(); builder.setFriendlyName(friendlyName); Dataset updatedDataset = builder.build().update(); - // [END update] + // [END ] return updatedDataset; } @@ -93,14 +92,14 @@ public Dataset updateDataset(String friendlyName) { */ // [TARGET delete(DatasetDeleteOption...)] public boolean deleteDataset() { - // [START delete] + // [START ] boolean deleted = dataset.delete(); if (deleted) { // The dataset was deleted } else { // The dataset was not found } - // [END delete] + // [END ] return deleted; } @@ -109,27 +108,27 @@ public boolean deleteDataset() { */ // [TARGET list(TableListOption...)] public Page
list() { - // [START list] + // [START ] Page
tables = dataset.list(); for (Table table : tables.iterateAll()) { // do something with the table } - // [END list] - return tables; + // [END ] + return tables; } - + /** * Example of getting a table in the dataset. */ // [TARGET get(String, TableOption...)] // [VARIABLE “my_table”] public Table getTable(String tableName) { - // [START getTable] + // [START ] Table table = dataset.get(tableName); - // [END getTable] + // [END ] return table; } - + /** * Example of creating a table in the dataset with schema and time partitioning. */ @@ -137,14 +136,15 @@ public Table getTable(String tableName) { // [VARIABLE “my_table”] // [VARIABLE “my_field”] public Table createTable(String tableName, String fieldName) { - // [START createTable] + // [START ] Schema schema = Schema.of(Field.of(fieldName, LegacySQLTypeName.STRING)); - StandardTableDefinition definition = StandardTableDefinition.newBuilder() - .setSchema(schema) - .setTimePartitioning(TimePartitioning.of(TimePartitioning.Type.DAY)) - .build(); + StandardTableDefinition definition = + StandardTableDefinition.newBuilder() + .setSchema(schema) + .setTimePartitioning(TimePartitioning.of(TimePartitioning.Type.DAY)) + .build(); Table table = dataset.create(tableName, definition); - // [END createTable] + // [END ] return table; } } diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/JobSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/JobSnippets.java index 5dfc984d7a5c..3b01e66e696a 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/JobSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/JobSnippets.java @@ -28,7 +28,6 @@ import com.google.cloud.bigquery.BigQueryException; import com.google.cloud.bigquery.Job; import com.google.cloud.bigquery.JobStatus; - import org.threeten.bp.Duration; public class JobSnippets { @@ -44,11 +43,11 @@ public JobSnippets(Job job) { */ // [TARGET exists()] public boolean exists() throws InterruptedException { - // [START exists] + // [START ] if (!job.exists()) { // job doesn't exist } - // [END exists] + // [END ] return job.exists(); } @@ -57,11 +56,11 @@ public boolean exists() throws InterruptedException { */ // [TARGET isDone()] public void isDone() throws InterruptedException { - // [START isDone] + // [START ] while (!job.isDone()) { Thread.sleep(1000L); } - // [END isDone] + // [END ] } /** @@ -70,7 +69,7 @@ public void isDone() throws InterruptedException { // [TARGET waitFor(RetryOption...)] public boolean waitFor() throws InterruptedException { try { - // [START waitFor] + // [START ] Job completedJob = job.waitFor(); if (completedJob == null) { // job no longer exists @@ -79,7 +78,7 @@ public boolean waitFor() throws InterruptedException { } else { // job completed successfully } - // [END waitFor] + // [END ] } catch (BigQueryException e) { // Timeouts shouldn't happen without a timeout option. if (e.getCause() instanceof PollException) { @@ -96,7 +95,7 @@ public boolean waitFor() throws InterruptedException { // [TARGET waitFor(RetryOption...)] public boolean waitForWithOptions() throws InterruptedException { try { - // [START waitForWithOptions] + // [START ] Job completedJob = job.waitFor( RetryOption.initialRetryDelay(Duration.ofSeconds(1)), @@ -108,7 +107,7 @@ public boolean waitForWithOptions() throws InterruptedException { } else { // job completed successfully } - // [END waitForWithOptions] + // [END ] } catch (BigQueryException e) { if (e.getCause() instanceof PollException) { return false; @@ -123,12 +122,12 @@ public boolean waitForWithOptions() throws InterruptedException { */ // [TARGET reload(JobOption...)] public JobStatus.State reload() throws InterruptedException { - // [START reload] + // [START ] while (job.getStatus().getState() != JobStatus.State.DONE) { Thread.sleep(1000L); job = job.reload(); } - // [END reload] + // [END ] return job.getStatus().getState(); } @@ -137,12 +136,12 @@ public JobStatus.State reload() throws InterruptedException { */ // [TARGET reload(JobOption...)] public JobStatus.State reloadStatus() throws InterruptedException { - // [START reloadStatus] + // [START ] while (job.getStatus().getState() != JobStatus.State.DONE) { Thread.sleep(1000L); job = job.reload(BigQuery.JobOption.fields(BigQuery.JobField.STATUS)); } - // [END reloadStatus] + // [END ] return job.getStatus().getState(); } @@ -151,13 +150,13 @@ public JobStatus.State reloadStatus() throws InterruptedException { */ // [TARGET cancel()] public boolean cancel() { - // [START cancel] + // [START ] if (job.cancel()) { return true; // job successfully cancelled } else { // job not found } - // [END cancel] + // [END ] return false; } } diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/TableSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/TableSnippets.java index cb6bf1699cb9..67732a15d741 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/TableSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/TableSnippets.java @@ -60,14 +60,14 @@ public TableSnippets(Table table) { */ // [TARGET exists()] public boolean exists() { - // [START exists] + // [START ] boolean exists = table.exists(); if (exists) { // the table exists } else { // the table was not found } - // [END exists] + // [END ] return exists; } @@ -79,12 +79,12 @@ public boolean exists() { // [VARIABLE TableField.LAST_MODIFIED_TIME] // [VARIABLE TableField.NUM_ROWS] public Table reloadTableWithFields(TableField field1, TableField field2) { - // [START reloadTableWithFields] + // [START ] Table latestTable = table.reload(TableOption.fields(field1, field2)); if (latestTable == null) { // the table was not found } - // [END reloadTableWithFields] + // [END ] return latestTable; } @@ -93,9 +93,9 @@ public Table reloadTableWithFields(TableField field1, TableField field2) { */ // [TARGET update(TableOption...)] public Table update() { - // [START update] + // [START ] Table updatedTable = table.toBuilder().setDescription("new description").build().update(); - // [END update] + // [END ] return updatedTable; } @@ -104,14 +104,14 @@ public Table update() { */ // [TARGET delete()] public boolean delete() { - // [START delete] + // [START ] boolean deleted = table.delete(); if (deleted) { // the table was deleted } else { // the table was not found } - // [END delete] + // [END ] return deleted; } @@ -122,7 +122,7 @@ public boolean delete() { // [VARIABLE "rowId1"] // [VARIABLE "rowId2"] public InsertAllResponse insert(String rowId1, String rowId2) { - // [START insert] + // [START ] List rows = new ArrayList<>(); Map row1 = new HashMap<>(); row1.put("stringField", "value1"); @@ -134,7 +134,7 @@ public InsertAllResponse insert(String rowId1, String rowId2) { rows.add(RowToInsert.of(rowId2, row2)); InsertAllResponse response = table.insert(rows); // do something with response - // [END insert] + // [END ] return response; } @@ -145,7 +145,7 @@ public InsertAllResponse insert(String rowId1, String rowId2) { // [VARIABLE "rowId1"] // [VARIABLE "rowId2"] public InsertAllResponse insertWithParams(String rowId1, String rowId2) { - // [START insertWithParams] + // [START ] List rows = new ArrayList<>(); Map row1 = new HashMap<>(); row1.put("stringField", 1); @@ -157,7 +157,7 @@ public InsertAllResponse insertWithParams(String rowId1, String rowId2) { rows.add(RowToInsert.of(rowId2, row2)); InsertAllResponse response = table.insert(rows, true, true); // do something with response - // [END insertWithParams] + // [END ] return response; } @@ -166,28 +166,30 @@ public InsertAllResponse insertWithParams(String rowId1, String rowId2) { */ // [TARGET list(TableDataListOption...)] public Page list() { - // [START list] + // [START ] // This example reads the result 100 rows per RPC call. If there's no need to limit the number, // simply omit the option. Page page = table.list(TableDataListOption.pageSize(100)); for (FieldValueList row : page.iterateAll()) { // do something with the row } - // [END list] + // [END ] return page; } - /** Example of listing rows in the table. */ + /** + * Example of listing rows in the table given a schema. + */ // [TARGET list(Schema, TableDataListOption...)] // [VARIABLE ...] // [VARIABLE "my_field"] public Page list(Schema schema, String field) { - // [START list] + // [START ] Page page = table.list(schema); for (FieldValueList row : page.iterateAll()) { row.get(field); } - // [END list] + // [END ] return page; } @@ -198,12 +200,14 @@ public Page list(Schema schema, String field) { // [VARIABLE "my_dataset"] // [VARIABLE "my_destination_table"] public Job copy(String datasetName, String tableName) { - // [START copy] + // [START ] Job job = table.copy(datasetName, tableName); // Wait for the job to complete. try { - Job completedJob = job.waitFor(RetryOption.initialRetryDelay(Duration.ofSeconds(1)), - RetryOption.totalTimeout(Duration.ofMinutes(3))); + Job completedJob = + job.waitFor( + RetryOption.initialRetryDelay(Duration.ofSeconds(1)), + RetryOption.totalTimeout(Duration.ofMinutes(3))); if (completedJob != null && completedJob.getStatus().getError() == null) { // Job completed successfully } else { @@ -212,7 +216,7 @@ public Job copy(String datasetName, String tableName) { } catch (InterruptedException e) { // Handle interrupted wait } - // [END copy] + // [END ] return job; } @@ -223,14 +227,16 @@ public Job copy(String datasetName, String tableName) { // [VARIABLE "my_dataset"] // [VARIABLE "my_destination_table"] public Job copyTableId(String dataset, String tableName) throws BigQueryException { - // [START copyTableId] + // [START bigquery_copy_table] TableId destinationId = TableId.of(dataset, tableName); JobOption options = JobOption.fields(JobField.STATUS, JobField.USER_EMAIL); Job job = table.copy(destinationId, options); // Wait for the job to complete. try { - Job completedJob = job.waitFor(RetryOption.initialRetryDelay(Duration.ofSeconds(1)), - RetryOption.totalTimeout(Duration.ofMinutes(3))); + Job completedJob = + job.waitFor( + RetryOption.initialRetryDelay(Duration.ofSeconds(1)), + RetryOption.totalTimeout(Duration.ofMinutes(3))); if (completedJob != null && completedJob.getStatus().getError() == null) { // Job completed successfully. } else { @@ -239,7 +245,7 @@ public Job copyTableId(String dataset, String tableName) throws BigQueryExceptio } catch (InterruptedException e) { // Handle interrupted wait } - // [END copyTableId] + // [END bigquery_copy_table] return job; } @@ -251,15 +257,17 @@ public Job copyTableId(String dataset, String tableName) throws BigQueryExceptio // [VARIABLE "gs://my_bucket/PartitionA_*.csv"] // [VARIABLE "gs://my_bucket/PartitionB_*.csv"] public Job extractList(String format, String gcsUrl1, String gcsUrl2) { - // [START extractList] + // [START ] List destinationUris = new ArrayList<>(); destinationUris.add(gcsUrl1); destinationUris.add(gcsUrl2); Job job = table.extract(format, destinationUris); // Wait for the job to complete try { - Job completedJob = job.waitFor(RetryOption.initialRetryDelay(Duration.ofSeconds(1)), - RetryOption.totalTimeout(Duration.ofMinutes(3))); + Job completedJob = + job.waitFor( + RetryOption.initialRetryDelay(Duration.ofSeconds(1)), + RetryOption.totalTimeout(Duration.ofMinutes(3))); if (completedJob != null && completedJob.getStatus().getError() == null) { // Job completed successfully } else { @@ -268,7 +276,7 @@ public Job extractList(String format, String gcsUrl1, String gcsUrl2) { } catch (InterruptedException e) { // Handle interrupted wait } - // [END extractList] + // [END ] return job; } @@ -279,12 +287,14 @@ public Job extractList(String format, String gcsUrl1, String gcsUrl2) { // [VARIABLE "CSV"] // [VARIABLE "gs://my_bucket/filename.csv"] public Job extractSingle(String format, String gcsUrl) { - // [START extractSingle] + // [START bigquery_extract_table] Job job = table.extract(format, gcsUrl); // Wait for the job to complete try { - Job completedJob = job.waitFor(RetryOption.initialRetryDelay(Duration.ofSeconds(1)), - RetryOption.totalTimeout(Duration.ofMinutes(3))); + Job completedJob = + job.waitFor( + RetryOption.initialRetryDelay(Duration.ofSeconds(1)), + RetryOption.totalTimeout(Duration.ofMinutes(3))); if (completedJob != null && completedJob.getStatus().getError() == null) { // Job completed successfully } else { @@ -293,7 +303,7 @@ public Job extractSingle(String format, String gcsUrl) { } catch (InterruptedException e) { // Handle interrupted wait } - // [END extractSingle] + // [END bigquery_extract_table] return job; } @@ -304,15 +314,17 @@ public Job extractSingle(String format, String gcsUrl) { // [VARIABLE "gs://my_bucket/filename1.csv"] // [VARIABLE "gs://my_bucket/filename2.csv"] public Job loadList(String gcsUrl1, String gcsUrl2) { - // [START loadList] + // [START ] List sourceUris = new ArrayList<>(); sourceUris.add(gcsUrl1); sourceUris.add(gcsUrl2); Job job = table.load(FormatOptions.csv(), sourceUris); // Wait for the job to complete try { - Job completedJob = job.waitFor(RetryOption.initialRetryDelay(Duration.ofSeconds(1)), - RetryOption.totalTimeout(Duration.ofMinutes(3))); + Job completedJob = + job.waitFor( + RetryOption.initialRetryDelay(Duration.ofSeconds(1)), + RetryOption.totalTimeout(Duration.ofMinutes(3))); if (completedJob != null && completedJob.getStatus().getError() == null) { // Job completed successfully } else { @@ -321,7 +333,7 @@ public Job loadList(String gcsUrl1, String gcsUrl2) { } catch (InterruptedException e) { // Handle interrupted wait } - // [END loadList] + // [END ] return job; } @@ -331,12 +343,14 @@ public Job loadList(String gcsUrl1, String gcsUrl2) { // [TARGET load(FormatOptions, String, JobOption...)] // [VARIABLE "gs://my_bucket/filename.csv"] public Job loadSingle(String sourceUri) { - // [START loadSingle] + // [START bigquery_load_table_gcs_csv] Job job = table.load(FormatOptions.csv(), sourceUri); // Wait for the job to complete try { - Job completedJob = job.waitFor(RetryOption.initialRetryDelay(Duration.ofSeconds(1)), - RetryOption.totalTimeout(Duration.ofMinutes(3))); + Job completedJob = + job.waitFor( + RetryOption.initialRetryDelay(Duration.ofSeconds(1)), + RetryOption.totalTimeout(Duration.ofMinutes(3))); if (completedJob != null && completedJob.getStatus().getError() == null) { // Job completed successfully } else { @@ -345,7 +359,7 @@ public Job loadSingle(String sourceUri) { } catch (InterruptedException e) { // Handle interrupted wait } - // [END loadSingle] + // [END bigquery_load_table_gcs_csv] return job; } } diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITBigQuerySnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITBigQuerySnippets.java index 2ef79ed95a83..9218a7b497f9 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITBigQuerySnippets.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITBigQuerySnippets.java @@ -40,17 +40,19 @@ import com.google.cloud.bigquery.Table; import com.google.cloud.bigquery.TableId; import com.google.cloud.bigquery.TableInfo; +import com.google.cloud.bigquery.TableResult; import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; import com.google.common.base.Function; import com.google.common.collect.Iterators; import com.google.common.collect.Sets; import com.google.common.io.Resources; +import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.PrintStream; import java.net.URISyntaxException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; -import java.util.Iterator; import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeoutException; @@ -66,14 +68,13 @@ public class ITBigQuerySnippets { private static final String OTHER_DATASET = RemoteBigQueryHelper.generateDatasetName(); private static final String QUERY = "SELECT unique(corpus) FROM [bigquery-public-data:samples.shakespeare]"; - private static final String QUERY_WITH_PARAMETERS = - "SELECT distinct(corpus) FROM `bigquery-public-data.samples.shakespeare` where word_count > @wordCount"; - private static final Function TO_JOB_ID_FUNCTION = new Function() { - @Override - public JobId apply(Job job) { - return job.getJobId(); - } - }; + private static final Function TO_JOB_ID_FUNCTION = + new Function() { + @Override + public JobId apply(Job job) { + return job.getJobId(); + } + }; private static final Function TO_TABLE_ID_FUNCTION = new Function() { @Override @@ -91,21 +92,26 @@ public DatasetId apply(Dataset dataset) { private static BigQuery bigquery; private static BigQuerySnippets bigquerySnippets; + private static ByteArrayOutputStream bout; + private static PrintStream out; - @Rule - public Timeout globalTimeout = Timeout.seconds(300); + @Rule public Timeout globalTimeout = Timeout.seconds(300); @BeforeClass public static void beforeClass() { bigquery = RemoteBigQueryHelper.create().getOptions().getService(); bigquerySnippets = new BigQuerySnippets(bigquery); bigquery.create(DatasetInfo.newBuilder(DATASET).build()); + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); } @AfterClass public static void afterClass() throws ExecutionException, InterruptedException { bigquery.delete(DATASET, DatasetDeleteOption.deleteContents()); bigquery.delete(OTHER_DATASET, DatasetDeleteOption.deleteContents()); + System.setOut(null); } @Test @@ -115,28 +121,45 @@ public void testCreateGetAndDeleteTable() throws InterruptedException { Table table = bigquerySnippets.createTable(DATASET, tableName, fieldName); assertNotNull(table); TableId tableId = TableId.of(bigquery.getOptions().getProjectId(), DATASET, tableName); - assertEquals(tableId, - bigquerySnippets.getTable(tableId.getDataset(), tableId.getTable()).getTableId()); - assertNotNull(bigquerySnippets.updateTable(DATASET, tableName, "new friendly name")); - assertEquals("new friendly name", bigquerySnippets.getTableFromId( - tableId.getProject(), tableId.getDataset(), tableId.getTable()).getFriendlyName()); - Set tables = Sets.newHashSet( - Iterators.transform(bigquerySnippets.listTables(DATASET).iterateAll().iterator(), - TO_TABLE_ID_FUNCTION)); + assertEquals( + tableId, bigquerySnippets.getTable(tableId.getDataset(), tableId.getTable()).getTableId()); + assertNotNull(bigquerySnippets.updateTable(DATASET, tableName, "new description")); + assertEquals( + "new description", + bigquerySnippets + .getTableFromId(tableId.getProject(), tableId.getDataset(), tableId.getTable()) + .getDescription()); + Set tables = + Sets.newHashSet( + Iterators.transform( + bigquerySnippets.listTables(DATASET).iterateAll().iterator(), + TO_TABLE_ID_FUNCTION)); while (!tables.contains(tableId)) { Thread.sleep(500); - tables = Sets.newHashSet( - Iterators.transform(bigquerySnippets.listTables(DATASET).iterateAll().iterator(), - TO_TABLE_ID_FUNCTION)); + tables = + Sets.newHashSet( + Iterators.transform( + bigquerySnippets.listTables(DATASET).iterateAll().iterator(), + TO_TABLE_ID_FUNCTION)); } - tables = Sets.newHashSet(Iterators.transform( - bigquerySnippets.listTablesFromId(tableId.getProject(), DATASET).iterateAll().iterator(), - TO_TABLE_ID_FUNCTION)); + tables = + Sets.newHashSet( + Iterators.transform( + bigquerySnippets + .listTablesFromId(tableId.getProject(), DATASET) + .iterateAll() + .iterator(), + TO_TABLE_ID_FUNCTION)); while (!tables.contains(tableId)) { Thread.sleep(500); - tables = Sets.newHashSet(Iterators.transform( - bigquerySnippets.listTablesFromId(tableId.getProject(), DATASET).iterateAll().iterator(), - TO_TABLE_ID_FUNCTION)); + tables = + Sets.newHashSet( + Iterators.transform( + bigquerySnippets + .listTablesFromId(tableId.getProject(), DATASET) + .iterateAll() + .iterator(), + TO_TABLE_ID_FUNCTION)); } assertTrue(bigquerySnippets.deleteTable(DATASET, tableName)); assertFalse(bigquerySnippets.deleteTableFromId(tableId.getProject(), DATASET, tableName)); @@ -148,26 +171,33 @@ public void testCreateGetAndDeleteDataset() throws InterruptedException { Dataset dataset = bigquerySnippets.createDataset(OTHER_DATASET); assertNotNull(dataset); assertEquals(datasetId, bigquerySnippets.getDataset(OTHER_DATASET).getDatasetId()); - assertNotNull(bigquerySnippets.updateDataset(OTHER_DATASET, "new friendly name")); - assertEquals("new friendly name", - bigquerySnippets.getDatasetFromId(datasetId.getProject(), OTHER_DATASET).getFriendlyName()); - Set datasets = Sets.newHashSet( - Iterators.transform(bigquerySnippets.listDatasets().iterateAll().iterator(), - TO_DATASET_ID_FUNCTION)); + assertNotNull(bigquerySnippets.updateDataset(OTHER_DATASET, "new description")); + assertEquals( + "new description", + bigquerySnippets.getDatasetFromId(datasetId.getProject(), OTHER_DATASET).getDescription()); + Set datasets = + Sets.newHashSet( + Iterators.transform( + bigquerySnippets.listDatasets().iterateAll().iterator(), TO_DATASET_ID_FUNCTION)); while (!datasets.contains(datasetId)) { Thread.sleep(500); - datasets = Sets.newHashSet( - Iterators.transform(bigquerySnippets.listDatasets().iterateAll().iterator(), - TO_DATASET_ID_FUNCTION)); + datasets = + Sets.newHashSet( + Iterators.transform( + bigquerySnippets.listDatasets().iterateAll().iterator(), TO_DATASET_ID_FUNCTION)); } - datasets = Sets.newHashSet( - Iterators.transform(bigquerySnippets.listDatasets(datasetId.getProject()).iterateAll().iterator(), - TO_DATASET_ID_FUNCTION)); + datasets = + Sets.newHashSet( + Iterators.transform( + bigquerySnippets.listDatasets(datasetId.getProject()).iterateAll().iterator(), + TO_DATASET_ID_FUNCTION)); while (!datasets.contains(datasetId)) { Thread.sleep(500); - datasets = Sets.newHashSet( - Iterators.transform(bigquerySnippets.listDatasets(datasetId.getProject()).iterateAll().iterator(), - TO_DATASET_ID_FUNCTION)); + datasets = + Sets.newHashSet( + Iterators.transform( + bigquerySnippets.listDatasets(datasetId.getProject()).iterateAll().iterator(), + TO_DATASET_ID_FUNCTION)); } assertTrue(bigquerySnippets.deleteDataset(OTHER_DATASET)); assertFalse(bigquerySnippets.deleteDatasetFromId(datasetId.getProject(), OTHER_DATASET)); @@ -187,15 +217,15 @@ public void testWriteAndListTableData() // Add rows from file Path csvPath = Paths.get(Resources.getResource("bigquery/test_write_and_list_table_data.csv").toURI()); - outputRows = bigquerySnippets.writeFileToTable(DATASET, tableName, csvPath); + outputRows = bigquerySnippets.writeFileToTable(DATASET, tableName, csvPath, "us"); assertEquals(2L, outputRows); // List all rows - Page listPage = bigquerySnippets.listTableData(DATASET, tableName); - Iterator rowIterator = listPage.getValues().iterator(); - assertEquals("StringValue1", rowIterator.next().get(0).getStringValue()); - assertEquals("StringValue2", rowIterator.next().get(0).getStringValue()); - assertEquals("StringValue3", rowIterator.next().get(0).getStringValue()); - assertEquals("StringValue4", rowIterator.next().get(0).getStringValue()); + TableResult tableData = bigquerySnippets.listTableData(DATASET, tableName); + String tableDataString = tableData.toString(); + assertTrue(tableDataString.contains("StringValue1")); + assertTrue(tableDataString.contains("StringValue2")); + assertTrue(tableDataString.contains("StringValue3")); + assertTrue(tableDataString.contains("StringValue4")); assertTrue(bigquerySnippets.deleteTable(DATASET, tableName)); } @@ -210,7 +240,7 @@ public void testWriteRemoteJsonToTable() throws InterruptedException { table = bigquery.getTable(datasetName, tableName); assertNotNull(table); ArrayList tableFieldNames = new ArrayList<>(); - for (Field field: table.getDefinition().getSchema().getFields()) { + for (Field field : table.getDefinition().getSchema().getFields()) { tableFieldNames.add(field.getName()); } bigquery.delete(table.getTableId()); @@ -229,7 +259,9 @@ public void testInsertAllAndListTableData() throws IOException, InterruptedExcep Schema.of( Field.of(fieldName1, LegacySQLTypeName.BOOLEAN), Field.of(fieldName2, LegacySQLTypeName.BYTES), - Field.of(fieldName3, LegacySQLTypeName.RECORD, + Field.of( + fieldName3, + LegacySQLTypeName.RECORD, Field.of(fieldName4, LegacySQLTypeName.STRING))); TableInfo table = TableInfo.of(tableId, StandardTableDefinition.of(schema)); assertNotNull(bigquery.create(table)); @@ -243,7 +275,7 @@ public void testInsertAllAndListTableData() throws IOException, InterruptedExcep } FieldValueList row = listPage.getValues().iterator().next(); assertEquals(true, row.get(0).getBooleanValue()); - assertArrayEquals(new byte[]{0xA, 0xD, 0xD, 0xE, 0xD}, row.get(1).getBytesValue()); + assertArrayEquals(new byte[] {0xA, 0xD, 0xD, 0xE, 0xD}, row.get(1).getBytesValue()); assertEquals("Hello, World!", row.get(2).getRecordValue().get(0).getStringValue()); listPage = bigquerySnippets.listTableDataSchema(DATASET, tableName, schema, fieldName1); @@ -263,14 +295,18 @@ public void testJob() throws ExecutionException, InterruptedException { assertNotNull(job1); assertNotNull(job2); assertEquals(job1.getJobId(), bigquerySnippets.getJob(job1.getJobId().getJob()).getJobId()); - assertEquals(job2.getJobId(), - bigquerySnippets.getJobFromId(job2.getJobId().getJob()).getJobId()); - Set jobs = Sets.newHashSet(Iterators.transform(bigquerySnippets.listJobs().iterateAll().iterator(), - TO_JOB_ID_FUNCTION)); + assertEquals( + job2.getJobId(), bigquerySnippets.getJobFromId(job2.getJobId().getJob()).getJobId()); + Set jobs = + Sets.newHashSet( + Iterators.transform( + bigquerySnippets.listJobs().iterateAll().iterator(), TO_JOB_ID_FUNCTION)); while (!jobs.contains(job1.getJobId()) || !jobs.contains(job2.getJobId())) { Thread.sleep(500); - jobs = Sets.newHashSet(Iterators.transform(bigquerySnippets.listJobs().iterateAll().iterator(), - TO_JOB_ID_FUNCTION)); + jobs = + Sets.newHashSet( + Iterators.transform( + bigquerySnippets.listJobs().iterateAll().iterator(), TO_JOB_ID_FUNCTION)); } assertTrue(bigquerySnippets.cancelJob(job1.getJobId().getJob())); assertTrue(bigquerySnippets.cancelJobFromId(job2.getJobId().getJob())); @@ -278,11 +314,8 @@ public void testJob() throws ExecutionException, InterruptedException { @Test public void testRunQuery() throws InterruptedException { - bigquerySnippets.runQuery(QUERY); - } - - @Test - public void testRunQueryWithParameters() throws InterruptedException { - bigquerySnippets.runQueryWithParameters(QUERY_WITH_PARAMETERS); + bigquerySnippets.runQuery(); + String got = bout.toString(); + assertTrue(got.contains("romeoandjuliet")); } } diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITCloudSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITCloudSnippets.java new file mode 100644 index 000000000000..28c79580c324 --- /dev/null +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITCloudSnippets.java @@ -0,0 +1,122 @@ +/* + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.examples.bigquery.cloudsnippets; + +import static org.junit.Assert.assertTrue; + +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption; +import com.google.cloud.bigquery.DatasetInfo; +import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ITCloudSnippets { + + private static final String DATASET = RemoteBigQueryHelper.generateDatasetName(); + + private static BigQuery bigquery; + private static CloudSnippets cloudSnippets; + private static ByteArrayOutputStream bout; + private static PrintStream out; + + @BeforeClass + public static void beforeClass() { + bigquery = RemoteBigQueryHelper.create().getOptions().getService(); + cloudSnippets = new CloudSnippets(bigquery); + bigquery.create(DatasetInfo.newBuilder(DATASET).build()); + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @AfterClass + public static void afterClass() throws ExecutionException, InterruptedException { + bigquery.delete(DATASET, DatasetDeleteOption.deleteContents()); + System.setOut(null); + } + + @Test + public void testRunLegacySqlQuery() throws InterruptedException { + cloudSnippets.runLegacySqlQuery(); + String got = bout.toString(); + assertTrue(got.contains("romeoandjuliet")); + } + + @Test + public void testRunStandardSqlQuery() throws InterruptedException { + cloudSnippets.runStandardSqlQuery(); + String got = bout.toString(); + assertTrue(got.contains("romeoandjuliet")); + } + + @Test + public void testRunQueryPermanentTable() throws InterruptedException { + String tableName = "test_destination_table"; + cloudSnippets.runQueryPermanentTable(DATASET, tableName); + String got = bout.toString(); + assertTrue(got.contains("romeoandjuliet")); + } + + @Test + public void testRunQueryLargeResults() throws InterruptedException { + String tableName = "test_large_results"; + cloudSnippets.runQueryLargeResults(DATASET, tableName); + String got = bout.toString(); + assertTrue(got.contains("romeoandjuliet")); + } + + @Test + public void testRunUncachedQuery() throws TimeoutException, InterruptedException { + cloudSnippets.runUncachedQuery(); + String got = bout.toString(); + assertTrue(got.contains("romeoandjuliet")); + } + + @Test + public void testRunBatchQuery() throws TimeoutException, InterruptedException { + cloudSnippets.runBatchQuery(); + String got = bout.toString(); + assertTrue(got.contains("romeoandjuliet")); + } + + @Test + public void testRunQueryWithNamedParameters() throws InterruptedException { + cloudSnippets.runQueryWithNamedParameters(); + String got = bout.toString(); + assertTrue(got.contains("love")); + } + + @Test + public void testRunQueryWithArrayParameters() throws InterruptedException { + cloudSnippets.runQueryWithArrayParameters(); + String got = bout.toString(); + assertTrue(got.contains("James")); + } + + @Test + public void testRunQueryWithTimestampParameters() throws InterruptedException { + cloudSnippets.runQueryWithTimestampParameters(); + String got = bout.toString(); + assertTrue(got.contains("2016-12-07T09:00:00Z")); + } +} diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITDatasetSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITDatasetSnippets.java index a74dde881c8f..2d8a5dd7542e 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITDatasetSnippets.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITDatasetSnippets.java @@ -32,14 +32,12 @@ import com.google.cloud.bigquery.StandardTableDefinition; import com.google.cloud.bigquery.Table; import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; - +import java.util.Iterator; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; -import java.util.Iterator; - public class ITDatasetSnippets { private static final String DATASET = RemoteBigQueryHelper.generateDatasetName(); @@ -98,14 +96,6 @@ public void testReload() { assertEquals(FRIENDLY_NAME, reloadedDataset.getFriendlyName()); } - @Test - public void testUpdate() { - assertNull(dataset.getFriendlyName()); - - Dataset updatedDataset = datasetSnippets.updateDataset(FRIENDLY_NAME); - assertEquals(FRIENDLY_NAME, updatedDataset.getFriendlyName()); - } - @Test public void testDeleteNonExistingDataset() { assertFalse(nonExistingDatasetSnippets.deleteDataset()); diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITJobSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITJobSnippets.java index 8864a3053695..56c8eb447408 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITJobSnippets.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITJobSnippets.java @@ -26,7 +26,6 @@ import com.google.cloud.bigquery.JobStatus; import com.google.cloud.bigquery.QueryJobConfiguration; import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; - import org.junit.BeforeClass; import org.junit.Test; diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITTableSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITTableSnippets.java index 9c00276231a4..2dcc16d0a3a1 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITTableSnippets.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITTableSnippets.java @@ -45,7 +45,8 @@ import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; - +import java.util.List; +import java.util.Set; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; @@ -54,21 +55,17 @@ import org.junit.Test; import org.junit.rules.Timeout; -import java.util.List; -import java.util.Set; - -/** - * Integration tests for {@link TableSnippets}. - */ +/** Integration tests for {@link TableSnippets}. */ public class ITTableSnippets { private static final String BASE_TABLE_NAME = "my_table"; private static final String DATASET_NAME = RemoteBigQueryHelper.generateDatasetName(); private static final String COPY_DATASET_NAME = RemoteBigQueryHelper.generateDatasetName(); private static final String BUCKET_NAME = RemoteStorageHelper.generateBucketName(); - private static final Schema SCHEMA = Schema.of( - Field.of("stringField", LegacySQLTypeName.STRING), - Field.of("booleanField", LegacySQLTypeName.BOOLEAN)); + private static final Schema SCHEMA = + Schema.of( + Field.of("stringField", LegacySQLTypeName.STRING), + Field.of("booleanField", LegacySQLTypeName.BOOLEAN)); private static final List ROW1 = ImmutableList.of("value1", true); private static final List ROW2 = ImmutableList.of("value2", false); private static final String DOOMED_TABLE_NAME = "doomed_table"; @@ -81,8 +78,7 @@ public class ITTableSnippets { private Table table; private TableSnippets tableSnippets; - @Rule - public Timeout globalTimeout = Timeout.seconds(300); + @Rule public Timeout globalTimeout = Timeout.seconds(300); @BeforeClass public static void beforeClass() { @@ -176,12 +172,16 @@ public void testInsertParams() throws InterruptedException { rows = ImmutableList.copyOf(tableSnippets.list().getValues()); } Set> values = - FluentIterable.from(rows).transform(new Function>() { - @Override - public List apply(FieldValueList row) { - return ImmutableList.of(row.get(0).getStringValue(), row.get(1).getBooleanValue()); - } - }).toSet(); + FluentIterable.from(rows) + .transform( + new Function>() { + @Override + public List apply(FieldValueList row) { + return ImmutableList.of( + row.get(0).getStringValue(), row.get(1).getBooleanValue()); + } + }) + .toSet(); assertEquals(ImmutableSet.of(ROW2), values); } @@ -241,12 +241,16 @@ private void verifyTestRows(Table checkTable) throws InterruptedException { List rows = waitForTableRows(checkTable, 2); // Verify that the table data matches what it's supposed to. Set> values = - FluentIterable.from(rows).transform(new Function>() { - @Override - public List apply(FieldValueList row) { - return ImmutableList.of(row.get(0).getStringValue(), row.get(1).getBooleanValue()); - } - }).toSet(); + FluentIterable.from(rows) + .transform( + new Function>() { + @Override + public List apply(FieldValueList row) { + return ImmutableList.of( + row.get(0).getStringValue(), row.get(1).getBooleanValue()); + } + }) + .toSet(); assertEquals(ImmutableSet.of(ROW2, ROW1), values); }