Class: Gcloud::Bigquery::View
- Inherits:
-
Object
- Object
- Gcloud::Bigquery::View
- Defined in:
- lib/gcloud/bigquery/view.rb
Overview
# View
A view is a virtual table defined by a SQL query. You can query views in the browser tool, or by using a query job.
BigQuery’s views are logical views, not materialized views, which means that the query that defines the view is re-executed every time the view is queried. Queries are billed according to the total amount of data in all table fields referenced directly or indirectly by the top-level query.
Instance Attribute Summary collapse
Attributes collapse
-
#api_url ⇒ Object
A URL that can be used to access the dataset using the REST API.
-
#created_at ⇒ Object
The time when this table was created.
-
#dataset_id ⇒ Object
The ID of the ‘Dataset` containing this table.
-
#description ⇒ Object
The description of the table.
-
#description=(new_description) ⇒ Object
Updates the description of the table.
-
#etag ⇒ Object
A string hash of the dataset.
-
#expires_at ⇒ Object
The time when this table expires.
-
#fields ⇒ Object
The fields of the view.
-
#headers ⇒ Object
The names of the columns in the view.
-
#id ⇒ Object
The combined Project ID, Dataset ID, and Table ID for this table, in the format specified by the [Query Reference](cloud.google.com/bigquery/query-reference#from): ‘project_name:datasetId.tableId`.
-
#location ⇒ Object
The geographic location where the table should reside.
-
#modified_at ⇒ Object
The date when this table was last modified.
-
#name ⇒ Object
The name of the table.
-
#name=(new_name) ⇒ Object
Updates the name of the table.
-
#project_id ⇒ Object
The ID of the ‘Project` containing this table.
-
#query ⇒ Object
The query that executes each time the view is loaded.
-
#query_id ⇒ Object
The value returned by #id, wrapped in square brackets if the Project ID contains dashes, as specified by the [Query Reference](cloud.google.com/bigquery/query-reference#from).
-
#schema ⇒ Object
The schema of the view.
-
#table? ⇒ Boolean
Checks if the table’s type is “TABLE”.
-
#table_id ⇒ Object
A unique ID for this table.
-
#table_ref ⇒ Object
The gapi fragment containing the Project ID, Dataset ID, and Table ID as a camel-cased hash.
-
#view? ⇒ Boolean
Checks if the table’s type is “VIEW”.
Lifecycle collapse
- .from_gapi(gapi, conn) ⇒ Object
-
#delete ⇒ Boolean
Permanently deletes the table.
-
#query=(new_query) ⇒ Object
Updates the query that executes each time the view is loaded.
-
#reload! ⇒ Object
(also: #refresh!)
Reloads the table with current data from the BigQuery service.
Data collapse
-
#data(max: nil, timeout: 10000, cache: true, dryrun: nil) ⇒ Gcloud::Bigquery::QueryData
Runs a query to retrieve all data from the view.
Instance Method Summary collapse
-
#initialize ⇒ View
constructor
A new instance of View.
Constructor Details
#initialize ⇒ View
Returns a new instance of View.
55 56 57 58 |
# File 'lib/gcloud/bigquery/view.rb', line 55 def initialize @service = nil @gapi = Google::Apis::BigqueryV2::Table.new end |
Instance Attribute Details
#gapi ⇒ Object
51 52 53 |
# File 'lib/gcloud/bigquery/view.rb', line 51 def gapi @gapi end |
#service ⇒ Object
47 48 49 |
# File 'lib/gcloud/bigquery/view.rb', line 47 def service @service end |
Class Method Details
.from_gapi(gapi, conn) ⇒ Object
417 418 419 420 421 422 |
# File 'lib/gcloud/bigquery/view.rb', line 417 def self.from_gapi gapi, conn new.tap do |f| f.gapi = gapi f.service = conn end end |
Instance Method Details
#api_url ⇒ Object
A URL that can be used to access the dataset using the REST API.
168 169 170 171 |
# File 'lib/gcloud/bigquery/view.rb', line 168 def api_url ensure_full_data! @gapi.self_link end |
#created_at ⇒ Object
The time when this table was created.
198 199 200 201 202 203 204 205 |
# File 'lib/gcloud/bigquery/view.rb', line 198 def created_at ensure_full_data! begin Time.at(Integer(@gapi.creation_time) / 1000.0) rescue nil end end |
#data(max: nil, timeout: 10000, cache: true, dryrun: nil) ⇒ Gcloud::Bigquery::QueryData
Runs a query to retrieve all data from the view.
372 373 374 375 376 377 378 |
# File 'lib/gcloud/bigquery/view.rb', line 372 def data max: nil, timeout: 10000, cache: true, dryrun: nil sql = "SELECT * FROM #{query_id}" ensure_service! = { max: max, timeout: timeout, cache: cache, dryrun: dryrun } gapi = service.query sql, QueryData.from_gapi gapi, service end |
#dataset_id ⇒ Object
The ID of the ‘Dataset` containing this table.
76 77 78 |
# File 'lib/gcloud/bigquery/view.rb', line 76 def dataset_id @gapi.table_reference.dataset_id end |
#delete ⇒ Boolean
Permanently deletes the table.
397 398 399 400 401 |
# File 'lib/gcloud/bigquery/view.rb', line 397 def delete ensure_service! service.delete_table dataset_id, table_id true end |
#description ⇒ Object
The description of the table.
178 179 180 181 |
# File 'lib/gcloud/bigquery/view.rb', line 178 def description ensure_full_data! @gapi.description end |
#description=(new_description) ⇒ Object
Updates the description of the table.
188 189 190 191 |
# File 'lib/gcloud/bigquery/view.rb', line 188 def description= new_description @gapi.update! description: new_description patch_gapi! :description end |
#etag ⇒ Object
A string hash of the dataset.
158 159 160 161 |
# File 'lib/gcloud/bigquery/view.rb', line 158 def etag ensure_full_data! @gapi.etag end |
#expires_at ⇒ Object
The time when this table expires. If not present, the table will persist indefinitely. Expired tables will be deleted and their storage reclaimed.
214 215 216 217 218 219 220 221 |
# File 'lib/gcloud/bigquery/view.rb', line 214 def expires_at ensure_full_data! begin Time.at(Integer(@gapi.expiration_time) / 1000.0) rescue nil end end |
#fields ⇒ Object
The fields of the view.
281 282 283 |
# File 'lib/gcloud/bigquery/view.rb', line 281 def fields schema.fields end |
#headers ⇒ Object
The names of the columns in the view.
290 291 292 |
# File 'lib/gcloud/bigquery/view.rb', line 290 def headers fields.map(&:name) end |
#id ⇒ Object
The combined Project ID, Dataset ID, and Table ID for this table, in the format specified by the [Query Reference](cloud.google.com/bigquery/query-reference#from): ‘project_name:datasetId.tableId`. To use this value in queries see #query_id.
108 109 110 |
# File 'lib/gcloud/bigquery/view.rb', line 108 def id @gapi.id end |
#location ⇒ Object
The geographic location where the table should reside. Possible values include EU and US. The default value is US.
261 262 263 264 |
# File 'lib/gcloud/bigquery/view.rb', line 261 def location ensure_full_data! @gapi.location end |
#modified_at ⇒ Object
The date when this table was last modified.
228 229 230 231 232 233 234 235 |
# File 'lib/gcloud/bigquery/view.rb', line 228 def modified_at ensure_full_data! begin Time.at(Integer(@gapi.last_modified_time) / 1000.0) rescue nil end end |
#name ⇒ Object
The name of the table.
139 140 141 |
# File 'lib/gcloud/bigquery/view.rb', line 139 def name @gapi.friendly_name end |
#name=(new_name) ⇒ Object
Updates the name of the table.
148 149 150 151 |
# File 'lib/gcloud/bigquery/view.rb', line 148 def name= new_name @gapi.update! friendly_name: new_name patch_gapi! :friendly_name end |
#project_id ⇒ Object
The ID of the ‘Project` containing this table.
85 86 87 |
# File 'lib/gcloud/bigquery/view.rb', line 85 def project_id @gapi.table_reference.project_id end |
#query ⇒ Object
The query that executes each time the view is loaded.
299 300 301 |
# File 'lib/gcloud/bigquery/view.rb', line 299 def query @gapi.view.query if @gapi.view end |
#query=(new_query) ⇒ Object
Updates the query that executes each time the view is loaded.
323 324 325 326 327 |
# File 'lib/gcloud/bigquery/view.rb', line 323 def query= new_query @gapi.view ||= Google::Apis::BigqueryV2::ViewDefinition.new @gapi.view.update! query: new_query patch_view_gapi! :query end |
#query_id ⇒ Object
The value returned by #id, wrapped in square brackets if the Project ID contains dashes, as specified by the [Query Reference](cloud.google.com/bigquery/query-reference#from). Useful in queries.
130 131 132 |
# File 'lib/gcloud/bigquery/view.rb', line 130 def query_id project_id["-"] ? "[#{id}]" : id end |
#reload! ⇒ Object Also known as: refresh!
Reloads the table with current data from the BigQuery service.
408 409 410 411 412 |
# File 'lib/gcloud/bigquery/view.rb', line 408 def reload! ensure_service! gapi = service.get_table dataset_id, table_id @gapi = gapi end |
#schema ⇒ Object
The schema of the view.
271 272 273 274 |
# File 'lib/gcloud/bigquery/view.rb', line 271 def schema ensure_full_data! Schema.from_gapi(@gapi.schema).freeze end |
#table? ⇒ Boolean
Checks if the table’s type is “TABLE”.
242 243 244 |
# File 'lib/gcloud/bigquery/view.rb', line 242 def table? @gapi.type == "TABLE" end |
#table_id ⇒ Object
A unique ID for this table. The ID must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_). The maximum length is 1,024 characters.
67 68 69 |
# File 'lib/gcloud/bigquery/view.rb', line 67 def table_id @gapi.table_reference.table_id end |
#table_ref ⇒ Object
The gapi fragment containing the Project ID, Dataset ID, and Table ID as a camel-cased hash.
93 94 95 96 97 |
# File 'lib/gcloud/bigquery/view.rb', line 93 def table_ref table_ref = @gapi.table_reference table_ref = table_ref.to_hash if table_ref.respond_to? :to_hash table_ref end |
#view? ⇒ Boolean
Checks if the table’s type is “VIEW”.
251 252 253 |
# File 'lib/gcloud/bigquery/view.rb', line 251 def view? @gapi.type == "VIEW" end |