Skip to content
This repository was archived by the owner on Mar 23, 2026. It is now read-only.

Commit 208883c

Browse files
author
Praful Makani
authored
docs(samples): add query with positional type parameters (#634)
1 parent 082f1a2 commit 208883c

2 files changed

Lines changed: 138 additions & 0 deletions

File tree

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_positional_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 positional types parameters.
29+
public class QueryWithPositionalTypesParameters {
30+
31+
public static void runQueryWithPositionalTypesParameters() {
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(?)"
39+
+ " AND corpus = ?"
40+
+ " AND word_count >= ?"
41+
+ " ORDER BY word_count DESC";
42+
queryWithPositionalTypesParameters(query, words, corpus, wordsCount);
43+
}
44+
45+
public static void queryWithPositionalTypesParameters(
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+
.addPositionalParameter(wordList)
61+
.addPositionalParameter(corpusParam)
62+
.addPositionalParameter(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 positional 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_positional_types]
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 QueryWithPositionalTypesParametersIT {
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 testQueryWithPositionalTypesParameters() {
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(?)"
53+
+ " AND corpus = ?"
54+
+ " AND word_count >= ?"
55+
+ " ORDER BY word_count DESC";
56+
QueryWithPositionalTypesParameters.queryWithPositionalTypesParameters(
57+
query, words, corpus, wordsCount);
58+
assertThat(bout.toString())
59+
.contains("Query with positional types parameters performed successfully");
60+
}
61+
}

0 commit comments

Comments
 (0)