Skip to content

Commit 082f1a2

Browse files
author
Praful Makani
authored
docs(samples): add query with name types parameters (#633)
1 parent 108e462 commit 082f1a2

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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_named_types]
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.StandardSQLTypeName;
26+
import com.google.cloud.bigquery.TableResult;
27+
28+
// Sample to run query with named types parameters.
29+
public class QueryWithNamedTypesParameters {
30+
31+
public static void runQueryWithNamedTypesParameters() {
32+
String[] words = {"and", "is", "the", "moon"};
33+
String corpus = "romeoandjuliet";
34+
Integer wordsCount = 250;
35+
String query =
36+
"SELECT word, word_count"
37+
+ " FROM `bigquery-public-data.samples.shakespeare`"
38+
+ " WHERE word IN UNNEST(@wordList)"
39+
+ " AND corpus = @corpus"
40+
+ " AND word_count >= @minWordCount"
41+
+ " ORDER BY word_count DESC";
42+
queryWithNamedTypesParameters(query, words, corpus, wordsCount);
43+
}
44+
45+
public static void queryWithNamedTypesParameters(
46+
String query, String[] words, String corpus, Integer wordsCount) {
47+
try {
48+
// Initialize client that will be used to send requests. This client only needs to be created
49+
// once, and can be reused for multiple requests.
50+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
51+
52+
QueryParameterValue wordList = QueryParameterValue.array(words, StandardSQLTypeName.STRING);
53+
QueryParameterValue corpusParam = QueryParameterValue.of(corpus, StandardSQLTypeName.STRING);
54+
QueryParameterValue minWordCount =
55+
QueryParameterValue.of(wordsCount, StandardSQLTypeName.INT64);
56+
57+
// Note: Standard SQL is required to use query parameters.
58+
QueryJobConfiguration queryConfig =
59+
QueryJobConfiguration.newBuilder(query)
60+
.addNamedParameter("wordList", wordList)
61+
.addNamedParameter("corpus", corpusParam)
62+
.addNamedParameter("minWordCount", minWordCount)
63+
.build();
64+
65+
TableResult results = bigquery.query(queryConfig);
66+
67+
results
68+
.iterateAll()
69+
.forEach(row -> row.forEach(val -> System.out.printf("%s,", val.toString())));
70+
71+
System.out.println("Query with named types parameters performed successfully.");
72+
} catch (BigQueryException | InterruptedException e) {
73+
System.out.println("Query not performed \n" + e.toString());
74+
}
75+
}
76+
}
77+
// [END bigquery_query_params_named_types]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 QueryWithNamedTypesParametersIT {
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 testQueryWithNamedTypesParameters() {
46+
String[] words = {"and", "is", "the", "moon"};
47+
String corpus = "romeoandjuliet";
48+
Integer wordsCount = 250;
49+
String query =
50+
"SELECT word, word_count"
51+
+ " FROM `bigquery-public-data.samples.shakespeare`"
52+
+ " WHERE word IN UNNEST(@wordList)"
53+
+ " AND corpus = @corpus"
54+
+ " AND word_count >= @minWordCount"
55+
+ " ORDER BY word_count DESC";
56+
QueryWithNamedTypesParameters.queryWithNamedTypesParameters(query, words, corpus, wordsCount);
57+
assertThat(bout.toString())
58+
.contains("Query with named types parameters performed successfully");
59+
}
60+
}

0 commit comments

Comments
 (0)