Skip to content

Commit 9ee8e1c

Browse files
authored
Merge pull request #1268 from mziccard/dataset-snippets
Add snippets for BigQuery Dataset class and tests
2 parents a60d06f + 9fa618e commit 9ee8e1c

3 files changed

Lines changed: 392 additions & 2 deletions

File tree

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Dataset.java

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.base.Preconditions.checkNotNull;
2020

2121
import com.google.cloud.Page;
22+
import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption;
2223
import com.google.cloud.bigquery.BigQuery.DatasetOption;
2324
import com.google.cloud.bigquery.BigQuery.TableListOption;
2425
import com.google.cloud.bigquery.BigQuery.TableOption;
@@ -144,6 +145,16 @@ public Dataset build() {
144145
/**
145146
* Checks if this dataset exists.
146147
*
148+
* <p>Example of checking whether a dataset exists.
149+
* <pre> {@code
150+
* boolean exists = dataset.exists();
151+
* if (exists) {
152+
* // the dataset exists
153+
* } else {
154+
* // the dataset was not found
155+
* }
156+
* }</pre>
157+
*
147158
* @return {@code true} if this dataset exists, {@code false} otherwise
148159
* @throws BigQueryException upon failure
149160
*/
@@ -155,6 +166,14 @@ public boolean exists() {
155166
* Fetches current dataset's latest information. Returns {@code null} if the dataset does not
156167
* exist.
157168
*
169+
* <p>Example of reloading a dataset.
170+
* <pre> {@code
171+
* Dataset latestDataset = dataset.reload();
172+
* if (latestDataset == null) {
173+
* // The dataset was not found
174+
* }
175+
* }</pre>
176+
*
158177
* @param options dataset options
159178
* @return a {@code Dataset} object with latest information or {@code null} if not found
160179
* @throws BigQueryException upon failure
@@ -167,6 +186,14 @@ public Dataset reload(DatasetOption... options) {
167186
* Updates the dataset's information with this dataset's information. Dataset's user-defined id
168187
* cannot be changed. A new {@code Dataset} object is returned.
169188
*
189+
* <p>Example of updating a dataset.
190+
* <pre> {@code
191+
* String friendlyName = "my_friendly_name";
192+
* Builder builder = dataset.toBuilder();
193+
* builder.friendlyName(friendlyName);
194+
* Dataset updatedDataset = builder.build().update();
195+
* }</pre>
196+
*
170197
* @param options dataset options
171198
* @return a {@code Dataset} object with updated information
172199
* @throws BigQueryException upon failure
@@ -178,16 +205,36 @@ public Dataset update(DatasetOption... options) {
178205
/**
179206
* Deletes this dataset.
180207
*
208+
* <p>Example of deleting a dataset.
209+
* <pre> {@code
210+
* boolean deleted = dataset.delete();
211+
* if (deleted) {
212+
* // The dataset was deleted
213+
* } else {
214+
* // The dataset was not found
215+
* }
216+
* }</pre>
217+
*
181218
* @return {@code true} if dataset was deleted, {@code false} if it was not found
182219
* @throws BigQueryException upon failure
183220
*/
184-
public boolean delete() {
185-
return bigquery.delete(datasetId());
221+
public boolean delete(DatasetDeleteOption... options) {
222+
return bigquery.delete(datasetId(), options);
186223
}
187224

188225
/**
189226
* Returns the paginated list of tables in this dataset.
190227
*
228+
* <p>Example of listing tables in the dataset.
229+
* <pre> {@code
230+
* Page<Table> tables = dataset.list();
231+
* Iterator<Table> tableIterator = tables.iterateAll();
232+
* while (tableIterator.hasNext()) {
233+
* Table table = tableIterator.next();
234+
* // do something with the table
235+
* }
236+
* }</pre>
237+
*
191238
* @param options options for listing tables
192239
* @throws BigQueryException upon failure
193240
*/
@@ -198,6 +245,12 @@ public Page<Table> list(TableListOption... options) {
198245
/**
199246
* Returns the requested table in this dataset or {@code null} if not found.
200247
*
248+
* <p>Example of getting a table in the dataset.
249+
* <pre> {@code
250+
* String tableName = “my_table”;
251+
* Table table = dataset.get(tableName);
252+
* }</pre>
253+
*
201254
* @param table user-defined id of the requested table
202255
* @param options table options
203256
* @throws BigQueryException upon failure
@@ -209,6 +262,18 @@ public Table get(String table, TableOption... options) {
209262
/**
210263
* Creates a new table in this dataset.
211264
*
265+
* <p>Example of creating a table in the dataset with schema and time partitioning.
266+
* <pre> {@code
267+
* String tableName = “my_table”;
268+
* String fieldName = “my_field”;
269+
* Schema schema = Schema.of(Field.of(fieldName, Type.string()));
270+
* StandardTableDefinition definition = StandardTableDefinition.builder()
271+
* .schema(schema)
272+
* .timePartitioning(TimePartitioning.of(TimePartitioning.Type.DAY))
273+
* .build();
274+
* Table table = dataset.create(tableName, definition);
275+
* }</pre>
276+
*
212277
* @param table the table's user-defined id
213278
* @param definition the table's definition
214279
* @param options options for table creation
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* Copyright 2016 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+
/*
18+
* EDITING INSTRUCTIONS
19+
* This file is referenced in Dataset’s javadoc. Any change to this file should be reflected in
20+
* Dataset’s javadoc.
21+
*/
22+
23+
package com.google.cloud.examples.bigquery.snippets;
24+
25+
import com.google.cloud.Page;
26+
import com.google.cloud.bigquery.Dataset;
27+
import com.google.cloud.bigquery.Dataset.Builder;
28+
import com.google.cloud.bigquery.Field;
29+
import com.google.cloud.bigquery.Field.Type;
30+
import com.google.cloud.bigquery.Schema;
31+
import com.google.cloud.bigquery.StandardTableDefinition;
32+
import com.google.cloud.bigquery.Table;
33+
import com.google.cloud.bigquery.TimePartitioning;
34+
35+
import java.util.Iterator;
36+
37+
/**
38+
* This class contains a number of snippets for the {@link Dataset} interface.
39+
*/
40+
public class DatasetSnippets {
41+
42+
private final Dataset dataset;
43+
44+
public DatasetSnippets(Dataset dataset) {
45+
this.dataset = dataset;
46+
}
47+
48+
/**
49+
* Example of checking whether a dataset exists.
50+
*/
51+
// [TARGET exists()]
52+
public boolean doesDatasetExist() {
53+
// [START exists]
54+
boolean exists = dataset.exists();
55+
if (exists) {
56+
// the dataset exists
57+
} else {
58+
// the dataset was not found
59+
}
60+
// [END exists]
61+
return exists;
62+
}
63+
64+
/**
65+
* Example of reloading a dataset.
66+
*/
67+
// [TARGET reload(DatasetOption...)]
68+
public Dataset reloadDataset() {
69+
// [START reload]
70+
Dataset latestDataset = dataset.reload();
71+
if (latestDataset == null) {
72+
// The dataset was not found
73+
}
74+
// [END reload]
75+
return latestDataset;
76+
}
77+
78+
/**
79+
* Example of updating a dataset.
80+
*/
81+
// [TARGET update(DatasetOption...)]
82+
// [VARIABLE "my_friendly_name"]
83+
public Dataset updateDataset(String friendlyName) {
84+
// [START update]
85+
Builder builder = dataset.toBuilder();
86+
builder.friendlyName(friendlyName);
87+
Dataset updatedDataset = builder.build().update();
88+
// [END update]
89+
return updatedDataset;
90+
}
91+
92+
/**
93+
* Example of deleting a dataset.
94+
*/
95+
// [TARGET delete(DatasetDeleteOption...)]
96+
public boolean deleteDataset() {
97+
// [START delete]
98+
boolean deleted = dataset.delete();
99+
if (deleted) {
100+
// The dataset was deleted
101+
} else {
102+
// The dataset was not found
103+
}
104+
// [END delete]
105+
return deleted;
106+
}
107+
108+
/**
109+
* Example of listing tables in the dataset.
110+
*/
111+
// [TARGET list(TableListOption...)]
112+
public Page<Table> list() {
113+
// [START list]
114+
Page<Table> tables = dataset.list();
115+
Iterator<Table> tableIterator = tables.iterateAll();
116+
while (tableIterator.hasNext()) {
117+
Table table = tableIterator.next();
118+
// do something with the table
119+
}
120+
// [END list]
121+
return tables;
122+
}
123+
124+
/**
125+
* Example of getting a table in the dataset.
126+
*/
127+
// [TARGET get(String, TableOption...)]
128+
// [VARIABLE “my_table”]
129+
public Table getTable(String tableName) {
130+
// [START getTable]
131+
Table table = dataset.get(tableName);
132+
// [END getTable]
133+
return table;
134+
}
135+
136+
/**
137+
* Example of creating a table in the dataset with schema and time partitioning.
138+
*/
139+
// [TARGET create(String, TableDefinition, TableOption...)]
140+
// [VARIABLE “my_table”]
141+
// [VARIABLE “my_field”]
142+
public Table createTable(String tableName, String fieldName) {
143+
// [START createTable]
144+
Schema schema = Schema.of(Field.of(fieldName, Type.string()));
145+
StandardTableDefinition definition = StandardTableDefinition.builder()
146+
.schema(schema)
147+
.timePartitioning(TimePartitioning.of(TimePartitioning.Type.DAY))
148+
.build();
149+
Table table = dataset.create(tableName, definition);
150+
// [END createTable]
151+
return table;
152+
}
153+
}

0 commit comments

Comments
 (0)