Class: Google::Cloud::Bigquery::Table::Updater

Inherits:
Google::Cloud::Bigquery::Table show all
Defined in:
lib/google/cloud/bigquery/table.rb

Overview

Yielded to a block to accumulate changes for a patch request.

Lifecycle collapse

Lifecycle collapse

Schema collapse

Methods inherited from Google::Cloud::Bigquery::Table

#api_url, #buffer_bytes, #buffer_oldest_at, #buffer_rows, #bytes_count, #copy, #copy_job, #created_at, #data, #dataset_id, #delete, #description, #description=, #encryption, #encryption=, #etag, #exists?, #expires_at, #external, #external=, #external?, #extract, #extract_job, #fields, #headers, #id, #insert, #insert_async, #labels, #labels=, #load, #load_job, #location, #modified_at, #name, #name=, #project_id, #query, #query=, #query_id, #query_legacy_sql?, #query_standard_sql?, #query_udfs, #reference?, #reload!, #resource?, #resource_full?, #resource_partial?, #rows_count, #set_query, #table?, #table_id, #time_partitioning?, #time_partitioning_expiration, #time_partitioning_expiration=, #time_partitioning_field, #time_partitioning_field=, #time_partitioning_type, #time_partitioning_type=, #view?

Constructor Details

#initialize(gapi) ⇒ Updater

Create an Updater object.



2442
2443
2444
2445
2446
# File 'lib/google/cloud/bigquery/table.rb', line 2442

def initialize gapi
  @updates = []
  @gapi = gapi
  @schema = nil
end

Instance Attribute Details

#updatesObject (readonly)

A list of attributes that were updated.



2438
2439
2440
# File 'lib/google/cloud/bigquery/table.rb', line 2438

def updates
  @updates
end

Instance Method Details

#boolean(name, description: nil, mode: :nullable) ⇒ Object

Adds a boolean field to the schema.

See Schema#boolean.

Examples:

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
dataset = bigquery.dataset "my_dataset"
table = dataset.create_table "my_table" do |schema|
  schema.boolean "active", mode: :required
end

Parameters:

  • name (String)

    The field name. The name must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String)

    A description of the field.

  • mode (Symbol)

    The field's mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable.



2621
2622
2623
# File 'lib/google/cloud/bigquery/table.rb', line 2621

def boolean name, description: nil, mode: :nullable
  schema.boolean name, description: description, mode: mode
end

#bytes(name, description: nil, mode: :nullable) ⇒ Object

Adds a bytes field to the schema.

See Schema#bytes.

Examples:

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
dataset = bigquery.dataset "my_dataset"
table = dataset.create_table "my_table" do |schema|
  schema.bytes "avatar", mode: :required
end

Parameters:

  • name (String)

    The field name. The name must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String)

    A description of the field.

  • mode (Symbol)

    The field's mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable.



2649
2650
2651
# File 'lib/google/cloud/bigquery/table.rb', line 2649

def bytes name, description: nil, mode: :nullable
  schema.bytes name, description: description, mode: mode
end

#check_for_mutated_schema!Object

Make sure any access changes are saved



2805
2806
2807
2808
2809
2810
# File 'lib/google/cloud/bigquery/table.rb', line 2805

def check_for_mutated_schema!
  return if @schema.nil?
  return unless @schema.changed?
  @gapi.schema = @schema.to_gapi
  patch_gapi! :schema
end

#date(name, description: nil, mode: :nullable) ⇒ Object

Adds a date field to the schema.

See Schema#date.

Examples:

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
dataset = bigquery.dataset "my_dataset"
table = dataset.create_table "my_table" do |schema|
  schema.date "birthday", mode: :required
end

Parameters:

  • name (String)

    The field name. The name must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String)

    A description of the field.

  • mode (Symbol)

    The field's mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable.



2761
2762
2763
# File 'lib/google/cloud/bigquery/table.rb', line 2761

def date name, description: nil, mode: :nullable
  schema.date name, description: description, mode: mode
end

#datetime(name, description: nil, mode: :nullable) ⇒ Object

Adds a datetime field to the schema.

See Schema#datetime.

Examples:

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
dataset = bigquery.dataset "my_dataset"
table = dataset.create_table "my_table" do |schema|
  schema.datetime "target_end", mode: :required
end

Parameters:

  • name (String)

    The field name. The name must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String)

    A description of the field.

  • mode (Symbol)

    The field's mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable.



2733
2734
2735
# File 'lib/google/cloud/bigquery/table.rb', line 2733

def datetime name, description: nil, mode: :nullable
  schema.datetime name, description: description, mode: mode
end

#float(name, description: nil, mode: :nullable) ⇒ Object

Adds a floating-point number field to the schema.

See Schema#float.

Examples:

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
dataset = bigquery.dataset "my_dataset"
table = dataset.create_table "my_table" do |schema|
  schema.float "price", mode: :required
end

Parameters:

  • name (String)

    The field name. The name must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String)

    A description of the field.

  • mode (Symbol)

    The field's mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable.



2593
2594
2595
# File 'lib/google/cloud/bigquery/table.rb', line 2593

def float name, description: nil, mode: :nullable
  schema.float name, description: description, mode: mode
end

#integer(name, description: nil, mode: :nullable) ⇒ Object

Adds an integer field to the schema.

See Schema#integer.

