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.
-
#etag ⇒ Object
A string hash of the dataset.
-
#expires_at ⇒ Object
The time when this table expires.
-
#fields ⇒ Object
The fields of the table.
-
#headers ⇒ Object
The names of the columns in the table.
-
#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.
-
#project_id ⇒ Object
The ID of the ‘Project` containing this table.
-
#query ⇒ Object
The query that executes each time the view is loaded.
-
#schema ⇒ Object
The schema of the table.
-
#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.
-
#description=(new_description) ⇒ Object
Updates the description of the table.
-
#name=(new_name) ⇒ Object
Updates the name of 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: nil, cache: nil, 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.
53 54 55 56 |
# File 'lib/gcloud/bigquery/view.rb', line 53 def initialize @connection = nil @gapi = {} end |
Instance Attribute Details
#connection ⇒ Object
45 46 47 |
# File 'lib/gcloud/bigquery/view.rb', line 45 def connection @connection end |
#gapi ⇒ Object
49 50 51 |
# File 'lib/gcloud/bigquery/view.rb', line 49 def gapi @gapi end |
Class Method Details
.from_gapi(gapi, conn) ⇒ Object
383 384 385 386 387 388 |
# File 'lib/gcloud/bigquery/view.rb', line 383 def self.from_gapi gapi, conn new.tap do |f| f.gapi = gapi f.connection = conn end end |
Instance Method Details
#api_url ⇒ Object
A URL that can be used to access the dataset using the REST API.
130 131 132 133 |
# File 'lib/gcloud/bigquery/view.rb', line 130 def api_url ensure_full_data! @gapi["selfLink"] end |
#created_at ⇒ Object
The time when this table was created.
159 160 161 162 |
# File 'lib/gcloud/bigquery/view.rb', line 159 def created_at ensure_full_data! Time.at(@gapi["creationTime"] / 1000.0) end |
#data(max: nil, timeout: nil, cache: nil, dryrun: nil) ⇒ Gcloud::Bigquery::QueryData
Runs a query to retrieve all data from the view.
326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/gcloud/bigquery/view.rb', line 326 def data max: nil, timeout: nil, cache: nil, dryrun: nil sql = "SELECT * FROM #{@gapi['id']}" ensure_connection! = { max: max, timeout: timeout, cache: cache, dryrun: dryrun } resp = connection.query sql, if resp.success? QueryData.from_gapi resp.data, connection else fail ApiError.from_response(resp) end end |
#dataset_id ⇒ Object
The ID of the ‘Dataset` containing this table.
74 75 76 |
# File 'lib/gcloud/bigquery/view.rb', line 74 def dataset_id @gapi["tableReference"]["datasetId"] end |
#delete ⇒ Boolean
Permanently deletes the table.
355 356 357 358 359 360 361 362 363 |
# File 'lib/gcloud/bigquery/view.rb', line 355 def delete ensure_connection! resp = connection.delete_table dataset_id, table_id if resp.success? true else fail ApiError.from_response(resp) end end |
#description ⇒ Object
The description of the table.
140 141 142 143 |
# File 'lib/gcloud/bigquery/view.rb', line 140 def description ensure_full_data! @gapi["description"] end |
#description=(new_description) ⇒ Object
Updates the description of the table.
150 151 152 |
# File 'lib/gcloud/bigquery/view.rb', line 150 def description= new_description patch_gapi! description: new_description end |
#etag ⇒ Object
A string hash of the dataset.
120 121 122 123 |
# File 'lib/gcloud/bigquery/view.rb', line 120 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.
171 172 173 174 175 |
# File 'lib/gcloud/bigquery/view.rb', line 171 def expires_at ensure_full_data! return nil if @gapi["expirationTime"].nil? Time.at(@gapi["expirationTime"] / 1000.0) end |
#fields ⇒ Object
The fields of the table.
234 235 236 237 238 239 |
# File 'lib/gcloud/bigquery/view.rb', line 234 def fields f = schema["fields"] f = f.to_hash if f.respond_to? :to_hash f = [] if f.nil? f end |
#headers ⇒ Object
The names of the columns in the table.
246 247 248 |
# File 'lib/gcloud/bigquery/view.rb', line 246 def headers fields.map { |f| f["name"] } end |
#location ⇒ Object
The geographic location where the table should reside. Possible values include EU and US. The default value is US.
211 212 213 214 |
# File 'lib/gcloud/bigquery/view.rb', line 211 def location ensure_full_data! @gapi["location"] end |
#modified_at ⇒ Object
The date when this table was last modified.
182 183 184 185 |
# File 'lib/gcloud/bigquery/view.rb', line 182 def modified_at ensure_full_data! Time.at(@gapi["lastModifiedTime"] / 1000.0) end |
#name ⇒ Object
The name of the table.
102 103 104 |
# File 'lib/gcloud/bigquery/view.rb', line 102 def name @gapi["friendlyName"] end |
#name=(new_name) ⇒ Object
Updates the name of the table.
111 112 113 |
# File 'lib/gcloud/bigquery/view.rb', line 111 def name= new_name patch_gapi! name: new_name end |
#project_id ⇒ Object
The ID of the ‘Project` containing this table.
83 84 85 |
# File 'lib/gcloud/bigquery/view.rb', line 83 def project_id @gapi["tableReference"]["projectId"] end |
#query ⇒ Object
The query that executes each time the view is loaded.
255 256 257 |
# File 'lib/gcloud/bigquery/view.rb', line 255 def query @gapi["view"]["query"] if @gapi["view"] end |
#query=(new_query) ⇒ Object
Updates the query that executes each time the view is loaded.
279 280 281 |
# File 'lib/gcloud/bigquery/view.rb', line 279 def query= new_query patch_gapi! query: new_query end |
#reload! ⇒ Object Also known as: refresh!
Reloads the table with current data from the BigQuery service.
370 371 372 373 374 375 376 377 378 |
# File 'lib/gcloud/bigquery/view.rb', line 370 def reload! ensure_connection! resp = connection.get_table dataset_id, table_id if resp.success? @gapi = resp.data else fail ApiError.from_response(resp) end end |
#schema ⇒ Object
The schema of the table.
221 222 223 224 225 226 227 |
# File 'lib/gcloud/bigquery/view.rb', line 221 def schema ensure_full_data! s = @gapi["schema"] s = s.to_hash if s.respond_to? :to_hash s = {} if s.nil? s end |
#table? ⇒ Boolean
Checks if the table’s type is “TABLE”.
192 193 194 |
# File 'lib/gcloud/bigquery/view.rb', line 192 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.
65 66 67 |
# File 'lib/gcloud/bigquery/view.rb', line 65 def table_id @gapi["tableReference"]["tableId"] end |
#table_ref ⇒ Object
The gapi fragment containing the Project ID, Dataset ID, and Table ID as a camel-cased hash.
91 92 93 94 95 |
# File 'lib/gcloud/bigquery/view.rb', line 91 def table_ref table_ref = @gapi["tableReference"] 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”.
201 202 203 |
# File 'lib/gcloud/bigquery/view.rb', line 201 def view? @gapi["type"] == "VIEW" end |