Module: PatriotHadoop::Ext::Hive

Includes:
Patriot::Util::DBClient, Patriot::Util::Logger, Patriot::Util::System
Included in:
Command::HiveCommand
Defined in:
lib/patriot_hadoop/ext/hive.rb

Defined Under Namespace

Classes: HiveException

Constant Summary collapse

HIVE_MAX_ERROR_MSG_SIZE =
512

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(cls) ⇒ Object



11
12
13
# File 'lib/patriot_hadoop/ext/hive.rb', line 11

def self.included(cls)
  cls.send(:include, Patriot::Util::System)
end

Instance Method Details

#_compress_option(extension) ⇒ Object



30
31
32
33
34
35
36
# 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) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# 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