Class: Google::Cloud::Bigquery::View

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/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.

Examples:

require "google/cloud"

gcloud = Google::Cloud.new
bigquery = gcloud.bigquery
dataset = bigquery.dataset "my_dataset"
view = dataset.create_view "my_view",
         "SELECT name, age FROM [proj:dataset.users]"

Attributes collapse

Lifecycle collapse

Data collapse

Instance Method Details

#api_urlObject

A URL that can be used to access the dataset using the REST API.



169
170
171
172
# File 'lib/google/cloud/bigquery/view.rb', line 169

def api_url
  ensure_full_data!
  @gapi.self_link
end

#created_atObject

The time when this table was created.



199
200
201
202
203
204
205
206
# File 'lib/google/cloud/bigquery/view.rb', line 199

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) ⇒ Google::Cloud::Bigquery::QueryData

Runs a query to retrieve all data from the view.

Examples:

require "google/cloud"

gcloud = Google::Cloud.new
bigquery = gcloud.bigquery
dataset = bigquery.dataset "my_dataset"
view = dataset.table "my_view"

data = view.data
data.each do |row|
  puts row["first_name"]
end
more_data = data.next if data.next?

Parameters:

  • max (Integer)

    The maximum number of rows of data to return per page of results. Setting this flag to a small value such as 1000 and then paging through results might improve reliability when the query result set is large. In addition to this limit, responses are also limited to 10 MB. By default, there is no maximum row count, and only the byte limit applies.

  • timeout (Integer)

    How long to wait for the query to complete, in milliseconds, before the request times out and returns. Note that this is only a timeout for the request, not the query. If the query takes longer to run than the timeout value, the call returns without any results and with QueryData#complete? set to false. The default value is 10000 milliseconds (10 seconds).

  • cache (Boolean)

    Whether to look for the result in the query cache. The query cache is a best-effort cache that will be flushed whenever tables in the query are modified. The default value is true. For more information, see query caching.

  • dryrun (Boolean)

    If set to true, BigQuery doesn't run the job. Instead, if the query is valid, BigQuery returns statistics about the job such as how many bytes would be processed. If the query is invalid, an error returns. The default value is false.

Returns:



374
375
376
377
378
379
380
# File 'lib/google/cloud/bigquery/view.rb', line 374

def data max: nil, timeout: 10000, cache: true, dryrun: nil
  sql = "SELECT * FROM #{query_id}"
  ensure_service!
  options = { max: max, timeout: timeout, cache: cache, dryrun: dryrun }
  gapi = service.query sql, options
  QueryData.from_gapi gapi, service
end

#dataset_idObject

The ID of the Dataset containing this table.



78
79
80
# File 'lib/google/cloud/bigquery/view.rb', line 78

def dataset_id
  @gapi.table_reference.dataset_id
end

#deleteBoolean

Permanently deletes the table.

Examples:

require "google/cloud"

gcloud = Google::Cloud.new
bigquery = gcloud.bigquery
dataset = bigquery.dataset "my_dataset"
table = dataset.table "my_table"

table.delete

Returns:

  • (Boolean)

    Returns true if the table was deleted.



399
400
401
402
403
# File 'lib/google/cloud/bigquery/view.rb', line 399

def delete
  ensure_service!
  service.delete_table dataset_id, table_id
  true
end

#descriptionObject

The description of the table.



179
180
181
182
# File 'lib/google/cloud/bigquery/view.rb', line 179

def description
  ensure_full_data!
  @gapi.description
end

#description=(new_description) ⇒ Object

Updates the description of the table.



189
190
191
192
# File 'lib/google/cloud/bigquery/view.rb', line 189

def description= new_description
  @gapi.update! description: new_description
  patch_gapi! :description
end

#etagObject

A string hash of the dataset.



159
160
161
162
# File 'lib/google/cloud/bigquery/view.rb', line 159

def etag
  ensure_full_data!
  @gapi.etag
end

#expires_atObject

The time when this table expires. If not present, the table will persist indefinitely. Expired tables will be deleted and their storage reclaimed.



215
216
217
218
219
220
221
222
# File 'lib/google/cloud/bigquery/view.rb', line 215

def expires_at
  ensure_full_data!
  begin
    Time.at(Integer(@gapi.expiration_time) / 1000.0)
  rescue
    nil
  end
end

#fieldsObject

The fields of the view.



