From 4cc931db990206ca0053124d13c2bb7c4323aade Mon Sep 17 00:00:00 2001 From: Irvi Firqotul Aini Date: Thu, 28 May 2020 19:47:08 +0700 Subject: [PATCH] docs(samples): Convert tightly coupled local variable involved options inside of method into method arguments As of the current version we can see that more options related to argument are written inside of the test instead of included as arguments, so I think as this is tightly coupled with argument, it will be better to pass it as well. --- .../bigquery/CreatePartitionedTable.java | 15 +++++++-------- .../example/bigquery/ExtractTableToJson.java | 19 +++++++++++-------- .../com/example/bigquery/LoadLocalFile.java | 7 ++++--- .../bigquery/CreatePartitionedTableIT.java | 10 +++++++++- .../bigquery/ExtractTableToJsonIT.java | 6 ++++-- .../com/example/bigquery/LoadLocalFileIT.java | 3 ++- 6 files changed, 37 insertions(+), 23 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/bigquery/CreatePartitionedTable.java b/samples/snippets/src/main/java/com/example/bigquery/CreatePartitionedTable.java index 79a15a801..62a51c669 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/CreatePartitionedTable.java +++ b/samples/snippets/src/main/java/com/example/bigquery/CreatePartitionedTable.java @@ -34,10 +34,15 @@ public static void runCreatePartitionedTable() { // TODO(developer): Replace these variables before running the sample. String datasetName = "MY_DATASET_NAME"; String tableName = "MY_TABLE_NAME"; - createPartitionedTable(datasetName, tableName); + Schema schema = + Schema.of( + Field.of("stringField", StandardSQLTypeName.STRING), + Field.of("booleanField", StandardSQLTypeName.BOOL), + Field.of("dateField", StandardSQLTypeName.DATE)); + createPartitionedTable(datasetName, tableName, schema); } - public static void createPartitionedTable(String datasetName, String tableName) { + public static void createPartitionedTable(String datasetName, String tableName, Schema schema) { try { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. @@ -47,12 +52,6 @@ public static void createPartitionedTable(String datasetName, String tableName) TimePartitioning partitioning = TimePartitioning.of(TimePartitioning.Type.DAY); - Schema schema = - Schema.of( - Field.of("stringField", StandardSQLTypeName.STRING), - Field.of("booleanField", StandardSQLTypeName.BOOL), - Field.of("dateField", StandardSQLTypeName.DATE)); - StandardTableDefinition tableDefinition = StandardTableDefinition.newBuilder() .setSchema(schema) diff --git a/samples/snippets/src/main/java/com/example/bigquery/ExtractTableToJson.java b/samples/snippets/src/main/java/com/example/bigquery/ExtractTableToJson.java index 4d657c690..0f05cb20d 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/ExtractTableToJson.java +++ b/samples/snippets/src/main/java/com/example/bigquery/ExtractTableToJson.java @@ -35,12 +35,19 @@ public static void runExtractTableToJson() { String tableName = "shakespeare"; String bucketName = "my-bucket"; String destinationUri = "gs://" + bucketName + "/path/to/file"; - extractTableToJson(projectId, datasetName, tableName, destinationUri); + // For more information on export formats available see: + // https://cloud.google.com/bigquery/docs/exporting-data#export_formats_and_compression_types + // For more information on Job see: + // https://googleapis.dev/java/google-cloud-clients/latest/index.html?com/google/cloud/bigquery/package-summary.html + + String dataFormat = "CSV"; + extractTableToJson(projectId, datasetName, tableName, destinationUri, dataFormat); } // Exports datasetName:tableName to destinationUri as raw CSV public static void extractTableToJson( - String projectId, String datasetName, String tableName, String destinationUri) { + String projectId, String datasetName, String tableName, String destinationUri, + String dataFormat) { try { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. @@ -49,11 +56,7 @@ public static void extractTableToJson( TableId tableId = TableId.of(projectId, datasetName, tableName); Table table = bigquery.getTable(tableId); - // For more information on export formats available see: - // https://cloud.google.com/bigquery/docs/exporting-data#export_formats_and_compression_types - // For more information on Job see: - // https://googleapis.dev/java/google-cloud-clients/latest/index.html?com/google/cloud/bigquery/package-summary.html - Job job = table.extract("CSV", destinationUri); + Job job = table.extract(dataFormat, destinationUri); // Blocks until this job completes its execution, either failing or succeeding. Job completedJob = @@ -68,7 +71,7 @@ public static void extractTableToJson( "BigQuery was unable to extract due to an error: \n" + job.getStatus().getError()); return; } - System.out.println("Table export successful. Check in GCS bucket for the CSV file."); + System.out.println("Table export successful. Check in GCS bucket for the " + dataFormat + " file."); } catch (BigQueryException | InterruptedException e) { System.out.println("Table extraction job was interrupted. \n" + e.toString()); } diff --git a/samples/snippets/src/main/java/com/example/bigquery/LoadLocalFile.java b/samples/snippets/src/main/java/com/example/bigquery/LoadLocalFile.java index e6e2e3915..3e580ec7d 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/LoadLocalFile.java +++ b/samples/snippets/src/main/java/com/example/bigquery/LoadLocalFile.java @@ -41,10 +41,11 @@ public static void runLoadLocalFile() throws IOException, InterruptedException { String datasetName = "MY_DATASET_NAME"; String tableName = "MY_TABLE_NAME"; Path csvPath = FileSystems.getDefault().getPath(".", "my-data.csv"); - loadLocalFile(datasetName, tableName, csvPath); + loadLocalFile(datasetName, tableName, csvPath, FormatOptions.csv()); } - public static void loadLocalFile(String datasetName, String tableName, Path csvPath) + public static void loadLocalFile(String datasetName, String tableName, Path csvPath, + FormatOptions formatOptions) throws IOException, InterruptedException { try { // Initialize client that will be used to send requests. This client only needs to be created @@ -54,7 +55,7 @@ public static void loadLocalFile(String datasetName, String tableName, Path csvP WriteChannelConfiguration writeChannelConfiguration = WriteChannelConfiguration.newBuilder(tableId) - .setFormatOptions(FormatOptions.csv()) + .setFormatOptions(formatOptions) .build(); // The location and JobName must be specified; other fields can be auto-detected. diff --git a/samples/snippets/src/test/java/com/example/bigquery/CreatePartitionedTableIT.java b/samples/snippets/src/test/java/com/example/bigquery/CreatePartitionedTableIT.java index cbe612b32..32000c5ce 100644 --- a/samples/snippets/src/test/java/com/example/bigquery/CreatePartitionedTableIT.java +++ b/samples/snippets/src/test/java/com/example/bigquery/CreatePartitionedTableIT.java @@ -19,6 +19,9 @@ import static com.google.common.truth.Truth.assertThat; import static junit.framework.TestCase.assertNotNull; +import com.google.cloud.bigquery.Field; +import com.google.cloud.bigquery.Schema; +import com.google.cloud.bigquery.StandardSQLTypeName; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import org.junit.After; @@ -58,8 +61,13 @@ public void tearDown() { @Test public void testCreatePartitionedTable() { String tableName = "MY_PARTITIONED_TABLE"; + Schema schema = + Schema.of( + Field.of("stringField", StandardSQLTypeName.STRING), + Field.of("booleanField", StandardSQLTypeName.BOOL), + Field.of("dateField", StandardSQLTypeName.DATE)); - CreatePartitionedTable.createPartitionedTable(BIGQUERY_DATASET_NAME, tableName); + CreatePartitionedTable.createPartitionedTable(BIGQUERY_DATASET_NAME, tableName, schema); assertThat(bout.toString()).contains("Partitioned table created successfully"); diff --git a/samples/snippets/src/test/java/com/example/bigquery/ExtractTableToJsonIT.java b/samples/snippets/src/test/java/com/example/bigquery/ExtractTableToJsonIT.java index e018384ed..fd28bc1cb 100644 --- a/samples/snippets/src/test/java/com/example/bigquery/ExtractTableToJsonIT.java +++ b/samples/snippets/src/test/java/com/example/bigquery/ExtractTableToJsonIT.java @@ -61,10 +61,12 @@ public void testExtractTableToJson() { String datasetName = "samples"; String tableName = "shakespeare"; String destinationUri = "gs://" + GCS_BUCKET + "/extractTest.csv"; + String dataFormat = "CSV"; // Extract table content to GCS in CSV format - ExtractTableToJson.extractTableToJson(projectId, datasetName, tableName, destinationUri); + ExtractTableToJson.extractTableToJson(projectId, datasetName, tableName, destinationUri, + dataFormat); assertThat(bout.toString()) - .contains("Table export successful. Check in GCS bucket for the CSV file."); + .contains("Table export successful. Check in GCS bucket for the " + dataFormat + " file."); } } diff --git a/samples/snippets/src/test/java/com/example/bigquery/LoadLocalFileIT.java b/samples/snippets/src/test/java/com/example/bigquery/LoadLocalFileIT.java index a1a8d5a6f..aefa2adba 100644 --- a/samples/snippets/src/test/java/com/example/bigquery/LoadLocalFileIT.java +++ b/samples/snippets/src/test/java/com/example/bigquery/LoadLocalFileIT.java @@ -20,6 +20,7 @@ import static junit.framework.TestCase.assertNotNull; import com.google.cloud.bigquery.Field; +import com.google.cloud.bigquery.FormatOptions; import com.google.cloud.bigquery.LegacySQLTypeName; import com.google.cloud.bigquery.Schema; import java.io.ByteArrayOutputStream; @@ -76,7 +77,7 @@ public void loadLocalFile() throws IOException, InterruptedException { Path csvPath = FileSystems.getDefault().getPath("src/test/resources", "bigquery_noheader.csv"); - LoadLocalFile.loadLocalFile(BIGQUERY_DATASET_NAME, tableName, csvPath); + LoadLocalFile.loadLocalFile(BIGQUERY_DATASET_NAME, tableName, csvPath, FormatOptions.csv()); assertThat(bout.toString()).contains("Successfully loaded");