Skip to content

Commit 2476419

Browse files
feat: new sample - Tables: Copy multiple tables (#76)
* feat: new sample - Tables: Copy multiple tables b/147141782 * update test to create new destination table for each test since existing table cannot be overwritten and would throw bigquery error * add env vars for sample test builds * update testing var names * format code
1 parent fd2dcf8 commit 2476419

File tree

4 files changed

+152
-1
lines changed

4 files changed

+152
-1
lines changed

.kokoro/run_samples_tests.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ mvn -v
3030
# Setup required env variables
3131
export GOOGLE_CLOUD_PROJECT=java-docs-samples-testing
3232
export GOOGLE_APPLICATION_CREDENTIALS=${KOKORO_GFILE_DIR}/service-acct.json
33+
export BIGQUERY_DATASET_NAME=bigquery_test_dataset
3334
export GCS_BUCKET=java-docs-samples-testing
35+
export BIGQUERY_TABLE1=table1
36+
export BIGQUERY_TABLE2=table2
3437

3538
# Activate service account
3639
gcloud auth activate-service-account \
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
// [START bigquery_copy_table_multiple_source]
20+
import com.google.cloud.bigquery.BigQuery;
21+
import com.google.cloud.bigquery.BigQueryException;
22+
import com.google.cloud.bigquery.BigQueryOptions;
23+
import com.google.cloud.bigquery.CopyJobConfiguration;
24+
import com.google.cloud.bigquery.Job;
25+
import com.google.cloud.bigquery.JobInfo;
26+
import com.google.cloud.bigquery.TableId;
27+
import java.util.Arrays;
28+
29+
public class CopyMultipleTables {
30+
31+
public static void runCopyMultipleTables() {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String destinationDatasetName = "MY_DATASET_NAME";
34+
String destinationTableId = "MY_TABLE_NAME";
35+
copyMultipleTables(destinationDatasetName, destinationTableId);
36+
}
37+
38+
public static void copyMultipleTables(String destinationDatasetName, String destinationTableId) {
39+
try {
40+
// Initialize client that will be used to send requests. This client only needs to be created
41+
// once, and can be reused for multiple requests.
42+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
43+
44+
TableId destinationTable = TableId.of(destinationDatasetName, destinationTableId);
45+
46+
// For more information on CopyJobConfiguration see:
47+
// https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/bigquery/JobConfiguration.html
48+
CopyJobConfiguration configuration =
49+
CopyJobConfiguration.newBuilder(
50+
destinationTable,
51+
Arrays.asList(
52+
TableId.of(destinationDatasetName, "table1"),
53+
TableId.of(destinationDatasetName, "table2")))
54+
.build();
55+
56+
// For more information on Job see:
57+
// https://googleapis.dev/java/google-cloud-clients/latest/index.html?com/google/cloud/bigquery/package-summary.html
58+
Job job = bigquery.create(JobInfo.of(configuration));
59+
60+
// Blocks until this job completes its execution, either failing or succeeding.
61+
Job completedJob = job.waitFor();
62+
if (completedJob == null) {
63+
System.out.println("Job not executed since it no longer exists.");
64+
return;
65+
} else if (completedJob.getStatus().getError() != null) {
66+
System.out.println(
67+
"BigQuery was unable to copy tables due to an error: \n" + job.getStatus().getError());
68+
return;
69+
}
70+
System.out.println("Table copied successfully.");
71+
} catch (BigQueryException | InterruptedException e) {
72+
System.out.println("Table copying job was interrupted. \n" + e.toString());
73+
}
74+
}
75+
}
76+
// [END bigquery_copy_table_multiple_source]
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.PrintStream;
24+
import java.util.UUID;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
30+
public class CopyMultipleTablesIT {
31+
private ByteArrayOutputStream bout;
32+
private PrintStream out;
33+
34+
private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME");
35+
private static final String BIGQUERY_TABLE1 = System.getenv("BIGQUERY_TABLE1");
36+
private static final String BIGQUERY_TABLE2 = System.getenv("BIGQUERY_TABLE2");
37+
38+
private static void requireEnvVar(String varName) {
39+
assertNotNull(
40+
"Environment variable " + varName + " is required to perform these tests.",
41+
System.getenv(varName));
42+
}
43+
44+
@BeforeClass
45+
public static void checkRequirements() {
46+
requireEnvVar("BIGQUERY_DATASET_NAME");
47+
requireEnvVar("BIGQUERY_TABLE1");
48+
requireEnvVar("BIGQUERY_TABLE2");
49+
}
50+
51+
@Before
52+
public void setUp() throws Exception {
53+
bout = new ByteArrayOutputStream();
54+
out = new PrintStream(bout);
55+
System.setOut(out);
56+
}
57+
58+
@After
59+
public void tearDown() {
60+
System.setOut(null);
61+
}
62+
63+
@Test
64+
public void testCopyMultipleTables() {
65+
// Create a new destination table for each test since existing table cannot be overwritten
66+
String generatedTableName =
67+
"gcloud_test_table_temp_" + UUID.randomUUID().toString().replace('-', '_');
68+
CreateTable.createTable(BIGQUERY_DATASET_NAME, generatedTableName, null);
69+
70+
CopyMultipleTables.copyMultipleTables(BIGQUERY_DATASET_NAME, generatedTableName);
71+
assertThat(bout.toString()).contains("Table copied successfully.");
72+
}
73+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ public void testExtractTableToJson() {
6161
String datasetName = "samples";
6262
String tableName = "shakespeare";
6363
String destinationUri = "gs://" + GCS_BUCKET + "/extractTest.csv";
64-
System.out.println(destinationUri);
6564

6665
// Extract table content to GCS in CSV format
6766
ExtractTableToJson.extractTableToJson(projectId, datasetName, tableName, destinationUri);

0 commit comments

Comments
 (0)