Skip to content

Commit 1eeffe6

Browse files
author
Praful Makani
authored
docs(samples): add query with arrays parameters (#603)
1 parent 8dc5bc3 commit 1eeffe6

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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_params_arrays]
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.QueryParameterValue;
25+
import com.google.cloud.bigquery.TableResult;
26+
27+
// Sample to running a query with array query parameters.
28+
public class QueryWithArrayParameters {
29+
30+
public static void runQueryWithArrayParameters() {
31+
String gender = "M";
32+
String[] states = {"WA", "WI", "WV", "WY"};
33+
String query =
34+
"SELECT name, sum(number) as count\n"
35+
+ "FROM `bigquery-public-data.usa_names.usa_1910_2013`\n"
36+
+ "WHERE gender = @gender\n"
37+
+ "AND state IN UNNEST(@states)\n"
38+
+ "GROUP BY name\n"
39+
+ "ORDER BY count DESC\n"
40+
+ "LIMIT 10;";
41+
queryWithArrayParameters(query, gender, states);
42+
}
43+
44+
public static void queryWithArrayParameters(String query, String gender, String[] states) {
45+
try {
46+
// Initialize client that will be used to send requests. This client only needs to be created
47+
// once, and can be reused for multiple requests.
48+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
49+
50+
// Note: Standard SQL is required to use query parameters.
51+
QueryJobConfiguration queryConfig =
52+
QueryJobConfiguration.newBuilder(query)
53+
.addNamedParameter("gender", QueryParameterValue.string(gender))
54+
.addNamedParameter("states", QueryParameterValue.array(states, String.class))
55+
.build();
56+
57+
TableResult results = bigquery.query(queryConfig);
58+
59+
// Print the results.
60+
results
61+
.iterateAll()
62+
.forEach(row -> row.forEach(val -> System.out.printf("%s,", val.toString())));
63+
System.out.println("Query with arrays parameters performed successfully");
64+
} catch (BigQueryException | InterruptedException e) {
65+
System.out.println("Query not performed \n" + e.toString());
66+
}
67+
}
68+
}
69+
// [END bigquery_query_params_arrays]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 QueryWithArrayParametersIT {
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 testQueryWithArrayParameters() {
46+
String gender = "M";
47+
String[] states = {"WA", "WI", "WV", "WY"};
48+
String query =
49+
"SELECT name, sum(number) as count\n"
50+
+ "FROM `bigquery-public-data.usa_names.usa_1910_2013`\n"
51+
+ "WHERE gender = @gender\n"
52+
+ "AND state IN UNNEST(@states)\n"
53+
+ "GROUP BY name\n"
54+
+ "ORDER BY count DESC\n"
55+
+ "LIMIT 10;";
56+
QueryWithArrayParameters.queryWithArrayParameters(query, gender, states);
57+
assertThat(bout.toString()).contains("Query with arrays parameters performed successfully");
58+
}
59+
}

0 commit comments

Comments
 (0)