class RSpec::Hive::ConnectionDelegator

Public Class Methods

new(connection, config) click to toggle source
Calls superclass method
# File lib/rspec/hive/connection_delegator.rb, line 9
def initialize(connection, config)
  super(connection)
  @config = config
end

Public Instance Methods

create_database(name) click to toggle source
# File lib/rspec/hive/connection_delegator.rb, line 47
def create_database(name)
  execute("CREATE DATABASE IF NOT EXISTS `#{name}`")
end
create_table(table_schema) click to toggle source
# File lib/rspec/hive/connection_delegator.rb, line 14
def create_table(table_schema)
  table_schema = table_schema.dup
  table_schema.instance_variable_set(:@location, nil)
  execute(table_schema.create_table_statement)
end
drop_database(name) click to toggle source
# File lib/rspec/hive/connection_delegator.rb, line 55
def drop_database(name)
  execute("DROP DATABASE `#{name}`")
end
load_into_table(table_schema, values, partitions = nil) click to toggle source
# File lib/rspec/hive/connection_delegator.rb, line 26
def load_into_table(table_schema, values, partitions = nil)
  table_name = table_schema.name
  Tempfile.open(table_name, @config.host_shared_directory_path) do |file|
    write_values_to_file(
      file,
      values,
      table_schema.instance_variable_get(:@field_sep)
    )
    partition_query = partition_clause(table_schema, partitions) if partitions
    load_file_to_hive_table(
      table_name,
      docker_path(file),
      partition_query
    )
  end
end
load_partitions(table_schema, partitions) click to toggle source
# File lib/rspec/hive/connection_delegator.rb, line 20
def load_partitions(table_schema, partitions)
  partitions = partition_clause(table_schema, partitions)
  query = "ALTER TABLE #{table_schema.name} ADD #{partitions}"
  execute(query)
end
show_databases() click to toggle source
# File lib/rspec/hive/connection_delegator.rb, line 59
def show_databases
  fetch('SHOW DATABASES')
end
show_tables() click to toggle source
# File lib/rspec/hive/connection_delegator.rb, line 43
def show_tables
  fetch('SHOW TABLES')
end
switch_database(db_name) click to toggle source
# File lib/rspec/hive/connection_delegator.rb, line 63
def switch_database(db_name)
  create_database(db_name)
  use_database(db_name)
end
use_database(name) click to toggle source
# File lib/rspec/hive/connection_delegator.rb, line 51
def use_database(name)
  execute("USE `#{name}`")
end

Private Instance Methods

docker_path(file) click to toggle source
# File lib/rspec/hive/connection_delegator.rb, line 93
def docker_path(file)
  File.join(
    @config.docker_shared_directory_path,
    File.basename(file.path)
  )
end
load_file_to_hive_table(table_name, path, partition_clause = nil) click to toggle source
# File lib/rspec/hive/connection_delegator.rb, line 87
def load_file_to_hive_table(table_name, path, partition_clause = nil)
  request_txt = "load data local inpath '#{path}' into table #{table_name}"
  return execute(request_txt) if partition_clause.nil?
  execute("#{request_txt} #{partition_clause}")
end
partition_clause(table_schema, partitions) click to toggle source
# File lib/rspec/hive/connection_delegator.rb, line 70
def partition_clause(table_schema, partitions)
  if partitions.is_a?(Array)
    partitions.collect { |x| to_partition_clause(table_schema, x) }.join(' ')
  else
    to_partition_clause(table_schema, partitions)
  end
end
partition_value(table_schema, key, value) click to toggle source
# File lib/rspec/hive/connection_delegator.rb, line 82
def partition_value(table_schema, key, value)
  return value if table_schema.partitions.detect { |x| x.name == key && x.type == :int }
  "'#{value}'"
end
to_partition_clause(table_schema, partition) click to toggle source
# File lib/rspec/hive/connection_delegator.rb, line 78
def to_partition_clause(table_schema, partition)
  "PARTITION(#{partition.map { |k, v| "#{k}=#{partition_value(table_schema, k, v)}" }.join(',')})"
end
write_values_to_file(file, values, delimiter = ';') click to toggle source
# File lib/rspec/hive/connection_delegator.rb, line 100
def write_values_to_file(file, values, delimiter = ';')
  values.each { |value| file.puts(value.join(delimiter)) }
  file.flush
end