module PatriotHadoop::Ext::Hive

Constants

HIVE_MAX_ERROR_MSG_SIZE

Public Class Methods

included(cls) click to toggle source
# File lib/patriot_hadoop/ext/hive.rb, line 11
def self.included(cls)
  cls.send(:include, Patriot::Util::System)
end

Public Instance Methods

_compress_option(extension) click to toggle source
# File lib/patriot_hadoop/ext/hive.rb, line 30
def _compress_option(extension)
  return case extension
    when '.gz'  then ' | gzip --stdout --force'
    when '.bz2' then ' | bzip2 --stdout --force'
    else ''
  end
end
_execute_hivequery_internal(command, output_file) click to toggle source
# File lib/patriot_hadoop/ext/hive.rb, line 39
def _execute_hivequery_internal(command, output_file)
  res = execute_command(command) do |status, so, se|
    err_size = File.stat(se).size
    err_msg  = ""
    max_err_size = HIVE_MAX_ERROR_MSG_SIZE
    File.open(se) do |f|
      if err_size > max_err_size
        f.seek(-1 * max_err_size, IO::SEEK_END)
        err_msg = "\n(#{err_size - max_err_size} bytes are truncated)"
      end
      err_msg = "#{f.read}#{err_msg}"
    end
    raise HiveException, "#{command}\n#{err_msg}"
  end
  File.rename(res, output_file) unless output_file.nil?
end
execute_hivequery(hql_file, output_file=nil, user=nil) click to toggle source
# File lib/patriot_hadoop/ext/hive.rb, line 18
def execute_hivequery(hql_file, output_file=nil, user=nil)
  command = "hive -f \"#{hql_file}\"#{_compress_option(File.extname(output_file))}"
  unless user.nil?
    if user !~ /^[a-z_][a-z0-9_]{0,30}$/
      raise HiveException, "Invalid username" 
    end
    command = "sudo -u #{user} #{command}"
  end
  return _execute_hivequery_internal(command, output_file)
end