Sha256: e4d9e5366d0826839af292083346cc5a2cced62a662c82b99d5e1be3e8c2d851

Contents?: true

Size: 1.32 KB

Versions: 3

Compression:

Stored size: 1.32 KB

Contents

require 'gcloud'

module BqFactory
  class Client
    delegate :bigquery, to: :gcloud

    def initialize(project_id, keyfile_path)
      @gcloud = Gcloud.new project_id, keyfile_path
    end

    def create_view(dataset_name, table_id, query)
      dataset(dataset_name).create_view(table_id, query)
    end

    def create_dataset!(dataset_name)
      bigquery.create_dataset(dataset_name)
    end

    def create_table!(dataset_name, table_id, schema)
      dataset(dataset_name).create_table(table_id, schema: { fields: schema })
    end

    def delete_dataset!(dataset_name)
      dataset(dataset_name).delete(force: true)
    end

    def delete_table!(dataset_name, table_id)
      dataset(dataset_name).table(table_id).delete(force: true)
    end

    def fetch_schema(dataset_name, table_id)
      table(dataset_name, table_id).schema["fields"]
    end

    def query(query)
      bigquery.query(query)
    end

    private

    attr_reader :gcloud

    def table(dataset_name, table_id)
      dataset = bigquery.dataset(dataset_name)
      table = dataset.table(table_id)
      raise ArgumentError.new, "table not found: #{table_id}" if table.nil?
      table
    end

    def dataset(name)
      dataset = bigquery.dataset(name)
      raise ArgumentError.new, "dataset not found: #{name}" if dataset.nil?
      dataset
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bq_factory-0.2.0 lib/bq_factory/client.rb
bq_factory-0.1.1 lib/bq_factory/client.rb
bq_factory-0.1.0 lib/bq_factory/client.rb