Skip to content

Commit de0d97f

Browse files
authored
docs(samples): added copy table and accompanying test (#414)
1 parent c1f21e6 commit de0d97f

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
// [START bigquery_copy_table]
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+
28+
public class CopyTable {
29+
30+
public static void runCopyTable() {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String destinationDatasetName = "MY_DESTINATION_DATASET_NAME";
33+
String destinationTableId = "MY_DESTINATION_TABLE_NAME";
34+
String sourceDatasetName = "MY_SOURCE_DATASET_NAME";
35+
String sourceTableId = "MY_SOURCE_TABLE_NAME";
36+
37+
copyTable(sourceDatasetName, sourceTableId, destinationDatasetName, destinationTableId);
38+
}
39+
40+
public static void copyTable(String sourceDatasetName, String sourceTableId,
41+
String destinationDatasetName, String destinationTableId) {
42+
try {
43+
// Initialize client that will be used to send requests. This client only needs to be created
44+
// once, and can be reused for multiple requests.
45+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
46+
47+
TableId sourceTable = TableId.of(sourceDatasetName, sourceTableId);
48+
TableId destinationTable = TableId.of(destinationDatasetName, destinationTableId);
49+
50+
// For more information on CopyJobConfiguration see:
51+
// https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/bigquery/JobConfiguration.html
52+
CopyJobConfiguration configuration =
53+
CopyJobConfiguration.newBuilder(
54+
destinationTable,
55+
sourceTable
56+
).build();
57+
58+
// For more information on Job see:
59+
// https://googleapis.dev/java/google-cloud-clients/latest/index.html?com/google/cloud/bigquery/package-summary.html
60+
Job job = bigquery.create(JobInfo.of(configuration));
61+
62+
// Blocks until this job completes its execution, either failing or succeeding.
63+
Job completedJob = job.waitFor();
64+
if (completedJob == null) {
65+
System.out.println("Job not executed since it no longer exists.");
66+
return;
67+
} else if (completedJob.getStatus().getError() != null) {
68+
System.out.println(
69+
"BigQuery was unable to copy table due to an error: \n" + job.getStatus().getError());
70+
return;
71+
}
72+
System.out.println("Table copied successfully.");
73+
} catch (BigQueryException | InterruptedException e) {
74+
System.out.println("Table copying job was interrupted. \n" + e.toString());
75+
}
76+
}
77+
}
78+
// [END bigquery_copy_table]
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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 com.google.cloud.bigquery.Field;
23+
import com.google.cloud.bigquery.Schema;
24+
import com.google.cloud.bigquery.StandardSQLTypeName;
25+
import java.io.ByteArrayOutputStream;
26+
import java.io.PrintStream;
27+
import java.util.UUID;
28+
import org.junit.After;
29+
import org.junit.Before;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
33+
public class CopyTableIT {
34+
private ByteArrayOutputStream bout;
35+
private PrintStream out;
36+
37+
private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME");
38+
39+
private static void requireEnvVar(String varName) {
40+
assertNotNull(
41+
"Environment variable " + varName + " is required to perform these tests.",
42+
System.getenv(varName));
43+
}
44+
45+
@BeforeClass
46+
public static void checkRequirements() {
47+
requireEnvVar("BIGQUERY_DATASET_NAME");
48+
}
49+
50+
@Before
51+
public void setUp() throws Exception {
52+
bout = new ByteArrayOutputStream();
53+
out = new PrintStream(bout);
54+
System.setOut(out);
55+
}
56+
57+
@After
58+
public void tearDown() {
59+
System.setOut(null);
60+
}
61+
62+
@Test
63+
public void testCopyTable() {
64+
// Create a new destination and source table for each test since existing table cannot be overwritten
65+
String generatedDestTableName = "gcloud_test_table_temp_" +
66+
UUID.randomUUID().toString().replace('-', '_');
67+
String generatedSourceTableName = "gcloud_test_table_temp_" +
68+
UUID.randomUUID().toString().replace('-', '_');
69+
70+
//Adding an arbitrary table schema so we aren't copying nothing.
71+
Schema schema =
72+
Schema.of(
73+
Field.of("stringField", StandardSQLTypeName.STRING),
74+
Field.of("booleanField", StandardSQLTypeName.BOOL));
75+
76+
CreateTable.createTable(BIGQUERY_DATASET_NAME, generatedDestTableName, schema);
77+
CreateTable.createTable(BIGQUERY_DATASET_NAME, generatedSourceTableName, schema);
78+
79+
CopyTable.copyTable(BIGQUERY_DATASET_NAME, generatedSourceTableName,
80+
BIGQUERY_DATASET_NAME, generatedDestTableName);
81+
assertThat(bout.toString()).contains("Table copied successfully.");
82+
83+
// Clean up
84+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, generatedDestTableName);
85+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, generatedSourceTableName);
86+
}
87+
}

0 commit comments

Comments
 (0)