class Google::Cloud::Bigquery::External::JsonSource

# JsonSource

{External::JsonSource} is a subclass of {External::DataSource} and represents a JSON external data source that can be queried from directly, such as Google Cloud Storage or Google Drive, even though the data is not stored in BigQuery. Instead of loading or streaming the data, this object references the external data source.

@example

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new

json_url = "gs://bucket/path/to/data.json"
json_table = bigquery.external json_url do |json|
  json.schema do |schema|
    schema.string "name", mode: :required
    schema.string "email", mode: :required
    schema.integer "age", mode: :required
    schema.boolean "active", mode: :required
  end
end

data = bigquery.query "SELECT * FROM my_ext_table",
                      external: { my_ext_table: json_table }

# Iterate over the first page of results
data.each do |row|
  puts row[:name]
end
# Retrieve the next page of results
data = data.next if data.next?

Public Class Methods

from_gapi(gapi) click to toggle source

@private Google API Client object.

# File lib/google/cloud/bigquery/external/json_source.rb, line 160
def self.from_gapi gapi
  new_table = super
  schema = Schema.from_gapi gapi.schema
  new_table.instance_variable_set :@schema, schema
  new_table
end

Public Instance Methods

fields() click to toggle source

The fields of the schema.

@return [Array<Schema::Field>] An array of field objects.

# File lib/google/cloud/bigquery/external/json_source.rb, line 128
def fields
  schema.fields
end
headers() click to toggle source

The names of the columns in the schema.

@return [Array<Symbol>] An array of column names.

# File lib/google/cloud/bigquery/external/json_source.rb, line 137
def headers
  schema.headers
end
param_types() click to toggle source

The types of the fields in the data in the schema, using the same format as the optional query parameter types.

@return [Hash] A hash with field names as keys, and types as values.

# File lib/google/cloud/bigquery/external/json_source.rb, line 147
def param_types
  schema.param_types
end
schema(replace: false) { |schema| ... } click to toggle source

The schema for the data.

@param [Boolean] replace 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. The default value is `false`.

@yield [schema] a block for setting the schema @yieldparam [Schema] schema the object accepting the schema

@return [Google::Cloud::Bigquery::Schema]

@example

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new

json_url = "gs://bucket/path/to/data.json"
json_table = bigquery.external json_url do |json|
  json.schema do |schema|
    schema.string "name", mode: :required
    schema.string "email", mode: :required
    schema.integer "age", mode: :required
    schema.boolean "active", mode: :required
  end
end
# File lib/google/cloud/bigquery/external/json_source.rb, line 86
def schema replace: false
  @schema ||= Schema.from_gapi @gapi.schema
  if replace
    frozen_check!
    @schema = Schema.from_gapi
  end
  @schema.freeze if frozen?
  yield @schema if block_given?
  @schema
end
schema=(new_schema) click to toggle source

Set the schema for the data.

@param [Schema] new_schema The schema object.

@example

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new

json_shema = bigquery.schema do |schema|
  schema.string "name", mode: :required
  schema.string "email", mode: :required
  schema.integer "age", mode: :required
  schema.boolean "active", mode: :required
end

json_url = "gs://bucket/path/to/data.json"
json_table = bigquery.external json_url
json_table.schema = json_shema
# File lib/google/cloud/bigquery/external/json_source.rb, line 118
def schema= new_schema
  frozen_check!
  @schema = new_schema
end
to_gapi() click to toggle source

@private Google API Client object.

# File lib/google/cloud/bigquery/external/json_source.rb, line 153
def to_gapi
  @gapi.schema = @schema.to_gapi if @schema
  @gapi
end