Class: Google::Cloud::Bigquery::Schema
- Inherits:
-
Object
- Object
- Google::Cloud::Bigquery::Schema
- Defined in:
- lib/google/cloud/bigquery/schema.rb
Overview
Table Schema
A builder for BigQuery table schemas, passed to block arguments to Dataset#create_table and Table#schema. Supports nested and repeated fields via a nested block.
Defined Under Namespace
Classes: Field
Instance Method Summary collapse
-
#boolean(name, description: nil, mode: :nullable) ⇒ Object
Adds a boolean field to the schema.
- #empty? ⇒ Boolean
- #fields ⇒ Object
- #fields=(new_fields) ⇒ Object
-
#float(name, description: nil, mode: :nullable) ⇒ Object
Adds a floating-point number field to the schema.
-
#initialize ⇒ Schema
constructor
A new instance of Schema.
-
#integer(name, description: nil, mode: :nullable) ⇒ Object
Adds an integer field to the schema.
-
#record(name, description: nil, mode: nil) {|nested_schema| ... } ⇒ Object
Adds a record field to the schema.
-
#string(name, description: nil, mode: :nullable) ⇒ Object
Adds a string field to the schema.
-
#timestamp(name, description: nil, mode: :nullable) ⇒ Object
Adds a timestamp field to the schema.
Constructor Details
#initialize ⇒ Schema
Returns a new instance of Schema
46 47 48 |
# File 'lib/google/cloud/bigquery/schema.rb', line 46 def initialize @nested = nil end |
Instance Method Details
#boolean(name, description: nil, mode: :nullable) ⇒ Object
Adds a boolean field to the schema.
167 168 169 |
# File 'lib/google/cloud/bigquery/schema.rb', line 167 def boolean name, description: nil, mode: :nullable add_field name, :boolean, nil, description: description, mode: mode end |
#empty? ⇒ Boolean
59 60 61 |
# File 'lib/google/cloud/bigquery/schema.rb', line 59 def empty? fields.empty? end |
#fields ⇒ Object
50 51 52 |
# File 'lib/google/cloud/bigquery/schema.rb', line 50 def fields @fields ||= @gapi.fields.map { |f| Field.from_gapi f } end |
#fields=(new_fields) ⇒ Object
54 55 56 57 |
# File 'lib/google/cloud/bigquery/schema.rb', line 54 def fields= new_fields @gapi.fields = Array(new_fields).map(&:to_gapi) @fields = @gapi.fields.map { |f| Field.from_gapi f } end |
#float(name, description: nil, mode: :nullable) ⇒ Object
Adds a floating-point number field to the schema.
152 153 154 |
# File 'lib/google/cloud/bigquery/schema.rb', line 152 def float name, description: nil, mode: :nullable add_field name, :float, nil, description: description, mode: mode end |
#integer(name, description: nil, mode: :nullable) ⇒ Object
Adds an integer field to the schema.
137 138 139 |
# File 'lib/google/cloud/bigquery/schema.rb', line 137 def integer name, description: nil, mode: :nullable add_field name, :integer, nil, 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 .
220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/google/cloud/bigquery/schema.rb', line 220 def record name, description: nil, mode: nil fail ArgumentError, "nested RECORD type is not permitted" if @nested fail ArgumentError, "a block is required" unless block_given? empty_schema = Google::Apis::BigqueryV2::TableSchema.new fields: [] nested_schema = self.class.from_gapi(empty_schema).tap do |s| s.instance_variable_set :@nested, true end yield nested_schema add_field name, :record, nested_schema.fields, description: description, mode: mode end |
#string(name, description: nil, mode: :nullable) ⇒ Object
Adds a string field to the schema.
122 123 124 |
# File 'lib/google/cloud/bigquery/schema.rb', line 122 def string name, description: nil, mode: :nullable add_field name, :string, nil, description: description, mode: mode end |
#timestamp(name, description: nil, mode: :nullable) ⇒ Object
Adds a timestamp field to the schema.
182 183 184 |
# File 'lib/google/cloud/bigquery/schema.rb', line 182 def name, description: nil, mode: :nullable add_field name, :timestamp, nil, description: description, mode: mode end |