Skip to content

Commit a11f3cb

Browse files
authored
docs(samples): Convert tightly coupled local variable involved options inside of method into method arguments (#393)
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. Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [x] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/java-bigquery/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [x] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [x] Appropriate docs were updated (if necessary) Fixes #394 ☕️
1 parent 3f13ccb commit a11f3cb

File tree

6 files changed

+37
-23
lines changed

6 files changed

+37
-23
lines changed

‎samples/snippets/src/main/java/com/example/bigquery/CreatePartitionedTable.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,15 @@ public static void runCreatePartitionedTable() {
3434
// TODO(developer): Replace these variables before running the sample.
3535
String datasetName = "MY_DATASET_NAME";
3636
String tableName = "MY_TABLE_NAME";
37-
createPartitionedTable(datasetName, tableName);
37+
Schema schema =
38+
Schema.of(
39+
Field.of("stringField", StandardSQLTypeName.STRING),
40+
Field.of("booleanField", StandardSQLTypeName.BOOL),
41+
Field.of("dateField", StandardSQLTypeName.DATE));
42+
createPartitionedTable(datasetName, tableName, schema);
3843
}
3944

40-
public static void createPartitionedTable(String datasetName, String tableName) {
45+
public static void createPartitionedTable(String datasetName, String tableName, Schema schema) {
4146
try {
4247
// Initialize client that will be used to send requests. This client only needs to be created
4348
// once, and can be reused for multiple requests.
@@ -47,12 +52,6 @@ public static void createPartitionedTable(String datasetName, String tableName)
4752

4853
TimePartitioning partitioning = TimePartitioning.of(TimePartitioning.Type.DAY);
4954

50-
Schema schema =
51-
Schema.of(
52-
Field.of("stringField", StandardSQLTypeName.STRING),
53-
Field.of("booleanField", StandardSQLTypeName.BOOL),
54-
Field.of("dateField", StandardSQLTypeName.DATE));
55-
5655
StandardTableDefinition tableDefinition =
5756
StandardTableDefinition.newBuilder()
5857
.setSchema(schema)

‎samples/snippets/src/main/java/com/example/bigquery/ExtractTableToJson.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,19 @@ public static void runExtractTableToJson() {
3535
String tableName = "shakespeare";
3636
String bucketName = "my-bucket";
3737
String destinationUri = "gs://" + bucketName + "/path/to/file";
38-
extractTableToJson(projectId, datasetName, tableName, destinationUri);
38+
// For more information on export formats available see:
39+
// https://cloud.google.com/bigquery/docs/exporting-data#export_formats_and_compression_types
40+
// For more information on Job see:
41+
// https://googleapis.dev/java/google-cloud-clients/latest/index.html?com/google/cloud/bigquery/package-summary.html
42+
43+
String dataFormat = "CSV";
44+
extractTableToJson(projectId, datasetName, tableName, destinationUri, dataFormat);
3945
}
4046

4147
// Exports datasetName:tableName to destinationUri as raw CSV
4248
public static void extractTableToJson(
43-
String projectId, String datasetName, String tableName, String destinationUri) {
49+
String projectId, String datasetName, String tableName, String destinationUri,
50+
String dataFormat) {
4451
try {
4552
// Initialize client that will be used to send requests. This client only needs to be created
4653
// once, and can be reused for multiple requests.
@@ -49,11 +56,7 @@ public static void extractTableToJson(
4956
TableId tableId = TableId.of(projectId, datasetName, tableName);
5057
Table table = bigquery.getTable(tableId);
5158

52-
// For more information on export formats available see:
53-
// https://cloud.google.com/bigquery/docs/exporting-data#export_formats_and_compression_types
54-
// For more information on Job see:
55-
// https://googleapis.dev/java/google-cloud-clients/latest/index.html?com/google/cloud/bigquery/package-summary.html
56-
Job job = table.extract("CSV", destinationUri);
59+
Job job = table.extract(dataFormat, destinationUri);
5760

5861
// Blocks until this job completes its execution, either failing or succeeding.
5962
Job completedJob =
@@ -68,7 +71,7 @@ public static void extractTableToJson(
6871
"BigQuery was unable to extract due to an error: \n" + job.getStatus().getError());
6972
return;
7073
}
71-
System.out.println("Table export successful. Check in GCS bucket for the CSV file.");
74+
System.out.println("Table export successful. Check in GCS bucket for the " + dataFormat + " file.");
7275
} catch (BigQueryException | InterruptedException e) {
7376
System.out.println("Table extraction job was interrupted. \n" + e.toString());
7477
}

‎samples/snippets/src/main/java/com/example/bigquery/LoadLocalFile.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ public static void runLoadLocalFile() throws IOException, InterruptedException {
4141
String datasetName = "MY_DATASET_NAME";
4242
String tableName = "MY_TABLE_NAME";
4343
Path csvPath = FileSystems.getDefault().getPath(".", "my-data.csv");
44-
loadLocalFile(datasetName, tableName, csvPath);
44+
loadLocalFile(datasetName, tableName, csvPath, FormatOptions.csv());
4545
}
4646

47-
public static void loadLocalFile(String datasetName, String tableName, Path csvPath)
47+
public static void loadLocalFile(String datasetName, String tableName, Path csvPath,
48+
FormatOptions formatOptions)
4849
throws IOException, InterruptedException {
4950
try {
5051
// 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
5455

5556
WriteChannelConfiguration writeChannelConfiguration =
5657
WriteChannelConfiguration.newBuilder(tableId)
57-
.setFormatOptions(FormatOptions.csv())
58+
.setFormatOptions(formatOptions)
5859
.build();
5960

6061
// The location and JobName must be specified; other fields can be auto-detected.

‎samples/snippets/src/test/java/com/example/bigquery/CreatePartitionedTableIT.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020
import static junit.framework.TestCase.assertNotNull;
2121

22+
import com.google.cloud.bigquery.Field;
23+
import com.google.cloud.bigquery.Schema;
24+
import com.google.cloud.bigquery.StandardSQLTypeName;
2225
import java.io.ByteArrayOutputStream;
2326
import java.io.PrintStream;
2427
import org.junit.After;
@@ -58,8 +61,13 @@ public void tearDown() {
5861
@Test
5962
public void testCreatePartitionedTable() {
6063
String tableName = "MY_PARTITIONED_TABLE";
64+
Schema schema =
65+
Schema.of(
66+
Field.of("stringField", StandardSQLTypeName.STRING),
67+
Field.of("booleanField", StandardSQLTypeName.BOOL),
68+
Field.of("dateField", StandardSQLTypeName.DATE));
6169

62-
CreatePartitionedTable.createPartitionedTable(BIGQUERY_DATASET_NAME, tableName);
70+
CreatePartitionedTable.createPartitionedTable(BIGQUERY_DATASET_NAME, tableName, schema);
6371

6472
assertThat(bout.toString()).contains("Partitioned table created successfully");
6573

‎samples/snippets/src/test/java/com/example/bigquery/ExtractTableToJsonIT.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@ public void testExtractTableToJson() {
6161
String datasetName = "samples";
6262
String tableName = "shakespeare";
6363
String destinationUri = "gs://" + GCS_BUCKET + "/extractTest.csv";
64+
String dataFormat = "CSV";
6465

6566
// Extract table content to GCS in CSV format
66-
ExtractTableToJson.extractTableToJson(projectId, datasetName, tableName, destinationUri);
67+
ExtractTableToJson.extractTableToJson(projectId, datasetName, tableName, destinationUri,
68+
dataFormat);
6769
assertThat(bout.toString())
68-
.contains("Table export successful. Check in GCS bucket for the CSV file.");
70+
.contains("Table export successful. Check in GCS bucket for the " + dataFormat + " file.");
6971
}
7072
}

‎samples/snippets/src/test/java/com/example/bigquery/LoadLocalFileIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static junit.framework.TestCase.assertNotNull;
2121

2222
import com.google.cloud.bigquery.Field;
23+
import com.google.cloud.bigquery.FormatOptions;
2324
import com.google.cloud.bigquery.LegacySQLTypeName;
2425
import com.google.cloud.bigquery.Schema;
2526
import java.io.ByteArrayOutputStream;
@@ -76,7 +77,7 @@ public void loadLocalFile() throws IOException, InterruptedException {
7677

7778
Path csvPath = FileSystems.getDefault().getPath("src/test/resources", "bigquery_noheader.csv");
7879

79-
LoadLocalFile.loadLocalFile(BIGQUERY_DATASET_NAME, tableName, csvPath);
80+
LoadLocalFile.loadLocalFile(BIGQUERY_DATASET_NAME, tableName, csvPath, FormatOptions.csv());
8081

8182
assertThat(bout.toString()).contains("Successfully loaded");
8283

0 commit comments

Comments
 (0)