Skip to content

Commit 36c7406

Browse files
committed
Merge pull request #452 from mziccard/bigquery
Add query request and query response classes and tests
2 parents 7dfca8e + c6e53c8 commit 36c7406

File tree

7 files changed

+969
-1
lines changed

7 files changed

+969
-1
lines changed
Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
/*
2+
* Copyright 2015 Google Inc. All Rights Reserved.
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.google.gcloud.bigquery;
18+
19+
import static com.google.common.base.Preconditions.checkNotNull;
20+
21+
import com.google.common.base.MoreObjects;
22+
23+
import java.io.Serializable;
24+
import java.util.Objects;
25+
26+
/**
27+
* Google Cloud BigQuery Query Request. This class can be used to run a BigQuery SQL query and
28+
* return results if the query completes within a specified timeout. The query results are saved to
29+
* a temporary table that is deleted approximately 24 hours after the query is run. The query is run
30+
* through a BigQuery Job whose identity can be accessed via {@link QueryResponse#jobId()}.
31+
*
32+
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs/query">Query</a>
33+
* @see <a href="https://cloud.google.com/bigquery/query-reference">Query Reference</a>
34+
*/
35+
public class QueryRequest implements Serializable {
36+
37+
private static final long serialVersionUID = -8727328332415880852L;
38+
39+
private final String query;
40+
private final Long maxResults;
41+
private final DatasetId defaultDataset;
42+
private final Long maxWaitTime;
43+
private final Boolean dryRun;
44+
private final Boolean useQueryCache;
45+
46+
public static final class Builder {
47+
48+
private String query;
49+
private Long maxResults;
50+
private DatasetId defaultDataset;
51+
private Long maxWaitTime;
52+
private Boolean dryRun;
53+
private Boolean useQueryCache;
54+
55+
private Builder() {}
56+
57+
/**
58+
* Sets the BigQuery query to be executed.
59+
*/
60+
public Builder query(String query) {
61+
this.query = checkNotNull(query);
62+
return this;
63+
}
64+
65+
/**
66+
* Sets the maximum number of rows of data to return per page of results. Setting this flag to a
67+
* small value such as 1000 and then paging through results might improve reliability when the
68+
* query result set is large. In addition to this limit, responses are also limited to 10 MB.
69+
* By default, there is no maximum row count, and only the byte limit applies.
70+
*/
71+
public Builder maxResults(Long maxResults) {
72+
this.maxResults = maxResults;
73+
return this;
74+
}
75+
76+
/**
77+
* Sets the default dataset to assume for any unqualified table names in the query.
78+
*/
79+
public Builder defaultDataset(DatasetId defaultDataset) {
80+
this.defaultDataset = defaultDataset;
81+
return this;
82+
}
83+
84+
/**
85+
* Sets how long to wait for the query to complete, in milliseconds, before the request times
86+
* out and returns. Note that this is only a timeout for the request, not the query. If the
87+
* query takes longer to run than the timeout value, the call returns without any results and
88+
* with the {@link QueryResponse#jobComplete()} set to {@code false}. If not set, a wait time of
89+
* 10000 milliseconds (10 seconds) is used.
90+
*/
91+
public Builder maxWaitTime(Long maxWaitTime) {
92+
this.maxWaitTime = maxWaitTime;
93+
return this;
94+
}
95+
96+
/**
97+
* Sets whether the query has to be dry run or not. If set, the query is not executed. If the
98+
* query is valid statistics are returned on how many bytes would be processed. If the query is
99+
* invalid an error is returned. If not set the query is executed.
100+
*/
101+
public Builder dryRun(Boolean dryRun) {
102+
this.dryRun = dryRun;
103+
return this;
104+
}
105+
106+
/**
107+
* Sets whether to look for the result in the query cache. The query cache is a best-effort
108+
* cache that will be flushed whenever tables in the query are modified. If not specified the
109+
* query cache is used.
110+
*
111+
* @see <a href="https://cloud.google.com/bigquery/querying-data#querycaching">Query Caching</a>
112+
*/
113+
public Builder useQueryCache(Boolean useQueryCache) {
114+
this.useQueryCache = useQueryCache;
115+
return this;
116+
}
117+
118+
public QueryRequest build() {
119+
return new QueryRequest(this);
120+
}
121+
}
122+
123+
private QueryRequest(Builder builder) {
124+
query = builder.query;
125+
maxResults = builder.maxResults;
126+
defaultDataset = builder.defaultDataset;
127+
maxWaitTime = builder.maxWaitTime;
128+
dryRun = builder.dryRun;
129+
useQueryCache = builder.useQueryCache;
130+
}
131+
132+
/**
133+
* Sets the BigQuery query to be executed.
134+
*/
135+
public String query() {
136+
return query;
137+
}
138+
139+
/**
140+
* Returns the maximum number of rows of data to return per page of results.
141+
*/
142+
public Long maxResults() {
143+
return maxResults;
144+
}
145+
146+
/**
147+
* Returns the default dataset to assume for any unqualified table names in the query.
148+
*/
149+
public DatasetId defaultDataset() {
150+
return defaultDataset;
151+
}
152+
153+
/**
154+
* Returns how long to wait for the query to complete, in milliseconds, before the request times
155+
* out and returns. Note that this is only a timeout for the request, not the query. If the
156+
* query takes longer to run than the timeout value, the call returns without any results and
157+
* with the {@link QueryResponse#jobComplete()} set to {@code false}. You can call
158+
* {@link BigQuery#getQueryResults(JobId, BigQuery.QueryResultsOption...)} to wait for the query
159+
* to complete and read the results. If not set, a wait time of 10000 milliseconds (10 seconds)
160+
* is used.
161+
*/
162+
public Long maxWaitTime() {
163+
return maxWaitTime;
164+
}
165+
166+
/**
167+
* Returns whether the query has to be dry run or not. If set, the query is not executed. If the
168+
* query is valid statistics are returned on how many bytes would be processed. If the query is
169+
* invalid an error is returned. If not set the query is executed.
170+
*/
171+
public Boolean dryRun() {
172+
return dryRun;
173+
}
174+
175+
/**
176+
* Returns whether to look for the result in the query cache. The query cache is a best-effort
177+
* cache that will be flushed whenever tables in the query are modified. If not specified the
178+
* query cache is used.
179+
*
180+
* @see <a href="https://cloud.google.com/bigquery/querying-data#querycaching">Query Caching</a>
181+
*/
182+
public Boolean useQueryCache() {
183+
return useQueryCache;
184+
}
185+
186+
/**
187+
* Returns a builder for the {@code QueryRequest} object.
188+
*/
189+
public Builder toBuilder() {
190+
return new Builder()
191+
.query(query)
192+
.maxResults(maxResults)
193+
.defaultDataset(defaultDataset)
194+
.maxWaitTime(maxWaitTime)
195+
.dryRun(dryRun)
196+
.useQueryCache(useQueryCache);
197+
}
198+
199+
@Override
200+
public String toString() {
201+
return MoreObjects.toStringHelper(this)
202+
.add("query", query)
203+
.add("maxResults", maxResults)
204+
.add("defaultDataset", defaultDataset)
205+
.add("maxWaitTime", maxWaitTime)
206+
.add("dryRun", dryRun)
207+
.add("useQueryCache", useQueryCache)
208+
.toString();
209+
}
210+
211+
@Override
212+
public int hashCode() {
213+
return Objects.hash(query, maxResults, defaultDataset, maxWaitTime, dryRun, useQueryCache);
214+
}
215+
216+
@Override
217+
public boolean equals(Object obj) {
218+
return obj instanceof QueryRequest && Objects.equals(toPb(), ((QueryRequest) obj).toPb());
219+
}
220+
221+
com.google.api.services.bigquery.model.QueryRequest toPb() {
222+
com.google.api.services.bigquery.model.QueryRequest queryRequestPb =
223+
new com.google.api.services.bigquery.model.QueryRequest().setQuery(query);
224+
if (maxResults != null) {
225+
queryRequestPb.setMaxResults(maxResults);
226+
}
227+
if (defaultDataset != null) {
228+
queryRequestPb.setDefaultDataset(defaultDataset.toPb());
229+
}
230+
if (maxWaitTime != null) {
231+
queryRequestPb.setTimeoutMs(maxWaitTime);
232+
}
233+
if (dryRun != null) {
234+
queryRequestPb.setDryRun(dryRun);
235+
}
236+
if (useQueryCache != null) {
237+
queryRequestPb.setUseQueryCache(useQueryCache);
238+
}
239+
return queryRequestPb;
240+
}
241+
242+
/**
243+
* Creates a builder for a {@code QueryRequest} given the BigQuery SQL query to be executed.
244+
*/
245+
public static Builder builder(String query) {
246+
return new Builder().query(query);
247+
}
248+
249+
/**
250+
* Creates a {@code QueryRequest} object given the BigQuery SQL query to be executed.
251+
*/
252+
public static QueryRequest of(String query) {
253+
return new Builder().query(query).build();
254+
}
255+
256+
static QueryRequest fromPb(com.google.api.services.bigquery.model.QueryRequest queryRequestPb) {
257+
Builder builder = builder(queryRequestPb.getQuery());
258+
if (queryRequestPb.getMaxResults() != null) {
259+
builder.maxResults(queryRequestPb.getMaxResults());
260+
}
261+
if (queryRequestPb.getDefaultDataset() != null) {
262+
builder.defaultDataset(DatasetId.fromPb(queryRequestPb.getDefaultDataset()));
263+
}
264+
if (queryRequestPb.getTimeoutMs() != null) {
265+
builder.maxWaitTime(queryRequestPb.getTimeoutMs());
266+
}
267+
if (queryRequestPb.getDryRun() != null) {
268+
builder.dryRun(queryRequestPb.getDryRun());
269+
}
270+
if (queryRequestPb.getUseQueryCache() != null) {
271+
builder.useQueryCache(queryRequestPb.getUseQueryCache());
272+
}
273+
return builder.build();
274+
}
275+
}

0 commit comments

Comments
 (0)