class Google::Cloud::Bigquery::Model


model = dataset.model “my_model”
dataset = bigquery.dataset “my_dataset”
bigquery = Google::Cloud::Bigquery.new
require “google/cloud/bigquery”
@example
Getting model metadata
@see cloud.google.com/bigquery-ml/docs/getting-model-metadata<br>Introduction to BigQuery ML
@see cloud.google.com/bigquery-ml/docs/bigqueryml-intro<br><br>datasets for training and for prediction.
In BigQuery ML, a model can be used with data from multiple BigQuery
for training or evaluation.
technique, so model training does not require labels nor split data
identifying customer segments. K-means is an unsupervised learning
* K-means clustering for data segmentation (beta); for example,
uses a multinomial classifier with a cross entropy loss function.
unique values. In BigQuery ML, multiclass logistic regression training
“low-value,” “medium-value,” or “high-value.” Labels can have up to 50
used to predict multiple possible values such as whether an input is
* Multiclass logistic regression for classification. These models can be
have two possible values.
determining whether a customer will make a purchase. Labels must only
* Binary logistic regression for classification; for example,
NaN).
on a given day. Labels are real-valued (they cannot be +/- infinity or
* Linear regression for forecasting; for example, the sales of an item
The following types of models are supported by BigQuery ML:
training data.
A model in BigQuery ML represents what an ML system has learned from the
# Model
#

def self.from_gapi_json gapi_json, service

Other tags:
    Private: - New Model from a Google API Client object.
def self.from_gapi_json gapi_json, service
  new.tap do |m|
    m.instance_variable_set :@gapi_json, gapi_json
    m.instance_variable_set :@service, service
  end
end

def self.new_reference project_id, dataset_id, model_id, service

Other tags:
    Private: - New lazy Model object without making an HTTP request, for use with the skip_lookup option.
def self.new_reference project_id, dataset_id, model_id, service
  raise ArgumentError, "project_id is required" unless project_id
  raise ArgumentError, "dataset_id is required" unless dataset_id
  raise ArgumentError, "model_id is required" unless model_id
  raise ArgumentError, "service is required" unless service
  new.tap do |m|
    reference_gapi_json = Google::Apis::BigqueryV2::ModelReference.new(
      project_id: project_id,
      dataset_id: dataset_id,
      model_id:   model_id
    )
    m.instance_variable_set :@reference, reference_gapi_json
    m.instance_variable_set :@service, service
  end
end

def created_at

Returns:
  • (Time, nil) - The creation time, or `nil` if the object is a
def created_at
  return nil if reference?
  Convert.millis_to_time @gapi_json[:creationTime]
end

def dataset_id

Returns:
  • (String) - The ID must contain only letters (`[A-Za-z]`), numbers
def dataset_id
  return @reference.dataset_id if reference?
  @gapi_json[:modelReference][:datasetId]
end

def delete

Returns:
  • (Boolean) - Returns `true` if the model was deleted.
def delete
  ensure_service!
  service.delete_model dataset_id, model_id
  # Set flag for #exists?
  @exists = false
  true
end

def description

Returns:
  • (String, nil) - The description, or `nil` if the object is a
def description
  return nil if reference?
  ensure_full_data!
  @gapi_json[:description]
end

def description= new_description

Parameters:
  • new_description (String) -- The new user-friendly description.
def description= new_description
  ensure_full_data!
  patch_gapi! description: new_description
end

def encryption

Returns:
  • (EncryptionConfiguration, nil) - The encryption configuration.

Other tags:
    See: https://cloud.google.com/bigquery/docs/customer-managed-encryption -
def encryption
  return nil if reference?
  return nil if @gapi_json[:encryptionConfiguration].nil?
  # We have to create a gapic object from the hash because that is what
  # EncryptionConfiguration is expecing.
  json_cmek = @gapi_json[:encryptionConfiguration].to_json
  gapi_cmek = Google::Apis::BigqueryV2::EncryptionConfiguration.from_json json_cmek
  EncryptionConfiguration.from_gapi(gapi_cmek).freeze
end

def encryption= value

Parameters:
  • value (EncryptionConfiguration) -- The new encryption config.

Other tags:
    See: https://cloud.google.com/bigquery/docs/customer-managed-encryption -
def encryption= value
  ensure_full_data!
  # We have to create a hash from the gapic object's JSON because that
  # is what Model is expecing.
  json_cmek = JSON.parse value.to_gapi.to_json, symbolize_names: true
  patch_gapi! encryptionConfiguration: json_cmek
end

def ensure_full_data!

only partially loaded by a request to the API list method.
Load the complete representation of the model if it has been
#
def ensure_full_data!
  reload! unless resource_full?
end

def ensure_gapi_json!

from the service.
Ensures the Google::Apis::BigqueryV2::Model object has been loaded
#
def ensure_gapi_json!
  ensure_service!
  return unless reference?
  reload!
end

def ensure_job_succeeded! job

def ensure_job_succeeded! job
  return unless job.failed?
  begin
    # raise to activate ruby exception cause handling
    raise job.gapi_error
  rescue StandardError => e
    # wrap Google::Apis::Error with Google::Cloud::Error
    raise Google::Cloud::Error.from_error(e)
  end
end

def ensure_service!

Raise an error unless an active service is available.
#
def ensure_service!
  raise "Must have active connection" unless service
end

def etag

Returns:
  • (String, nil) - The ETag hash, or `nil` if the object is a
def etag
  return nil if reference?
  ensure_full_data!
  @gapi_json[:etag]
end

def exists? force: false

Returns:
  • (Boolean) - `true` when the model exists in the BigQuery

Parameters:
  • force (Boolean) -- Force the latest resource representation to be
def exists? force: false
  return resource_exists? if force
  # If we have a value, return it
  return @exists unless @exists.nil?
  # Always true if we have a gapi_json object
  return true if resource?
  resource_exists?
end

def expires_at

Returns:
  • (Time, nil) - The expiration time, or `nil` if not present or
def expires_at
  return nil if reference?
  ensure_full_data!
  Convert.millis_to_time @gapi_json[:expirationTime]
end

def expires_at= new_expires_at

Parameters:
  • new_expires_at (Integer) -- The new time when this model expires.
def expires_at= new_expires_at
  ensure_full_data!
  new_expires_millis = Convert.time_to_millis new_expires_at
  patch_gapi! expirationTime: new_expires_millis
end

def extract extract_url, format: nil, reservation: nil, &block

Returns:
  • (Boolean) - Returns `true` if the extract operation succeeded.

Other tags:
    Yieldparam: job - a job

Other tags:
    Yield: - a job configuration object

Parameters:
  • reservation (String) -- The reservation that job would use. User
  • format (String) -- The exported file format. The default value is
  • extract_url (String) -- The Google Storage URI to which BigQuery

Other tags:
    See: https://cloud.google.com/bigquery-ml/docs/exporting-models -
def extract extract_url, format: nil, reservation: nil, &block
  job = extract_job extract_url, format: format, reservation: reservation, &block
  job.wait_until_done!
  ensure_job_succeeded! job
  true
end

def extract_job extract_url, format: nil, job_id: nil, prefix: nil, labels: nil, reservation: nil

Returns:
  • (Google::Cloud::Bigquery::ExtractJob) -

Other tags:
    Yieldparam: job - a job

Other tags:
    Yield: - a job configuration object

Parameters:
  • reservation (String) -- The reservation that job would use. User
  • labels (Hash) -- A hash of user-provided labels associated with
  • prefix (String) -- A string, usually human-readable, that will be
  • job_id (String) -- A user-defined ID for the extract job. The ID
  • format (String) -- The exported file format. The default value is
  • extract_url (String) -- The Google Storage URI to which BigQuery

Other tags:
    See: https://cloud.google.com/bigquery-ml/docs/exporting-models -
def extract_job extract_url, format: nil, job_id: nil, prefix: nil, labels: nil, reservation: nil
  ensure_service!
  options = { format: format, job_id: job_id, prefix: prefix, labels: labels, reservation: reservation }
  updater = ExtractJob::Updater.from_options service, model_ref, extract_url, options
  updater.location = location if location # may be model reference
  yield updater if block_given?
  job_gapi = updater.to_gapi
  gapi = service.extract_table job_gapi
  Job.from_gapi gapi, service
end

def feature_columns

Returns:
  • (Array) -
def feature_columns
  ensure_full_data!
  Array(@gapi_json[:featureColumns]).map do |field_gapi_json|
    field_gapi = Google::Apis::BigqueryV2::StandardSqlField.from_json field_gapi_json.to_json
    StandardSql::Field.from_gapi field_gapi
  end
end

def initialize

Other tags:
    Private: - Create an empty Model object.
def initialize
  @service = nil
  @gapi_json = nil
  @reference = nil
end

def label_columns

Returns:
  • (Array) -
def label_columns
  ensure_full_data!
  Array(@gapi_json[:labelColumns]).map do |field_gapi_json|
    field_gapi = Google::Apis::BigqueryV2::StandardSqlField.from_json field_gapi_json.to_json
    StandardSql::Field.from_gapi field_gapi
  end
end

def labels

Returns:
  • (Hash, nil) - A hash containing key/value pairs.
def labels
  return nil if reference?
  m = @gapi_json[:labels]
  m = m.to_h if m.respond_to? :to_h
  m.dup.freeze
end

def labels= new_labels

Parameters:
  • new_labels (Hash) -- A hash containing key/value
def labels= new_labels
  ensure_full_data!
  patch_gapi! labels: new_labels
end

def location

Returns:
  • (String, nil) - The location code.
def location
  return nil if reference?
  ensure_full_data!
  @gapi_json[:location]
end

def model_id

Returns:
  • (String) - The ID must contain only letters (`[A-Za-z]`), numbers
def model_id
  return @reference.model_id if reference?
  @gapi_json[:modelReference][:modelId]
end

def model_ref

Returns:
  • (Google::Apis::BigqueryV2::ModelReference) -

Other tags:
    Private: - The gapi_json fragment containing the Project ID, Dataset ID,
def model_ref
  return @reference if reference?
  Google::Apis::BigqueryV2::ModelReference.new(
    project_id: project_id,
    dataset_id: dataset_id,
    model_id:   model_id
  )
end

def model_type

Returns:
  • (String, nil) - The model type, or `nil` if the object is a
def model_type
  return nil if reference?
  @gapi_json[:modelType]
end

def modified_at

Returns:
  • (Time, nil) - The last modified time, or `nil` if not present or
def modified_at
  return nil if reference?
  Convert.millis_to_time @gapi_json[:lastModifiedTime]
end

def name

Returns:
  • (String, nil) - The friendly name, or `nil` if the object is a
def name
  return nil if reference?
  ensure_full_data!
  @gapi_json[:friendlyName]
end

def name= new_name

Parameters:
  • new_name (String) -- The new friendly name.
def name= new_name
  ensure_full_data!
  patch_gapi! friendlyName: new_name
end

def patch_gapi! **changes

def patch_gapi! **changes
  return if changes.empty?
  ensure_service!
  patch_gapi = Google::Apis::BigqueryV2::Model.from_json changes.to_json
  patch_gapi.model_reference = model_ref
  @gapi_json = service.patch_model \
    dataset_id, model_id, patch_gapi, etag
  @reference = nil
  # TODO: restore original impl after acceptance test indicates that
  # service etag bug is fixed
  reload!
end

def project_id

Returns:
  • (String) - The project ID.
def project_id
  return @reference.project_id if reference?
  @gapi_json[:modelReference][:projectId]
end

def reference?

Returns:
  • (Boolean) - `true` when the model is just a local reference
def reference?
  @gapi_json.nil?
end

def reload!

Other tags:
    Example: Skip retrieving the model from the service, then load it: -

Returns:
  • (Google::Cloud::Bigquery::Model) - Returns the reloaded
def reload!
  ensure_service!
  @gapi_json = service.get_model dataset_id, model_id
  @reference = nil
  @exists = nil
  self
end

def resource?

Returns:
  • (Boolean) - `true` when the model was created with a resource
def resource?
  !@gapi_json.nil?
end

def resource_exists?

Fetch gapi_json and memoize whether resource exists.
#
def resource_exists?
  reload!
  @exists = true
rescue Google::Cloud::NotFoundError
  @exists = false
end

def resource_full?

Returns:
  • (Boolean) - `true` when the model was created with a full
def resource_full?
  resource? && @gapi_json.key?(:friendlyName)
end

def resource_partial?

Returns:
  • (Boolean) - `true` when the model was created with a partial
def resource_partial?
  resource? && !resource_full?
end

def training_runs

Returns:
  • (Array) -
def training_runs
  ensure_full_data!
  Array @gapi_json[:trainingRuns]
end