282
283
284
# File 'lib/google/cloud/bigquery/view.rb', line 282

def fields
  schema.fields
end

#headersObject

The names of the columns in the view.



291
292
293
# File 'lib/google/cloud/bigquery/view.rb', line 291

def headers
  fields.map(&:name)
end

#idObject

The combined Project ID, Dataset ID, and Table ID for this table, in the format specified by the Query Reference: project_name:datasetId.tableId. To use this value in queries see #query_id.



109
110
111
# File 'lib/google/cloud/bigquery/view.rb', line 109

def id
  @gapi.id
end

#locationObject

The geographic location where the table should reside. Possible values include EU and US. The default value is US.



262
263
264
265
# File 'lib/google/cloud/bigquery/view.rb', line 262

def location
  ensure_full_data!
  @gapi.location
end

#modified_atObject

The date when this table was last modified.



229
230
231
232
233
234
235
236
# File 'lib/google/cloud/bigquery/view.rb', line 229

def modified_at
  ensure_full_data!
  begin
    Time.at(Integer(@gapi.last_modified_time) / 1000.0)
  rescue
    nil
  end
end

#nameObject

The name of the table.



140
141
142
# File 'lib/google/cloud/bigquery/view.rb', line 140

def name
  @gapi.friendly_name
end

#name=(new_name) ⇒ Object

Updates the name of the table.



149
150
151
152
# File 'lib/google/cloud/bigquery/view.rb', line 149

def name= new_name
  @gapi.update! friendly_name: new_name
  patch_gapi! :friendly_name
end

#project_idObject

The ID of the Project containing this table.



87
88
89
# File 'lib/google/cloud/bigquery/view.rb', line 87

def project_id
  @gapi.table_reference.project_id
end

#queryObject

The query that executes each time the view is loaded.



300
301
302
# File 'lib/google/cloud/bigquery/view.rb', line 300

def query
  @gapi.view.query if @gapi.view
end

#query=(new_query) ⇒ Object

Updates the query that executes each time the view is loaded.

Examples:

require "google/cloud"

gcloud = Google::Cloud.new
bigquery = gcloud.bigquery
dataset = bigquery.dataset "my_dataset"
view = dataset.table "my_view"

view.query = "SELECT first_name FROM " \
             "[my_project:my_dataset.my_table]"

Parameters:

  • new_query (String)

    The query that defines the view.

See Also:



325
326
327
328
329
# File 'lib/google/cloud/bigquery/view.rb', line 325

def query= new_query
  @gapi.view ||= Google::Apis::BigqueryV2::ViewDefinition.new
  @gapi.view.update! query: new_query
  patch_view_gapi! :query
end

#query_idObject

The value returned by #id, wrapped in square brackets if the Project ID contains dashes, as specified by the Query Reference. Useful in queries.

Examples:

require "google/cloud"

gcloud = Google::Cloud.new
bigquery = gcloud.bigquery
dataset = bigquery.dataset "my_dataset"
table = dataset.table "my_table"

data = bigquery.query "SELECT name FROM #{table.query_id}"


131
132
133
# File 'lib/google/cloud/bigquery/view.rb', line 131

def query_id
  project_id["-"] ? "[#{id}]" : id
end

#reload!Object Also known as: refresh!

Reloads the table with current data from the BigQuery service.



410
411
412
413
414
# File 'lib/google/cloud/bigquery/view.rb', line 410

def reload!
  ensure_service!
  gapi = service.get_table dataset_id, table_id
  @gapi = gapi
end

#schemaObject

The schema of the view.



272
273
274
275
# File 'lib/google/cloud/bigquery/view.rb', line 272

def schema
  ensure_full_data!
  Schema.from_gapi(@gapi.schema).freeze
end

#table?Boolean

Checks if the table's type is "TABLE".

Returns:

  • (Boolean)


243
244
245
# File 'lib/google/cloud/bigquery/view.rb', line 243

def table?
  @gapi.type == "TABLE"
end

#table_idObject

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.



69
70
71
# File 'lib/google/cloud/bigquery/view.rb', line 69

def table_id
  @gapi.table_reference.table_id
end

#view?Boolean

Checks if the table's type is "VIEW".

Returns:

  • (Boolean)


252
253
254
# File 'lib/google/cloud/bigquery/view.rb', line 252

def view?
  @gapi.type == "VIEW"
end