diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/DatasetSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/DatasetSnippets.java
new file mode 100644
index 000000000000..3cd05860a97a
--- /dev/null
+++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/DatasetSnippets.java
@@ -0,0 +1,164 @@
+/*
+ * Copyright 2016 Google Inc. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * EDITING INSTRUCTIONS
+ * This file is referenced in Dataset’s javadoc. Any change to this file should be reflected in
+ * Dataset’s javadoc.
+*/
+
+package com.google.cloud.examples.bigquery.snippets;
+
+import com.google.cloud.Page;
+import com.google.cloud.bigquery.BigQuery.TableListOption;
+import com.google.cloud.bigquery.Dataset;
+import com.google.cloud.bigquery.Dataset.Builder;
+import com.google.cloud.bigquery.Field;
+import com.google.cloud.bigquery.Schema;
+import com.google.cloud.bigquery.StandardTableDefinition;
+import com.google.cloud.bigquery.Table;
+import java.util.Iterator;
+
+/**
+ * This class contains a number of snippets for the {@link Dataset} interface.
+ */
+public class DatasetSnippets {
+
+ private final Dataset dataset;
+
+ public DatasetSnippets(Dataset dataset) {
+ this.dataset = dataset;
+ }
+
+ /**
+ * Example of checking whether a dataset exists.
+ */
+ // [TARGET exists()]
+ public boolean doesDatasetExist() {
+ // [START doesDatasetExist]
+ boolean exists = this.dataset.exists();
+ // [END doesDatasetExist]
+ return exists;
+ }
+
+ /**
+ * Example of reloading a dataset.
+ */
+ // [TARGET reload(BigQuery.DatasetOption... options)]
+ public Dataset reloadDataset() {
+ // [START reloadDataset]
+ Dataset dataset = this.dataset.reload();
+ if (dataset != null) {
+ // The dataset was reloaded.
+ } else {
+ // The dataset was not found.
+ }
+ // [END reloadDataset]
+ return dataset;
+ }
+
+ /**
+ * Example of updating a dataset.
+ */
+ // [TARGET update(BigQuery.DatasetOption... options)]
+ // [VARIABLE "my_friendly_name"]
+ public Dataset updateDataset(String friendlyName) {
+ // [START updateDataset]
+ Builder builder = this.dataset.toBuilder();
+ builder.friendlyName(friendlyName);
+ Dataset updatedDataset = builder.build().update();
+ // [END updateDataset]
+ return updatedDataset;
+ }
+
+ /**
+ * Example of deleting a dataset.
+ */
+ // [TARGET delete()]
+ public boolean deleteDataset() {
+ // [START deleteDataset]
+ boolean deleted = this.dataset.delete();
+ if (deleted) {
+ // The dataset was deleted.
+ } else {
+ // The dataset was not found.
+ }
+ // [END deleteDataset]
+ return deleted;
+ }
+
+ /**
+ * Example of listing dataset tables.
+ */
+ // [TARGET list(BigQuery.TableListOption... options)]
+ public Page
listDataset() {
+ // [START listDataset]
+ Page tables = dataset.list();
+ Iterator tableIterator = tables.iterateAll();
+ while (tableIterator.hasNext()) {
+ Table table = tableIterator.next();
+ // do something with the table
+ }
+ // [END listDataset]
+ return tables;
+ }
+
+ /**
+ * Example of getting a dataset table.
+ */
+ // [TARGET get(String table, BigQuery.TableOption... options)]
+ // [VARIABLE “my_table”]
+ public Table getTable(String tableName) {
+ // [START getTable]
+ Table table = dataset.get(tableName);
+ // [END getTable]
+ return table;
+ }
+
+ /**
+ * Example of creating an empty dataset table.
+ */
+ // [TARGET create(String table, TableDefinition definition, BigQuery.TableOption... options)]
+ // [VARIABLE “my_table”]
+ public Table createTable(String tableName) {
+ // [START createTable]
+ StandardTableDefinition definition = StandardTableDefinition.builder()
+ .build();
+ Table table = dataset.create(tableName, definition);
+ // [END createTable]
+ return table;
+ }
+
+ /**
+ * Example of creating a dataset table with schema and time partitioning.
+ */
+ // [TARGET create(String table, TableDefinition definition, BigQuery.TableOption... options)]
+ // [VARIABLE “my_table”]
+ // [VARIABLE “my_field”]
+ public Table createTable(String tableName, String fieldName) {
+ // [START createTable]
+ Schema schema = Schema.builder()
+ .addField(Field.of(fieldName, Field.Type.string()))
+ .build();
+ StandardTableDefinition definition = StandardTableDefinition.builder()
+ .schema(schema)
+ .build();
+ Table table = dataset.create(tableName, definition);
+ // [END createTable]
+ return table;
+ }
+
+}
diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITDatasetSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITDatasetSnippets.java
new file mode 100644
index 000000000000..3f8ef00a916b
--- /dev/null
+++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITDatasetSnippets.java
@@ -0,0 +1,174 @@
+/*
+ * Copyright 2016 Google Inc. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.cloud.examples.bigquery.snippets;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import com.google.cloud.Page;
+import com.google.cloud.bigquery.BigQuery;
+import com.google.cloud.bigquery.BigQueryOptions;
+import com.google.cloud.bigquery.Dataset;
+import com.google.cloud.bigquery.Dataset.Builder;
+import com.google.cloud.bigquery.DatasetInfo;
+import com.google.cloud.bigquery.Field;
+import com.google.cloud.bigquery.StandardTableDefinition;
+import com.google.cloud.bigquery.Table;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.util.Iterator;
+
+public class ITDatasetSnippets {
+ private static final String datasetId = "dataset_snippets_integration_test";
+ private static final String nonExistandDatasetId = "non_existant_dataset";
+ private static final String friendlyName = "some_friendly_name";
+
+ private static BigQuery bigquery;
+ private static Dataset dataset;
+ private static DatasetSnippets datasetSnippets;
+
+ @BeforeClass
+ public static void beforeClass() {
+ bigquery = BigQueryOptions.defaultInstance().service();
+ }
+
+ @Before
+ public void before() {
+ dataset = bigquery.create(DatasetInfo.builder(datasetId).build());
+ datasetSnippets = new DatasetSnippets(dataset);
+ }
+
+ @After
+ public void after() {
+ bigquery.delete(datasetId, BigQuery.DatasetDeleteOption.deleteContents());
+ }
+
+ @Test
+ public void testDoesDataExistReturnsFalseWhenDatasetDoesntExist() {
+ Dataset nonExistantDataset = bigquery.create(DatasetInfo.builder(nonExistandDatasetId).build());
+ DatasetSnippets datasetSnippetsWithNonExistantDataset = new DatasetSnippets(nonExistantDataset);
+ bigquery.delete(nonExistandDatasetId, BigQuery.DatasetDeleteOption.deleteContents());
+ assertFalse(datasetSnippetsWithNonExistantDataset.doesDatasetExist());
+ }
+
+ @Test
+ public void testDoesDataExistReturnsTrueWhenDatasetExists() {
+ assertTrue(datasetSnippets.doesDatasetExist());
+ }
+
+ @Test
+ public void testReloadDatasetReturnsNullWhenDatasetDoesntExist() {
+ Dataset nonExistantDataset = bigquery.create(DatasetInfo.builder(nonExistandDatasetId).build());
+ DatasetSnippets datasetSnippetsWithNonExistantDataset = new DatasetSnippets(nonExistantDataset);
+ bigquery.delete(nonExistandDatasetId, BigQuery.DatasetDeleteOption.deleteContents());
+ assertNull(datasetSnippetsWithNonExistantDataset.reloadDataset());
+ }
+
+ @Test
+ public void testReloadDatasetReturnsTheReloadedDatasetWhenDatasetExists() {
+ assertNull(dataset.friendlyName());
+
+ Builder builder = dataset.toBuilder();
+ builder.friendlyName(friendlyName);
+ builder.build().update();
+
+ Dataset reloadedDataset = datasetSnippets.reloadDataset();
+ assertEquals(friendlyName, reloadedDataset.friendlyName());
+ }
+
+ @Test
+ public void testUpdateDatasetReturnsTheUpdatedDatasetWhenDatasetExists() {
+ assertNull(dataset.friendlyName());
+
+ Dataset updatedDataset = datasetSnippets.updateDataset(friendlyName);
+ assertEquals(friendlyName, updatedDataset.friendlyName());
+ }
+
+ @Test
+ public void testDeleteDatasetReturnsFalseWhenDatasetDoesntExist() {
+ Dataset nonExistantDataset = bigquery.create(DatasetInfo.builder(nonExistandDatasetId).build());
+ DatasetSnippets datasetSnippetsWithNonExistantDataset = new DatasetSnippets(nonExistantDataset);
+ bigquery.delete(nonExistandDatasetId, BigQuery.DatasetDeleteOption.deleteContents());
+ assertFalse(datasetSnippetsWithNonExistantDataset.deleteDataset());
+ }
+
+ @Test
+ public void testDeleteDatasetReturnsTrueWhenDatasetExists() {
+ assertTrue(datasetSnippets.deleteDataset());
+ }
+
+ @Test
+ public void testListTablesWhenEmpty() {
+ Page tables = datasetSnippets.listDataset();
+ assertFalse(tables.iterateAll().hasNext());
+ }
+
+ @Test
+ public void testListTablesWhenNotEmpty() {
+ String expectedTableName = "test_table";
+
+ dataset.create(expectedTableName, StandardTableDefinition.builder().build());
+ Page tables = datasetSnippets.listDataset();
+ Iterator iterator = tables.iterateAll();
+ assertTrue(iterator.hasNext());
+
+ Table actualTable = iterator.next();
+ assertTrue(actualTable.tableId().table().equals(expectedTableName));
+ assertFalse(iterator.hasNext());
+ }
+
+ @Test
+ public void testGetTable() {
+ String expectedTableName = "test_table";
+
+ dataset.create(expectedTableName, StandardTableDefinition.builder().build());
+ Table actualTable = datasetSnippets.getTable(expectedTableName);
+
+ Assert.assertNotNull(actualTable);
+ Assert.assertEquals(expectedTableName, actualTable.tableId().table());
+ }
+
+ @Test
+ public void testCreateTable() {
+ String expectedTableName = "test_table";
+
+ Table actualTable = datasetSnippets.createTable(expectedTableName);
+ Assert.assertNotNull(actualTable);
+ Assert.assertEquals(expectedTableName, actualTable.tableId().table());
+ }
+
+ @Test
+ public void testCreateTableWithSchema() {
+ String expectedTableName = "test_table";
+ String expectedFieldName = "test_field";
+
+ Table actualTable = datasetSnippets.createTable(expectedTableName, expectedFieldName);
+ Assert.assertNotNull(actualTable);
+ Assert.assertEquals(expectedTableName, actualTable.tableId().table());
+ Assert.assertEquals(1, actualTable.definition().schema().fields().size());
+
+ Field actualField = actualTable.definition().schema().fields().get(0);
+ Assert.assertEquals(expectedFieldName, actualField.name());
+ }
+}