Skip to content

Commit

Permalink
Minor fixes to Datastore snippets, generated javadoc, add option to D…
Browse files Browse the repository at this point in the history
…atastore.delete
  • Loading branch information
mziccard committed Sep 19, 2016
1 parent 180b73e commit 3240801
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.cloud.Page;
import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption;
import com.google.cloud.bigquery.BigQuery.DatasetOption;
import com.google.cloud.bigquery.BigQuery.TableListOption;
import com.google.cloud.bigquery.BigQuery.TableOption;
Expand Down Expand Up @@ -144,6 +145,16 @@ public Dataset build() {
/**
* Checks if this dataset exists.
*
* <p>Example of checking whether a dataset exists.
* <pre> {@code
* boolean exists = dataset.exists();
* if (exists) {
* // the dataset exists
* } else {
* // the dataset was not found
* }
* }</pre>
*
* @return {@code true} if this dataset exists, {@code false} otherwise
* @throws BigQueryException upon failure
*/
Expand All @@ -155,6 +166,14 @@ public boolean exists() {
* Fetches current dataset's latest information. Returns {@code null} if the dataset does not
* exist.
*
* <p>Example of reloading a dataset.
* <pre> {@code
* Dataset latestDataset = dataset.reload();
* if (latestDataset == null) {
* // The dataset was not found
* }
* }</pre>
*
* @param options dataset options
* @return a {@code Dataset} object with latest information or {@code null} if not found
* @throws BigQueryException upon failure
Expand All @@ -167,6 +186,14 @@ public Dataset reload(DatasetOption... options) {
* Updates the dataset's information with this dataset's information. Dataset's user-defined id
* cannot be changed. A new {@code Dataset} object is returned.
*
* <p>Example of updating a dataset.
* <pre> {@code
* String friendlyName = "my_friendly_name";
* Builder builder = dataset.toBuilder();
* builder.friendlyName(friendlyName);
* Dataset updatedDataset = builder.build().update();
* }</pre>
*
* @param options dataset options
* @return a {@code Dataset} object with updated information
* @throws BigQueryException upon failure
Expand All @@ -178,16 +205,36 @@ public Dataset update(DatasetOption... options) {
/**
* Deletes this dataset.
*
* <p>Example of deleting a dataset.
* <pre> {@code
* boolean deleted = dataset.delete();
* if (deleted) {
* // The dataset was deleted
* } else {
* // The dataset was not found
* }
* }</pre>
*
* @return {@code true} if dataset was deleted, {@code false} if it was not found
* @throws BigQueryException upon failure
*/
public boolean delete() {
return bigquery.delete(datasetId());
public boolean delete(DatasetDeleteOption... options) {
return bigquery.delete(datasetId(), options);
}

/**
* Returns the paginated list of tables in this dataset.
*
* <p>Example of listing tables in the dataset.
* <pre> {@code
* Page<Table> tables = dataset.list();
* Iterator<Table> tableIterator = tables.iterateAll();
* while (tableIterator.hasNext()) {
* Table table = tableIterator.next();
* // do something with the table
* }
* }</pre>
*
* @param options options for listing tables
* @throws BigQueryException upon failure
*/
Expand All @@ -198,6 +245,12 @@ public Page<Table> list(TableListOption... options) {
/**
* Returns the requested table in this dataset or {@code null} if not found.
*
* <p>Example of getting a table in the dataset.
* <pre> {@code
* String tableName = “my_table”;
* Table table = dataset.get(tableName);
* }</pre>
*
* @param table user-defined id of the requested table
* @param options table options
* @throws BigQueryException upon failure
Expand All @@ -209,6 +262,18 @@ public Table get(String table, TableOption... options) {
/**
* Creates a new table in this dataset.
*
* <p>Example of creating a table in the dataset with schema and time partitioning.
* <pre> {@code
* String tableName = “my_table”;
* String fieldName = “my_field”;
* Schema schema = Schema.of(Field.of(fieldName, Type.string()));
* StandardTableDefinition definition = StandardTableDefinition.builder()
* .schema(schema)
* .timePartitioning(TimePartitioning.of(TimePartitioning.Type.DAY))
* .build();
* Table table = dataset.create(tableName, definition);
* }</pre>
*
* @param table the table's user-defined id
* @param definition the table's definition
* @param options options for table creation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
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.Field.Type;
import com.google.cloud.bigquery.Schema;
import com.google.cloud.bigquery.StandardTableDefinition;
import com.google.cloud.bigquery.Table;
import com.google.cloud.bigquery.TimePartitioning;

import java.util.Iterator;

/**
Expand All @@ -48,117 +50,104 @@ public DatasetSnippets(Dataset dataset) {
*/
// [TARGET exists()]
public boolean doesDatasetExist() {
// [START doesDatasetExist]
boolean exists = this.dataset.exists();
// [END doesDatasetExist]
// [START exists]
boolean exists = dataset.exists();
if (exists) {
// the dataset exists
} else {
// the dataset was not found
}
// [END exists]
return exists;
}

/**
* Example of reloading a dataset.
*/
// [TARGET reload(BigQuery.DatasetOption... options)]
// [TARGET reload(DatasetOption...)]
public Dataset reloadDataset() {
// [START reloadDataset]
Dataset dataset = this.dataset.reload();
if (dataset != null) {
// The dataset was reloaded.
} else {
// The dataset was not found.
// [START reload]
Dataset latestDataset = dataset.reload();
if (latestDataset == null) {
// The dataset was not found
}
// [END reloadDataset]
return dataset;
// [END reload]
return latestDataset;
}

/**
* Example of updating a dataset.
*/
// [TARGET update(BigQuery.DatasetOption... options)]
// [TARGET update(DatasetOption...)]
// [VARIABLE "my_friendly_name"]
public Dataset updateDataset(String friendlyName) {
// [START updateDataset]
Builder builder = this.dataset.toBuilder();
// [START update]
Builder builder = dataset.toBuilder();
builder.friendlyName(friendlyName);
Dataset updatedDataset = builder.build().update();
// [END updateDataset]
// [END update]
return updatedDataset;
}

/**
* Example of deleting a dataset.
*/
// [TARGET delete()]
// [TARGET delete(DatasetDeleteOption...)]
public boolean deleteDataset() {
// [START deleteDataset]
boolean deleted = this.dataset.delete();
// [START delete]
boolean deleted = dataset.delete();
if (deleted) {
// The dataset was deleted.
// The dataset was deleted
} else {
// The dataset was not found.
// The dataset was not found
}
// [END deleteDataset]
// [END delete]
return deleted;
}

/**
* Example of listing dataset tables.
* Example of listing tables in the dataset.
*/
// [TARGET list(BigQuery.TableListOption... options)]
public Page<Table> listDataset() {
// [START listDataset]
// [TARGET list(TableListOption...)]
public Page<Table> list() {
// [START list]
Page<Table> tables = dataset.list();
Iterator<Table> tableIterator = tables.iterateAll();
while (tableIterator.hasNext()) {
Table table = tableIterator.next();
// do something with the table
}
// [END listDataset]
// [END list]
return tables;
}

/**
* Example of getting a dataset table.
* Example of getting a table in the dataset.
*/
// [TARGET get(String table, BigQuery.TableOption... options)]
// [TARGET get(String, TableOption...)]
// [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.
* Example of creating a table in the dataset with schema and time partitioning.
*/
// [TARGET create(String table, TableDefinition definition, BigQuery.TableOption... options)]
// [TARGET create(String, TableDefinition, TableOption...)]
// [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();
Schema schema = Schema.of(Field.of(fieldName, Type.string()));
StandardTableDefinition definition = StandardTableDefinition.builder()
.schema(schema)
.timePartitioning(TimePartitioning.of(TimePartitioning.Type.DAY))
.build();
Table table = dataset.create(tableName, definition);
// [END createTable]
return table;
}

}
Loading

0 comments on commit 3240801

Please sign in to comment.