Examples:

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
dataset = bigquery.dataset "my_dataset"
table = dataset.create_table "my_table" do |schema|
  schema.integer "age", mode: :required
end

Parameters:

  • name (String)

    The field name. The name must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String)

    A description of the field.

  • mode (Symbol)

    The field's mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable.



2565
2566
2567
# File 'lib/google/cloud/bigquery/table.rb', line 2565

def integer name, description: nil, mode: :nullable
  schema.integer name, description: description, mode: mode
end

#record(name, description: nil, mode: nil) {|nested_schema| ... } ⇒ Object

Adds a record field to the schema. A block must be passed describing the nested fields of the record. For more information about nested and repeated records, see Preparing Data for BigQuery .

See Schema#record.

Examples:

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
dataset = bigquery.dataset "my_dataset"
table = dataset.create_table "my_table" do |schema|
  schema.record "cities_lived", mode: :repeated do |cities_lived|
    cities_lived.string "place", mode: :required
    cities_lived.integer "number_of_years", mode: :required
  end
end

Parameters:

  • name (String)

    The field name. The name must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String)

    A description of the field.

  • mode (Symbol)

    The field's mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable.

Yields:

  • (nested_schema)

    a block for setting the nested schema

Yield Parameters:

  • nested_schema (Schema)

    the object accepting the nested schema



2799
2800
2801
# File 'lib/google/cloud/bigquery/table.rb', line 2799

def record name, description: nil, mode: nil, &block
  schema.record name, description: description, mode: mode, &block
end

#schema(replace: false) {|schema| ... } ⇒ Google::Cloud::Bigquery::Schema

Returns the table's schema. This method can also be used to set, replace, or add to the schema by passing a block. See Schema for available methods.

Examples:

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
dataset = bigquery.dataset "my_dataset"
table = dataset.create_table "my_table" do |t|
  t.name = "My Table",
  t.description = "A description of my table."
  t.schema do |s|
    s.string "first_name", mode: :required
    s.record "cities_lived", mode: :repeated do |r|
      r.string "place", mode: :required
      r.integer "number_of_years", mode: :required
    end
  end
end

Load the schema from a file

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
dataset = bigquery.dataset "my_dataset"
table = dataset.create_table "my_table" do |t|
  t.name = "My Table",
  t.description = "A description of my table."
  t.schema do |s|
    s.load File.open("schema.json")
  end
end

Parameters:

  • replace (Boolean)

    Whether to replace the existing schema with the new schema. If true, the fields will replace the existing schema. If false, the fields will be added to the existing schema. When a table already contains data, schema changes must be additive. Thus, the default value is false. When loading from a file this will always replace the schema, no matter what replace is set to. You can update the schema (for example, for a table that already contains data) by providing a schema file that includes the existing schema plus any new fields.

Yields:

  • (schema)

    a block for setting the schema

Yield Parameters:

  • schema (Schema)

    the object accepting the schema

Returns:



2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
# File 'lib/google/cloud/bigquery/table.rb', line 2500

def schema replace: false
  # Same as Table#schema, but not frozen
  # TODO: make sure to call ensure_full_data! on Dataset#update
  @schema ||= Schema.from_gapi @gapi.schema
  if block_given?
    @schema = Schema.from_gapi if replace
    yield @schema
    check_for_mutated_schema!
  end
  # Do not freeze on updater, allow modifications
  @schema
end

#string(name, description: nil, mode: :nullable) ⇒ Object

Adds a string field to the schema.

See Schema#string.

Examples:

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
dataset = bigquery.dataset "my_dataset"
table = dataset.create_table "my_table" do |schema|
  schema.string "first_name", mode: :required
end

Parameters:

  • name (String)

    The field name. The name must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String)

    A description of the field.

  • mode (Symbol)

    The field's mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable.



2537
2538
2539
# File 'lib/google/cloud/bigquery/table.rb', line 2537

def string name, description: nil, mode: :nullable
  schema.string name, description: description, mode: mode
end

#time(name, description: nil, mode: :nullable) ⇒ Object

Adds a time field to the schema.

See Schema#time.

Examples:

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
dataset = bigquery.dataset "my_dataset"
table = dataset.create_table "my_table" do |schema|
  schema.time "duration", mode: :required
end

Parameters:

  • name (String)

    The field name. The name must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String)

    A description of the field.

  • mode (Symbol)

    The field's mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable.



2705
2706
2707
# File 'lib/google/cloud/bigquery/table.rb', line 2705

def time name, description: nil, mode: :nullable
  schema.time name, description: description, mode: mode
end

#timestamp(name, description: nil, mode: :nullable) ⇒ Object

Adds a timestamp field to the schema.

See Schema#timestamp.

Examples:

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
dataset = bigquery.dataset "my_dataset"
table = dataset.create_table "my_table" do |schema|
  schema.timestamp "creation_date", mode: :required
end

Parameters:

  • name (String)

    The field name. The name must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String)

    A description of the field.

  • mode (Symbol)

    The field's mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable.



2677
2678
2679
# File 'lib/google/cloud/bigquery/table.rb', line 2677

def timestamp name, description: nil, mode: :nullable
  schema.timestamp name, description: description, mode: mode
end

#to_gapiObject



2812
2813
2814
2815
# File 'lib/google/cloud/bigquery/table.rb', line 2812

def to_gapi
  check_for_mutated_schema!
  @gapi
end