Class: Gcloud::Bigquery::View

Inherits:
Object
  • Object
show all
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.

require "gcloud"

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeView

Create an empty Table object.



52
53
54
55
# File 'lib/gcloud/bigquery/view.rb', line 52

def initialize #:nodoc:
  @connection = nil
  @gapi = {}
end

Instance Attribute Details

#connectionObject

The Connection object.



44
45
46
# File 'lib/gcloud/bigquery/view.rb', line 44

def connection
  @connection
end

#gapiObject

The Google API Client object.



48
49
50
# File 'lib/gcloud/bigquery/view.rb', line 48

def gapi
  @gapi
end

Class Method Details

.from_gapi(gapi, conn) ⇒ Object

New Table from a Google API Client object.



396
397
398
399
400
401
# File 'lib/gcloud/bigquery/view.rb', line 396

def self.from_gapi gapi, conn #:nodoc:
  new.tap do |f|
    f.gapi = gapi
    f.connection = conn
  end
end

Instance Method Details

#api_urlObject

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

:category: Attributes



128
129
130
131
# File 'lib/gcloud/bigquery/view.rb', line 128

def api_url
  ensure_full_data!
  @gapi["selfLink"]
end

#created_atObject

The time when this table was created.

:category: Attributes



157
158
159
160
# File 'lib/gcloud/bigquery/view.rb', line 157

def created_at
  ensure_full_data!
  Time.at(@gapi["creationTime"] / 1000.0)
end

#data(max: nil, timeout: nil, cache: nil, dryrun: nil) ⇒ Object

Runs a query to retrieve all data from the view.

Parameters

max

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. (Integer)

timeout

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). (Integer)

cache

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. (Boolean)

dryrun

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. (Boolean)

Returns

Gcloud::Bigquery::QueryData

Example

require "gcloud"

gcloud = Gcloud.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?

:category: Data



336
337
338
339
340
341
342
343
344
345
346
# File 'lib/gcloud/bigquery/view.rb', line 336

def data max: nil, timeout: nil, cache: nil, dryrun: nil
  sql = "SELECT * FROM #{@gapi['id']}"
  ensure_connection!
  options = { max: max, timeout: timeout, cache: cache, dryrun: dryrun }
  resp = connection.query sql, options
  if resp.success?
    QueryData.from_gapi resp.data, connection
  else
    fail ApiError.from_response(resp)
  end
end

#dataset_idObject

The ID of the Dataset containing this table.

:category: Attributes



73
74
75
# File 'lib/gcloud/bigquery/view.rb', line 73

def dataset_id
  @gapi["tableReference"]["datasetId"]
end

#deleteObject

Permanently deletes the table.

Returns

true if the table was deleted.

Example

require "gcloud"

gcloud = Gcloud.new
bigquery = gcloud.bigquery
dataset = bigquery.dataset "my_dataset"
table = dataset.table "my_table"

table.delete

:category: Lifecycle



368
369
370
371
372
373
374
375
376
# File 'lib/gcloud/bigquery/view.rb', line 368

def delete
  ensure_connection!
  resp = connection.delete_table dataset_id, table_id
  if resp.success?
    true
  else
    fail ApiError.from_response(resp)
  end
end

#descriptionObject

The description of the table.

:category: Attributes



138
139
140
141
# File 'lib/gcloud/bigquery/view.rb', line 138

def description
  ensure_full_data!
  @gapi["description"]
end

#description=(new_description) ⇒ Object

Updates the description of the table.

:category: Lifecycle



148
149
150
# File 'lib/gcloud/bigquery/view.rb', line 148

def description= new_description
  patch_gapi! description: new_description
end

#etagObject

A string hash of the dataset.

:category: Attributes



118
119
120
121
# File 'lib/gcloud/bigquery/view.rb', line 118

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.

:category: Attributes



169
170
171
172
173
# File 'lib/gcloud/bigquery/view.rb', line 169

def expires_at
  ensure_full_data!
  return nil if @gapi["expirationTime"].nil?
  Time.at(@gapi["expirationTime"] / 1000.0)
end

#fieldsObject

The fields of the table.

:category: Attributes



232
233
234
235
236
237
# File 'lib/gcloud/bigquery/view.rb', line 232

def fields
  f = schema["fields"]
  f = f.to_hash if f.respond_to? :to_hash
  f = [] if f.nil?
  f
end

#headersObject

The names of the columns in the table.

:category: Attributes



244
245
246
# File 'lib/gcloud/bigquery/view.rb', line 244

def headers
  fields.map { |f| f["name"] }
end

#locationObject

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

:category: Attributes



209
210
211
212
# File 'lib/gcloud/bigquery/view.rb', line 209

def location
  ensure_full_data!
  @gapi["location"]
end

#modified_atObject

The date when this table was last modified.

:category: Attributes



180
181
182
183
# File 'lib/gcloud/bigquery/view.rb', line 180

def modified_at
  ensure_full_data!
  Time.at(@gapi["lastModifiedTime"] / 1000.0)
end

#nameObject

The name of the table.

:category: Attributes



100
101
102
# File 'lib/gcloud/bigquery/view.rb', line 100

def name
  @gapi["friendlyName"]
end

#name=(new_name) ⇒ Object

Updates the name of the table.

:category: Lifecycle



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

def name= new_name
  patch_gapi! name: new_name
end

#project_idObject

The ID of the Project containing this table.

:category: Attributes



82
83
84
# File 'lib/gcloud/bigquery/view.rb', line 82

def project_id
  @gapi["tableReference"]["projectId"]
end

#queryObject

The query that executes each time the view is loaded.

:category: Attributes



253
254
255
# File 'lib/gcloud/bigquery/view.rb', line 253

def query
  @gapi["view"]["query"] if @gapi["view"]
end

#query=(new_query) ⇒ Object

Updates the query that executes each time the view is loaded. See the BigQuery Query Reference .

Parameters

new_query

The query that defines the view. (String)

Example

require "gcloud"

gcloud = Gcloud.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]"

:category: Lifecycle



280
281
282
# File 'lib/gcloud/bigquery/view.rb', line 280

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.

:category: Lifecycle



383
384
385
386
387
388
389
390
391
# File 'lib/gcloud/bigquery/view.rb', line 383

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

#schemaObject

The schema of the table.

:category: Attributes



219
220
221
222
223
224
225
# File 'lib/gcloud/bigquery/view.rb', line 219

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”.

:category: Attributes

Returns:

  • (Boolean)


190
191
192
# File 'lib/gcloud/bigquery/view.rb', line 190

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.

:category: Attributes



64
65
66
# File 'lib/gcloud/bigquery/view.rb', line 64

def table_id
  @gapi["tableReference"]["tableId"]
end

#table_refObject

The gapi fragment containing the Project ID, Dataset ID, and Table ID as a camel-cased hash.



89
90
91
92
93
# File 'lib/gcloud/bigquery/view.rb', line 89

def table_ref #:nodoc:
  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”.

:category: Attributes

Returns:

  • (Boolean)


199
200
201
# File 'lib/gcloud/bigquery/view.rb', line 199

def view?
  @gapi["type"] == "VIEW"
end