|
| 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 | +} |
0 commit comments