Class: Impaler::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/impaler/manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(impala_servers, hivethrift_servers, logger = Impaler::DEFAULT_LOGGER) ⇒ Manager

Returns a new instance of Manager.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/impaler/manager.rb', line 12

def initialize(impala_servers, hivethrift_servers, logger=Impaler::DEFAULT_LOGGER)
  if impala_servers.nil? and hivethrift_servers.nil? 
    raise Impaler::ConnectionError.new("No impaler or hive servers were specified, at least one is required")
  end

  if !impala_servers.nil?
    if impala_servers.respond_to?(:choice)
      @impala_servers=impala_servers
    else 
      @impala_servers=[impala_servers]
    end

    impala_server = @impala_servers.choice.split(":")
    @impala_host = impala_server[0]
    @impala_port = impala_server[1]
  end

  if !hivethrift_servers.nil?
    if hivethrift_servers.respond_to?(:choice)
      @hivethrift_servers=hivethrift_servers
    else
      @hivethrift_servers=[hivethrift_servers]
    end
    hivethrift_server = @hivethrift_servers.choice.split(":")
    @hivethrift_host = hivethrift_server[0]
    @hivethrift_port = hivethrift_server[1]
  end

  @logger = logger
  open
end

Instance Method Details

#closeObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/impaler/manager.rb', line 70

def close
  if !@impala_connection.nil?
    @impala_connection.close
    @impala_connection = nil
  end

  if !@hivethrift_connection.nil?
    @hivethrift_connection.close
    @hivethrift_connection = nil
  end
end

#columns(tablename) ⇒ Object

########################################################################### Metadata methods



144
145
146
147
148
149
150
151
152
# File 'lib/impaler/manager.rb', line 144

def columns(tablename)
  desc = {}
  (query "describe #{tablename}").each { |col|
    cname=col[:name] || col[:col_name]
    ctype=col[:type] || col[:data_type]
    desc[cname.intern] = ctype.intern
  }
  desc
end

#openObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/impaler/manager.rb', line 44

def open
  connected=false
  if !@impala_host.nil? && !@impala_port.nil?
    @logger.debug "Impala connection #{@impala_host}:#{@impala_port}"
    @impala_connection = Impala.connect(@impala_host, @impala_port)
    @impala_connection.open
    @impala_connection.refresh
    connected=true
  else
    @impala_connection = nil
  end

  if !@hivethrift_host.nil? && !@hivethrift_port.nil?
    @logger.debug "Hivethrift connection #{@hivethrift_host}:#{@hivethrift_port}"
    @hivethrift_connection = RBHive::Connection.new(@hivethrift_host, @hivethrift_port)
    @hivethrift_connection.open
    connected=true
  else
    @hivethrift_connection = nil
  end

  if !connected
    raise Impaler::ConnectionError.new("All connections failed")
  end
end

#query(sql, query_mode = Impaler::IMPALA_THEN_HIVE) ⇒ Object

########################################################################### General use methods



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/impaler/manager.rb', line 86

def query(sql, query_mode = Impaler::IMPALA_THEN_HIVE)
  ret = nil
  error = nil
  success = false
  unless query_mode == Impaler::HIVE_ONLY or @impala_connection.nil? 
    begin
      @logger.debug "Trying query in impala"
      ret = @impala_connection.query(sql)
      @logger.debug "Successful query in impala"
      success = true
    rescue StandardError => e
      error = e
      @logger.warn "Impala error: #{e}"
    end
  end

  unless @hivethrift_connection.nil? || success || query_mode == Impaler::IMPALA_ONLY 
    begin
      @logger.debug "Trying query in hive"
      ret = @hivethrift_connection.fetch(sql)
      @logger.debug "Successful query in hive"
      success = true
    rescue StandardError => e
      error = e
      @logger.warn "Hive error: #{e}"
    end
  end

  if !success && !error.nil? 
    raise error
  elsif !success 
    raise Impaler::QueryError.new("Query did not run due to no connections being available")
  end
  return ret
end

#row_count(tablename) ⇒ Object

########################################################################### Helper query methods



133
134
135
# File 'lib/impaler/manager.rb', line 133

def row_count(tablename)
    query("SELECT COUNT(1) c FROM #{tablename}").first[:c]
end

#set(key, value) ⇒ Object



122
123
124
125
126
127
# File 'lib/impaler/manager.rb', line 122

def set(key, value)
  # Only run on hive since that's the only one that supports set for now
  if !@hivethrift_connection.nil?
    @hivethrift_connection.set(key, value)
  end
end

#tables(pattern = nil) ⇒ Object



154
155
156
157
158
159
# File 'lib/impaler/manager.rb', line 154

def tables(pattern=nil)
  q = "SHOW TABLES" + ((pattern.nil?) ? "" : " '#{pattern}'")
  query(q).collect { |table|
    table[:name] || table[:tab_name]
  }
end