Skip to content

Commit e12122c

Browse files
docs(samples): fix CopyMultipleTables sample IT failure and improve a few other samples (#1817)
* docs(samples): fix CopyMultipleTables sample IT failure and improve a few other samples Fixes issue: #1805 * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent d48ae41 commit e12122c

File tree

4 files changed

+45
-14
lines changed

4 files changed

+45
-14
lines changed

samples/snippets/src/main/java/com/example/bigquery/CopyMultipleTables.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,30 @@ public static void main(String[] args) {
3232
// TODO(developer): Replace these variables before running the sample.
3333
String destinationDatasetName = "MY_DATASET_NAME";
3434
String destinationTableId = "MY_TABLE_NAME";
35-
copyMultipleTables(destinationDatasetName, destinationTableId);
35+
String sourceTable1Id = "MY_SOURCE_TABLE_1";
36+
String sourceTable2Id = "MY_SOURCE_TABLE_2";
37+
copyMultipleTables(destinationDatasetName, destinationTableId, sourceTable1Id, sourceTable2Id);
3638
}
3739

38-
public static void copyMultipleTables(String destinationDatasetName, String destinationTableId) {
40+
public static void copyMultipleTables(
41+
String destinationDatasetName,
42+
String destinationTableId,
43+
String sourceTable1Id,
44+
String sourceTable2Id) {
3945
try {
4046
// Initialize client that will be used to send requests. This client only needs to be created
4147
// once, and can be reused for multiple requests.
4248
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
4349

4450
TableId destinationTable = TableId.of(destinationDatasetName, destinationTableId);
51+
TableId sourceTable1 = TableId.of(destinationDatasetName, sourceTable1Id);
52+
TableId sourceTable2 = TableId.of(destinationDatasetName, sourceTable2Id);
4553

4654
// For more information on CopyJobConfiguration see:
4755
// https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/bigquery/JobConfiguration.html
4856
CopyJobConfiguration configuration =
4957
CopyJobConfiguration.newBuilder(
50-
destinationTable,
51-
Arrays.asList(
52-
TableId.of(destinationDatasetName, "table1"),
53-
TableId.of(destinationDatasetName, "table2")))
58+
destinationTable, Arrays.asList(sourceTable1, sourceTable2))
5459
.build();
5560

5661
// For more information on Job see:

samples/snippets/src/main/java/com/example/bigquery/QueryExternalBigtablePerm.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package com.example.bigquery;
1818

1919
// [START bigquery_query_external_bigtable_perm]
20+
21+
import com.google.api.client.util.Base64;
2022
import com.google.cloud.bigquery.BigQuery;
2123
import com.google.cloud.bigquery.BigQueryException;
2224
import com.google.cloud.bigquery.BigQueryOptions;
@@ -29,7 +31,6 @@
2931
import com.google.cloud.bigquery.TableInfo;
3032
import com.google.cloud.bigquery.TableResult;
3133
import com.google.common.collect.ImmutableList;
32-
import org.apache.commons.codec.binary.Base64;
3334

3435
// Sample to queries an external bigtable data source using a permanent table
3536
public class QueryExternalBigtablePerm {

samples/snippets/src/main/java/com/example/bigquery/QueryExternalBigtableTemp.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package com.example.bigquery;
1818

1919
// [START bigquery_query_external_bigtable_temp]
20+
21+
import com.google.api.client.util.Base64;
2022
import com.google.cloud.bigquery.BigQuery;
2123
import com.google.cloud.bigquery.BigQueryException;
2224
import com.google.cloud.bigquery.BigQueryOptions;
@@ -27,7 +29,6 @@
2729
import com.google.cloud.bigquery.QueryJobConfiguration;
2830
import com.google.cloud.bigquery.TableResult;
2931
import com.google.common.collect.ImmutableList;
30-
import org.apache.commons.codec.binary.Base64;
3132

3233
// Sample to queries an external bigtable data source using a temporary table
3334
public class QueryExternalBigtableTemp {

samples/snippets/src/test/java/com/example/bigquery/CopyMultipleTablesIT.java

+30-6
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 java.util.UUID;
@@ -32,22 +35,27 @@
3235
public class CopyMultipleTablesIT {
3336

3437
private final Logger log = Logger.getLogger(this.getClass().getName());
38+
private String datasetName;
3539
private String tableName;
40+
private String sourceTable1Name;
41+
private String sourceTable2Name;
3642
private ByteArrayOutputStream bout;
3743
private PrintStream out;
3844
private PrintStream originalPrintStream;
3945

40-
private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME");
46+
private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT");
4147

42-
private static void requireEnvVar(String varName) {
48+
private static String requireEnvVar(String varName) {
49+
String value = System.getenv(varName);
4350
assertNotNull(
4451
"Environment variable " + varName + " is required to perform these tests.",
4552
System.getenv(varName));
53+
return value;
4654
}
4755

4856
@BeforeClass
4957
public static void checkRequirements() {
50-
requireEnvVar("BIGQUERY_DATASET_NAME");
58+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
5159
}
5260

5361
@Before
@@ -56,15 +64,30 @@ public void setUp() throws Exception {
5664
out = new PrintStream(bout);
5765
originalPrintStream = System.out;
5866
System.setOut(out);
67+
5968
// Create a new destination table for each test since existing table cannot be overwritten
69+
datasetName = "MY_DATASET_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8);
6070
tableName = "COPY_MULTIPLE_TABLE_TEST" + UUID.randomUUID().toString().substring(0, 8);
61-
CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, null);
71+
sourceTable1Name =
72+
"COPY_MULTIPLE_TABLE_SOURCE1_TEST" + UUID.randomUUID().toString().substring(0, 8);
73+
sourceTable2Name =
74+
"COPY_MULTIPLE_TABLE_SOURCE2_TEST" + UUID.randomUUID().toString().substring(0, 8);
75+
CreateDataset.createDataset(datasetName);
76+
77+
Schema schema =
78+
Schema.of(
79+
Field.of("timestampField", StandardSQLTypeName.TIMESTAMP),
80+
Field.of("stringField", StandardSQLTypeName.STRING),
81+
Field.of("booleanField", StandardSQLTypeName.BOOL));
82+
CreateTable.createTable(datasetName, tableName, schema);
83+
CreateTable.createTable(datasetName, sourceTable1Name, schema);
84+
CreateTable.createTable(datasetName, sourceTable2Name, schema);
6285
}
6386

6487
@After
6588
public void tearDown() {
6689
// Clean up
67-
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
90+
DeleteDataset.deleteDataset(PROJECT_ID, datasetName);
6891
// restores print statements in the original method
6992
System.out.flush();
7093
System.setOut(originalPrintStream);
@@ -73,7 +96,8 @@ public void tearDown() {
7396

7497
@Test
7598
public void testCopyMultipleTables() {
76-
CopyMultipleTables.copyMultipleTables(BIGQUERY_DATASET_NAME, tableName);
99+
CopyMultipleTables.copyMultipleTables(
100+
datasetName, tableName, sourceTable1Name, sourceTable2Name);
77101
assertThat(bout.toString()).contains("Table copied successfully.");
78102
}
79103
}

0 commit comments

Comments
 (0)