Method: BigShift::BigQuery::Dataset#create_table

Defined in:
lib/bigshift/big_query/dataset.rb

#create_table(table_name, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bigshift/big_query/dataset.rb', line 22

def create_table(table_name, options={})
  table_reference = Google::Apis::BigqueryV2::TableReference.new(
    project_id: @project_id,
    dataset_id: @dataset_id,
    table_id: table_name
  )
  if options[:schema]
    fields = options[:schema]['fields'].map { |f| Google::Apis::BigqueryV2::TableFieldSchema.new(name: f['name'], type: f['type'], mode: f['mode']) }
    schema = Google::Apis::BigqueryV2::TableSchema.new(fields: fields)
  end
  table_spec = {}
  table_spec[:table_reference] = table_reference
  table_spec[:schema] = schema if schema
  table_data = Google::Apis::BigqueryV2::Table.new(table_spec)
  table_data = @big_query_service.insert_table(@project_id, @dataset_id, table_data)
  Table.new(@big_query_service, table_data, logger: @logger)
end