Skip to content

Commit 5e734ee

Browse files
committed
Add functional classes for BigQuery datasets jobs and tables
1 parent 823f0de commit 5e734ee

6 files changed

Lines changed: 1259 additions & 0 deletions

File tree

Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
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.checkArgument;
20+
import static com.google.common.base.Preconditions.checkNotNull;
21+
22+
import com.google.common.base.Function;
23+
import com.google.common.collect.Iterators;
24+
import com.google.gcloud.Page;
25+
import com.google.gcloud.PageImpl;
26+
27+
import java.io.IOException;
28+
import java.io.ObjectInputStream;
29+
import java.io.Serializable;
30+
import java.util.Iterator;
31+
import java.util.List;
32+
import java.util.Objects;
33+
34+
/**
35+
* A Google BigQuery Dataset.
36+
*
37+
* <p>Objects of this class are immutable. Operations that modify the dataset like {@link #update}
38+
* return a new object. To get a {@code Dataset} object with the most recent information use
39+
* {@link #reload}.
40+
* </p>
41+
*/
42+
public final class Dataset {
43+
44+
private final BigQuery bigquery;
45+
private final DatasetInfo info;
46+
47+
private static class TablePageFetcher implements PageImpl.NextPageFetcher<Table> {
48+
49+
private static final long serialVersionUID = 6906197848579250598L;
50+
51+
private final BigQueryOptions options;
52+
private final Page<BaseTableInfo> infoPage;
53+
54+
TablePageFetcher(BigQueryOptions options, Page<BaseTableInfo> infoPage) {
55+
this.options = options;
56+
this.infoPage = infoPage;
57+
}
58+
59+
@Override
60+
public Page<Table> nextPage() {
61+
Page<BaseTableInfo> nextInfoPage = infoPage.nextPage();
62+
return new PageImpl<>(new TablePageFetcher(options, nextInfoPage),
63+
nextInfoPage.nextPageCursor(), new LazyTableIterable(options, nextInfoPage.values()));
64+
}
65+
}
66+
67+
private static class LazyTableIterable implements Iterable<Table>, Serializable {
68+
69+
private static final long serialVersionUID = 3312744215731674032L;
70+
71+
private final BigQueryOptions options;
72+
private Iterable<BaseTableInfo> infoIterable;
73+
private transient BigQuery bigquery;
74+
75+
public LazyTableIterable(BigQueryOptions options, Iterable<BaseTableInfo> infoIterable) {
76+
this.options = options;
77+
this.infoIterable = infoIterable;
78+
this.bigquery = options.service();
79+
}
80+
81+
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
82+
in.defaultReadObject();
83+
this.bigquery = options.service();
84+
}
85+
86+
@Override
87+
public Iterator<Table> iterator() {
88+
return Iterators.transform(infoIterable.iterator(), new Function<BaseTableInfo, Table>() {
89+
@Override
90+
public Table apply(BaseTableInfo tableInfo) {
91+
return new Table(bigquery, tableInfo);
92+
}
93+
});
94+
}
95+
96+
@Override
97+
public int hashCode() {
98+
return Objects.hash(options, infoIterable);
99+
}
100+
101+
@Override
102+
public boolean equals(Object obj) {
103+
if (!(obj instanceof LazyTableIterable)) {
104+
return false;
105+
}
106+
LazyTableIterable other = (LazyTableIterable) obj;
107+
return Objects.equals(options, other.options)
108+
&& Objects.equals(infoIterable, other.infoIterable);
109+
}
110+
}
111+
112+
/**
113+
* Constructs a {@code Dataset} object for the provided {@code DatasetInfo}. The BigQuery service
114+
* is used to issue requests.
115+
*
116+
* @param bigquery the BigQuery service used for issuing requests
117+
* @param info dataset's info
118+
*/
119+
public Dataset(BigQuery bigquery, DatasetInfo info) {
120+
this.bigquery = checkNotNull(bigquery);
121+
this.info = checkNotNull(info);
122+
}
123+
124+
/**
125+
* Creates a {@code Dataset} object for the provided dataset's user-defined id. Performs an RPC
126+
* call to get the latest dataset information.
127+
*
128+
* @param bigquery the BigQuery service used for issuing requests
129+
* @param dataset dataset's user-defined id
130+
* @return the {@code Dataset} object or {@code null} if not found.
131+
* @throws BigQueryException upon failure
132+
*/
133+
public static Dataset load(BigQuery bigquery, String dataset) {
134+
DatasetInfo info = bigquery.getDataset(dataset);
135+
return info != null ? new Dataset(bigquery, info) : null;
136+
}
137+
138+
/**
139+
* Returns the dataset's information.
140+
*/
141+
public DatasetInfo info() {
142+
return info;
143+
}
144+
145+
/**
146+
* Checks if this dataset exists.
147+
*
148+
* @return {@code true} if this dataset exists, {@code false} otherwise.
149+
* @throws BigQueryException upon failure
150+
*/
151+
public boolean exists() {
152+
return bigquery.getDataset(info.datasetId(), BigQuery.DatasetOption.fields()) != null;
153+
}
154+
155+
/**
156+
* Fetches current dataset's latest information.
157+
*
158+
* @param options dataset options
159+
* @return a {@code Dataset} object with latest information.
160+
* @throws BigQueryException upon failure
161+
*/
162+
public Dataset reload(BigQuery.DatasetOption... options) {
163+
return new Dataset(bigquery, bigquery.getDataset(info.datasetId(), options));
164+
}
165+
166+
/**
167+
* Updates the dataset's information. Dataset's user-defined id cannot be changed. A new
168+
* {@code Dataset} object is returned.
169+
*
170+
* @param datasetInfo new dataset's information. User-defined id must match the one of the current
171+
* dataset
172+
* @param options dataset options
173+
* @return a {@code Dataset} object with updated information.
174+
* @throws BigQueryException upon failure
175+
*/
176+
public Dataset update(DatasetInfo datasetInfo, BigQuery.DatasetOption... options) {
177+
checkArgument(Objects.equals(datasetInfo.datasetId().dataset(),
178+
info.datasetId().dataset()), "Dataset's user-defined ids must match");
179+
return new Dataset(bigquery, bigquery.update(datasetInfo, options));
180+
}
181+
182+
/**
183+
* Deletes this dataset.
184+
*
185+
* @return {@code true} if dataset was deleted, {@code false} if it was not found.
186+
* @throws BigQueryException upon failure
187+
*/
188+
public boolean delete() {
189+
return bigquery.delete(info.datasetId());
190+
}
191+
192+
/**
193+
* Returns the paginated list of tables in this dataset.
194+
*
195+
* @param options options for listing tables
196+
* @throws BigQueryException upon failure
197+
*/
198+
public Page<Table> list(BigQuery.TableListOption... options) {
199+
Page<BaseTableInfo> infoPage = bigquery.listTables(info.datasetId(), options);
200+
BigQueryOptions bigqueryOptions = bigquery.options();
201+
return new PageImpl<>(new TablePageFetcher(bigqueryOptions, infoPage),
202+
infoPage.nextPageCursor(), new LazyTableIterable(bigqueryOptions, infoPage.values()));
203+
}
204+
205+
/**
206+
* Returns the requested table in this dataset or {@code null} if not found.
207+
*
208+
* @param table user-defined id of the requested table
209+
* @param options table options
210+
* @throws BigQueryException upon failure
211+
*/
212+
public Table get(String table, BigQuery.TableOption... options) {
213+
BaseTableInfo tableInfo =
214+
bigquery.getTable(TableId.of(info.datasetId().dataset(), table), options);
215+
return tableInfo != null ? new Table(bigquery, tableInfo) : null;
216+
}
217+
218+
/**
219+
* Creates a new simple table in this dataset.
220+
*
221+
* @param table the table's user-defined id
222+
* @param schema the table's schema
223+
* @param options options for table creation
224+
* @return a {@code Table} object for the created table.
225+
* @throws BigQueryException upon failure
226+
*/
227+
public Table create(String table, Schema schema, BigQuery.TableOption... options) {
228+
BaseTableInfo tableInfo = TableInfo.of(TableId.of(info.datasetId().dataset(), table), schema);
229+
return new Table(bigquery, bigquery.create(tableInfo, options));
230+
}
231+
232+
/**
233+
* Creates a new view table in this dataset.
234+
*
235+
* @param table the table's user-defined id
236+
* @param query the query used to generate the table
237+
* @param functions user-defined functions that can be used by the query
238+
* @param options options for table creation
239+
* @return a {@code Table} object for the created table.
240+
* @throws BigQueryException upon failure
241+
*/
242+
public Table create(String table, String query, List<UserDefinedFunction> functions,
243+
BigQuery.TableOption... options) {
244+
BaseTableInfo tableInfo =
245+
ViewInfo.of(TableId.of(info.datasetId().dataset(), table), query, functions);
246+
return new Table(bigquery, bigquery.create(tableInfo, options));
247+
}
248+
249+
/**
250+
* Creates a new view table in this dataset.
251+
*
252+
* @param table the table's user-defined id
253+
* @param query the query used to generate the table
254+
* @param options options for table creation
255+
* @return a {@code Table} object for the created table.
256+
* @throws BigQueryException upon failure
257+
*/
258+
public Table create(String table, String query, BigQuery.TableOption... options) {
259+
BaseTableInfo tableInfo = ViewInfo.of(TableId.of(info.datasetId().dataset(), table), query);
260+
return new Table(bigquery, bigquery.create(tableInfo, options));
261+
}
262+
263+
/**
264+
* Creates a new external table in this dataset.
265+
*
266+
* @param table the table's user-defined id
267+
* @param configuration data format, location and other properties of an external table
268+
* @param options options for table creation
269+
* @return a {@code Table} object for the created table.
270+
* @throws BigQueryException upon failure
271+
*/
272+
public Table create(String table, ExternalDataConfiguration configuration,
273+
BigQuery.TableOption... options) {
274+
BaseTableInfo tableInfo =
275+
ExternalTableInfo.of(TableId.of(info.datasetId().dataset(), table), configuration);
276+
return new Table(bigquery, bigquery.create(tableInfo, options));
277+
}
278+
279+
/**
280+
* Returns the dataset's {@code BigQuery} object used to issue requests.
281+
*/
282+
public BigQuery bigquery() {
283+
return bigquery;
284+
}
285+
}

0 commit comments

Comments
 (0)