Skip to content

Commit ca07302

Browse files
author
Praful Makani
authored
docs(samples): add query script (#605)
1 parent 0abdd90 commit ca07302

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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_script]
20+
import com.google.api.gax.paging.Page;
21+
import com.google.cloud.bigquery.BigQuery;
22+
import com.google.cloud.bigquery.BigQueryException;
23+
import com.google.cloud.bigquery.BigQueryOptions;
24+
import com.google.cloud.bigquery.Job;
25+
import com.google.cloud.bigquery.JobInfo;
26+
import com.google.cloud.bigquery.QueryJobConfiguration;
27+
28+
// Sample to run query script.
29+
public class QueryScript {
30+
31+
public static void runQueryScript() {
32+
String script =
33+
"-- Declare a variable to hold names as an array.\n"
34+
+ "DECLARE top_names ARRAY<STRING>;\n"
35+
+ "-- Build an array of the top 100 names from the year 2017.\n"
36+
+ "SET top_names = (\n"
37+
+ " SELECT ARRAY_AGG(name ORDER BY number DESC LIMIT 100)\n"
38+
+ " FROM `bigquery-public-data`.usa_names.usa_1910_current\n"
39+
+ " WHERE year = 2017\n"
40+
+ ");\n"
41+
+ "-- Which names appear as words in Shakespeare's plays?\n"
42+
+ "SELECT\n"
43+
+ " name AS shakespeare_name\n"
44+
+ "FROM UNNEST(top_names) AS name\n"
45+
+ "WHERE name IN (\n"
46+
+ " SELECT word\n"
47+
+ " FROM `bigquery-public-data`.samples.shakespeare\n"
48+
+ ");";
49+
queryScript(script);
50+
}
51+
52+
public static void queryScript(String script) {
53+
try {
54+
// Initialize client that will be used to send requests. This client only needs to be created
55+
// once, and can be reused for multiple requests.
56+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
57+
58+
QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(script).build();
59+
Job createJob = bigquery.create(JobInfo.of(queryConfig));
60+
// Wait for the whole script to finish.
61+
JobInfo jobInfo = createJob.waitFor();
62+
String parentJobId = jobInfo.getJobId().getJob();
63+
64+
// Fetch jobs created by the SQL script.
65+
Page<Job> childJobs = bigquery.listJobs(BigQuery.JobListOption.parentJobId(parentJobId));
66+
childJobs
67+
.iterateAll()
68+
.forEach(job -> System.out.printf("Child Job Id: ", job.getJobId().getJob()));
69+
70+
System.out.println("Query script performed successfully.");
71+
} catch (BigQueryException | InterruptedException e) {
72+
System.out.println("Query not performed \n" + e.toString());
73+
}
74+
}
75+
}
76+
// [END bigquery_query_script]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.PrintStream;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
27+
public class QueryScriptIT {
28+
29+
private ByteArrayOutputStream bout;
30+
private PrintStream out;
31+
32+
@Before
33+
public void setUp() {
34+
bout = new ByteArrayOutputStream();
35+
out = new PrintStream(bout);
36+
System.setOut(out);
37+
}
38+
39+
@After
40+
public void tearDown() {
41+
System.setOut(null);
42+
}
43+
44+
@Test
45+
public void testQueryScript() {
46+
String script =
47+
"-- Declare a variable to hold names as an array.\n"
48+
+ "DECLARE top_names ARRAY<STRING>;\n"
49+
+ "-- Build an array of the top 100 names from the year 2017.\n"
50+
+ "SET top_names = (\n"
51+
+ " SELECT ARRAY_AGG(name ORDER BY number DESC LIMIT 100)\n"
52+
+ " FROM `bigquery-public-data`.usa_names.usa_1910_current\n"
53+
+ " WHERE year = 2017\n"
54+
+ ");\n"
55+
+ "-- Which names appear as words in Shakespeare's plays?\n"
56+
+ "SELECT\n"
57+
+ " name AS shakespeare_name\n"
58+
+ "FROM UNNEST(top_names) AS name\n"
59+
+ "WHERE name IN (\n"
60+
+ " SELECT word\n"
61+
+ " FROM `bigquery-public-data`.samples.shakespeare\n"
62+
+ ");";
63+
64+
QueryScript.queryScript(script);
65+
assertThat(bout.toString()).contains("Query script performed successfully.");
66+
}
67+
}

0 commit comments

Comments
 (0)