Skip to content

Commit a5df219

Browse files
feat: sample - query with positional params (#117)
1 parent ecec2f7 commit a5df219

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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]
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+
public class QueryWithPositionalParameters {
28+
public static void queryWithPositionalParameters() {
29+
try {
30+
// Initialize client that will be used to send requests. This client only needs to be created
31+
// once, and can be reused for multiple requests.
32+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
33+
34+
String corpus = "romeoandjuliet";
35+
long minWordCount = 250;
36+
String query =
37+
"SELECT word, word_count\n"
38+
+ "FROM `bigquery-public-data.samples.shakespeare`\n"
39+
+ "WHERE corpus = ?\n"
40+
+ "AND word_count >= ?\n"
41+
+ "ORDER BY word_count DESC";
42+
43+
// Note: Standard SQL is required to use query parameters.
44+
QueryJobConfiguration queryConfig =
45+
QueryJobConfiguration.newBuilder(query)
46+
.addPositionalParameter(QueryParameterValue.string(corpus))
47+
.addPositionalParameter(QueryParameterValue.int64(minWordCount))
48+
.build();
49+
50+
TableResult results = bigquery.query(queryConfig);
51+
52+
results
53+
.iterateAll()
54+
.forEach(row -> row.forEach(val -> System.out.printf("%s,", val.toString())));
55+
56+
System.out.println("Query with positional parameters performed successfully.");
57+
} catch (BigQueryException | InterruptedException e) {
58+
System.out.println("Query not performed \n" + e.toString());
59+
}
60+
}
61+
}
62+
// [END bigquery_query_params_positional]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 QueryWithPositionalParametersIT {
28+
private ByteArrayOutputStream bout;
29+
private PrintStream out;
30+
31+
@Before
32+
public void setUp() {
33+
bout = new ByteArrayOutputStream();
34+
out = new PrintStream(bout);
35+
System.setOut(out);
36+
}
37+
38+
@After
39+
public void tearDown() {
40+
System.setOut(null);
41+
}
42+
43+
@Test
44+
public void testQueryWithPositionalParameters() {
45+
QueryWithPositionalParameters.queryWithPositionalParameters();
46+
assertThat(bout.toString())
47+
.contains("Query with positional parameters performed successfully.");
48+
}
49+
}

0 commit comments

Comments
 (0)