Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0f50961

Browse files
authoredJun 4, 2020
docs(samples): adding destination query sample and test (#418)
@stephaniewang526
1 parent 59426df commit 0f50961

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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_query_destination_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.QueryJobConfiguration;
24+
import com.google.cloud.bigquery.TableId;
25+
26+
public class SaveQueryToTable {
27+
28+
public static void runSaveQueryToTable() {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String query =
31+
"SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;";
32+
String destinationTable = "MY_TABLE";
33+
String destinationDataset = "MY_DATASET";
34+
35+
saveQueryToTable(destinationDataset, destinationTable, query);
36+
}
37+
38+
public static void saveQueryToTable(String destinationDataset,
39+
String destinationTableId, String query) {
40+
try {
41+
// Initialize client that will be used to send requests. This client only needs to be created
42+
// once, and can be reused for multiple requests.
43+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
44+
45+
// Identify the destination table
46+
TableId destinationTable = TableId.of(destinationDataset, destinationTableId);
47+
48+
// Build the query job
49+
QueryJobConfiguration queryConfig =
50+
QueryJobConfiguration.newBuilder(query).setDestinationTable(destinationTable).build();
51+
52+
// Execute the query.
53+
bigquery.query(queryConfig);
54+
55+
// The results are now saved in the destination table.
56+
57+
System.out.println("Saved query ran successfully");
58+
} catch (BigQueryException | InterruptedException e) {
59+
System.out.println("Saved query did not run \n" + e.toString());
60+
}
61+
}
62+
}
63+
// [END bigquery_query_destination_table]
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 SaveQueryToTableIT {
31+
private ByteArrayOutputStream bout;
32+
private PrintStream out;
33+
34+
35+
private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME");
36+
37+
private static void requireEnvVar(String varName) {
38+
assertNotNull(
39+
"Environment variable " + varName + " is required to perform these tests.",
40+
System.getenv(varName));
41+
}
42+
43+
@BeforeClass
44+
public static void checkRequirements() {
45+
requireEnvVar("BIGQUERY_DATASET_NAME");
46+
}
47+
48+
@Before
49+
public void setUp() {
50+
bout = new ByteArrayOutputStream();
51+
out = new PrintStream(bout);
52+
System.setOut(out);
53+
}
54+
55+
@After
56+
public void tearDown() {
57+
System.setOut(null);
58+
}
59+
60+
@Test
61+
public void testSaveQueryToTable() {
62+
String tableName = "MY_TABLE_NAME_" + UUID.randomUUID().toString().replace("-", "_");
63+
String query =
64+
"SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;";
65+
66+
SaveQueryToTable.saveQueryToTable(BIGQUERY_DATASET_NAME, tableName, query);
67+
68+
assertThat(bout.toString()).contains("Saved query ran successfully");
69+
70+
// Clean up
71+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
72+
}
73+
}

0 commit comments

Comments
 (0)
Failed to load comments.