Skip to content

Commit 180b73e

Browse files
wmwongmziccard
authored andcommitted
Add snippets for BigQuery Dataset class and tests.
1 parent 3c62510 commit 180b73e

2 files changed

Lines changed: 338 additions & 0 deletions

File tree

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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.BigQuery.TableListOption;
27+
import com.google.cloud.bigquery.Dataset;
28+
import com.google.cloud.bigquery.Dataset.Builder;
29+
import com.google.cloud.bigquery.Field;
30+
import com.google.cloud.bigquery.Schema;
31+
import com.google.cloud.bigquery.StandardTableDefinition;
32+
import com.google.cloud.bigquery.Table;
33+
import java.util.Iterator;
34+
35+
/**
36+
* This class contains a number of snippets for the {@link Dataset} interface.
37+
*/
38+
public class DatasetSnippets {
39+
40+
private final Dataset dataset;
41+
42+
public DatasetSnippets(Dataset dataset) {
43+
this.dataset = dataset;
44+
}
45+
46+
/**
47+
* Example of checking whether a dataset exists.
48+
*/
49+
// [TARGET exists()]
50+
public boolean doesDatasetExist() {
51+
// [START doesDatasetExist]
52+
boolean exists = this.dataset.exists();
53+
// [END doesDatasetExist]
54+
return exists;
55+
}
56+
57+
/**
58+
* Example of reloading a dataset.
59+
*/
60+
// [TARGET reload(BigQuery.DatasetOption... options)]
61+
public Dataset reloadDataset() {
62+
// [START reloadDataset]
63+
Dataset dataset = this.dataset.reload();
64+
if (dataset != null) {
65+
// The dataset was reloaded.
66+
} else {
67+
// The dataset was not found.
68+
}
69+
// [END reloadDataset]
70+
return dataset;
71+
}
72+
73+
/**
74+
* Example of updating a dataset.
75+
*/
76+
// [TARGET update(BigQuery.DatasetOption... options)]
77+
// [VARIABLE "my_friendly_name"]
78+
public Dataset updateDataset(String friendlyName) {
79+
// [START updateDataset]
80+
Builder builder = this.dataset.toBuilder();
81+
builder.friendlyName(friendlyName);
82+
Dataset updatedDataset = builder.build().update();
83+
// [END updateDataset]
84+
return updatedDataset;
85+
}
86+
87+
/**
88+
* Example of deleting a dataset.
89+
*/
90+
// [TARGET delete()]
91+
public boolean deleteDataset() {
92+
// [START deleteDataset]
93+
boolean deleted = this.dataset.delete();
94+
if (deleted) {
95+
// The dataset was deleted.
96+
} else {
97+
// The dataset was not found.
98+
}
99+
// [END deleteDataset]
100+
return deleted;
101+
}
102+
103+
/**
104+
* Example of listing dataset tables.
105+
*/
106+
// [TARGET list(BigQuery.TableListOption... options)]
107+
public Page<Table> listDataset() {
108+
// [START listDataset]
109+
Page<Table> tables = dataset.list();
110+
Iterator<Table> tableIterator = tables.iterateAll();
111+
while (tableIterator.hasNext()) {
112+
Table table = tableIterator.next();
113+
// do something with the table
114+
}
115+
// [END listDataset]
116+
return tables;
117+
}
118+
119+
/**
120+
* Example of getting a dataset table.
121+
*/
122+
// [TARGET get(String table, BigQuery.TableOption... options)]
123+
// [VARIABLE “my_table”]
124+
public Table getTable(String tableName) {
125+
// [START getTable]
126+
Table table = dataset.get(tableName);
127+
// [END getTable]
128+
return table;
129+
}
130+
131+
/**
132+
* Example of creating an empty dataset table.
133+
*/
134+
// [TARGET create(String table, TableDefinition definition, BigQuery.TableOption... options)]
135+
// [VARIABLE “my_table”]
136+
public Table createTable(String tableName) {
137+
// [START createTable]
138+
StandardTableDefinition definition = StandardTableDefinition.builder()
139+
.build();
140+
Table table = dataset.create(tableName, definition);
141+
// [END createTable]
142+
return table;
143+
}
144+
145+
/**
146+
* Example of creating a dataset table with schema and time partitioning.
147+
*/
148+
// [TARGET create(String table, TableDefinition definition, BigQuery.TableOption... options)]
149+
// [VARIABLE “my_table”]
150+
// [VARIABLE “my_field”]
151+
public Table createTable(String tableName, String fieldName) {
152+
// [START createTable]
153+
Schema schema = Schema.builder()
154+
.addField(Field.of(fieldName, Field.Type.string()))
155+
.build();
156+
StandardTableDefinition definition = StandardTableDefinition.builder()
157+
.schema(schema)
158+
.build();
159+
Table table = dataset.create(tableName, definition);
160+
// [END createTable]
161+
return table;
162+
}
163+
164+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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+
package com.google.cloud.examples.bigquery.snippets;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertFalse;
21+
import static org.junit.Assert.assertNull;
22+
import static org.junit.Assert.assertTrue;
23+
24+
import com.google.cloud.Page;
25+
import com.google.cloud.bigquery.BigQuery;
26+
import com.google.cloud.bigquery.BigQueryOptions;
27+
import com.google.cloud.bigquery.Dataset;
28+
import com.google.cloud.bigquery.Dataset.Builder;
29+
import com.google.cloud.bigquery.DatasetInfo;
30+
import com.google.cloud.bigquery.Field;
31+
import com.google.cloud.bigquery.StandardTableDefinition;
32+
import com.google.cloud.bigquery.Table;
33+
34+
import org.junit.After;
35+
import org.junit.Assert;
36+
import org.junit.Before;
37+
import org.junit.BeforeClass;
38+
import org.junit.Test;
39+
40+
import java.util.Iterator;
41+
42+
public class ITDatasetSnippets {
43+
private static final String datasetId = "dataset_snippets_integration_test";
44+
private static final String nonExistandDatasetId = "non_existant_dataset";
45+
private static final String friendlyName = "some_friendly_name";
46+
47+
private static BigQuery bigquery;
48+
private static Dataset dataset;
49+
private static DatasetSnippets datasetSnippets;
50+
51+
@BeforeClass
52+
public static void beforeClass() {
53+
bigquery = BigQueryOptions.defaultInstance().service();
54+
}
55+
56+
@Before
57+
public void before() {
58+
dataset = bigquery.create(DatasetInfo.builder(datasetId).build());
59+
datasetSnippets = new DatasetSnippets(dataset);
60+
}
61+
62+
@After
63+
public void after() {
64+
bigquery.delete(datasetId, BigQuery.DatasetDeleteOption.deleteContents());
65+
}
66+
67+
@Test
68+
public void testDoesDataExistReturnsFalseWhenDatasetDoesntExist() {
69+
Dataset nonExistantDataset = bigquery.create(DatasetInfo.builder(nonExistandDatasetId).build());
70+
DatasetSnippets datasetSnippetsWithNonExistantDataset = new DatasetSnippets(nonExistantDataset);
71+
bigquery.delete(nonExistandDatasetId, BigQuery.DatasetDeleteOption.deleteContents());
72+
assertFalse(datasetSnippetsWithNonExistantDataset.doesDatasetExist());
73+
}
74+
75+
@Test
76+
public void testDoesDataExistReturnsTrueWhenDatasetExists() {
77+
assertTrue(datasetSnippets.doesDatasetExist());
78+
}
79+
80+
@Test
81+
public void testReloadDatasetReturnsNullWhenDatasetDoesntExist() {
82+
Dataset nonExistantDataset = bigquery.create(DatasetInfo.builder(nonExistandDatasetId).build());
83+
DatasetSnippets datasetSnippetsWithNonExistantDataset = new DatasetSnippets(nonExistantDataset);
84+
bigquery.delete(nonExistandDatasetId, BigQuery.DatasetDeleteOption.deleteContents());
85+
assertNull(datasetSnippetsWithNonExistantDataset.reloadDataset());
86+
}
87+
88+
@Test
89+
public void testReloadDatasetReturnsTheReloadedDatasetWhenDatasetExists() {
90+
assertNull(dataset.friendlyName());
91+
92+
Builder builder = dataset.toBuilder();
93+
builder.friendlyName(friendlyName);
94+
builder.build().update();
95+
96+
Dataset reloadedDataset = datasetSnippets.reloadDataset();
97+
assertEquals(friendlyName, reloadedDataset.friendlyName());
98+
}
99+
100+
@Test
101+
public void testUpdateDatasetReturnsTheUpdatedDatasetWhenDatasetExists() {
102+
assertNull(dataset.friendlyName());
103+
104+
Dataset updatedDataset = datasetSnippets.updateDataset(friendlyName);
105+
assertEquals(friendlyName, updatedDataset.friendlyName());
106+
}
107+
108+
@Test
109+
public void testDeleteDatasetReturnsFalseWhenDatasetDoesntExist() {
110+
Dataset nonExistantDataset = bigquery.create(DatasetInfo.builder(nonExistandDatasetId).build());
111+
DatasetSnippets datasetSnippetsWithNonExistantDataset = new DatasetSnippets(nonExistantDataset);
112+
bigquery.delete(nonExistandDatasetId, BigQuery.DatasetDeleteOption.deleteContents());
113+
assertFalse(datasetSnippetsWithNonExistantDataset.deleteDataset());
114+
}
115+
116+
@Test
117+
public void testDeleteDatasetReturnsTrueWhenDatasetExists() {
118+
assertTrue(datasetSnippets.deleteDataset());
119+
}
120+
121+
@Test
122+
public void testListTablesWhenEmpty() {
123+
Page<Table> tables = datasetSnippets.listDataset();
124+
assertFalse(tables.iterateAll().hasNext());
125+
}
126+
127+
@Test
128+
public void testListTablesWhenNotEmpty() {
129+
String expectedTableName = "test_table";
130+
131+
dataset.create(expectedTableName, StandardTableDefinition.builder().build());
132+
Page<Table> tables = datasetSnippets.listDataset();
133+
Iterator<Table> iterator = tables.iterateAll();
134+
assertTrue(iterator.hasNext());
135+
136+
Table actualTable = iterator.next();
137+
assertTrue(actualTable.tableId().table().equals(expectedTableName));
138+
assertFalse(iterator.hasNext());
139+
}
140+
141+
@Test
142+
public void testGetTable() {
143+
String expectedTableName = "test_table";
144+
145+
dataset.create(expectedTableName, StandardTableDefinition.builder().build());
146+
Table actualTable = datasetSnippets.getTable(expectedTableName);
147+
148+
Assert.assertNotNull(actualTable);
149+
Assert.assertEquals(expectedTableName, actualTable.tableId().table());
150+
}
151+
152+
@Test
153+
public void testCreateTable() {
154+
String expectedTableName = "test_table";
155+
156+
Table actualTable = datasetSnippets.createTable(expectedTableName);
157+
Assert.assertNotNull(actualTable);
158+
Assert.assertEquals(expectedTableName, actualTable.tableId().table());
159+
}
160+
161+
@Test
162+
public void testCreateTableWithSchema() {
163+
String expectedTableName = "test_table";
164+
String expectedFieldName = "test_field";
165+
166+
Table actualTable = datasetSnippets.createTable(expectedTableName, expectedFieldName);
167+
Assert.assertNotNull(actualTable);
168+
Assert.assertEquals(expectedTableName, actualTable.tableId().table());
169+
Assert.assertEquals(1, actualTable.definition().schema().fields().size());
170+
171+
Field actualField = actualTable.definition().schema().fields().get(0);
172+
Assert.assertEquals(expectedFieldName, actualField.name());
173+
}
174+
}

0 commit comments

Comments
 (0)