Class: HiveMeta::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/hivemeta/connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(db_name = nil, db_host = nil, db_user = nil, db_pass = nil) ⇒ Connection

Returns a new instance of Connection.



26
27
28
29
30
31
# File 'lib/hivemeta/connection.rb', line 26

def initialize(db_name = nil, db_host = nil, db_user = nil, db_pass = nil)
  @db_name = db_name || ENV['hivemeta_db_name']
  @db_host = db_host || ENV['hivemeta_db_host']
  @db_user = db_user || ENV['hivemeta_db_user']
  @db_pass = db_pass || ENV['hivemeta_db_pass']
end

Instance Method Details

#query_dbi(sql, *args) ⇒ Object Also known as: query



33
34
35
36
37
38
39
40
41
42
43
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
69
# File 'lib/hivemeta/connection.rb', line 33

def query_dbi(sql, *args)
  dbh = results = nil
  dbi_string = "DBI:Mysql:#{@db_name}:#{@db_host}"

  # make a few attempts in the event that mysql has not been
  # configured with enough connections to handle many mappers
  attempts, max_attempts = 0, 3
  begin
    dbh = DBI.connect(dbi_string, @db_user, @db_pass)
  rescue DBI::DatabaseError => e
    attempts += 1
    if attempts < max_attempts
      s = rand + 0.50
      STDERR.puts "retrying hivemeta connection after %f seconds..." % s
      sleep s
      retry
    else
      warn "cannot connect to metastore on %s:\n  error %s\n  %s" %
        [@db_host, e.err, e.errstr]
      raise
    end
  end

  sth = dbh.prepare(sql)
  sth.execute(*args)
  if block_given?
    sth.fetch {|row| yield row}
  else
    results = []
    sth.fetch {|row| results << row.dup}
  end
  sth.finish

  dbh.disconnect

  results # returns nil if a block is given
end

#query_jdbc(sql, *args) ⇒ Object



81
82
83
84
85
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
121
122
123
124
125
126
# File 'lib/hivemeta/connection.rb', line 81

def query_jdbc(sql, *args)
  results = []
  db_url = "jdbc:mysql://#{@db_host}/#{@db_name}"

  # make a few attempts in the event that mysql has not been
  # configured with enough connections to handle many mappers
  attempts, max_attempts = 0, 3
  begin
    c = java.sql.DriverManager.get_connection(db_url, @db_user, @db_pass)
  rescue => e
    attempts += 1
    if attempts < max_attempts
      s = rand + 0.50
      STDERR.puts "retrying hivemeta connection after %f seconds..." % s
      sleep s
      retry
    else
      warn "cannot connect to metastore on %s:\n  error %s" %
        [@db_host, e]
      raise
    end
  end

  stmt = c.create_statement

  args.each do |arg|
    # poor man's prepare
    sql = sql.sub /\?/, "'#{arg}'"
    res = stmt.execute_query sql

    cols,names = table_info_jdbc res

    while res.next do 
      row = []
      1.upto(cols) do |i|
        row << res.get_string(i)
      end
      yield row if block_given?
      results << row
    end
  end

  c.close

  results
end

#table(name) ⇒ Object



182
183
184
185
# File 'lib/hivemeta/connection.rb', line 182

def table(name)
  t = tables(:filter_name => name) # appeasing the old skool 1.8 users
  t[0] # if it comes back with multiple tables, return the first
end

#table_info_jdbc(result) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/hivemeta/connection.rb', line 71

def table_info_jdbc result
  meta = result.
  cols = meta.column_count
  colnames = []
  cols.times do |i|
    colnames[i] = meta.column_name i+1
  end
  [cols, colnames]
end

#tables(opts = {}) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/hivemeta/connection.rb', line 128

def tables(opts = {})
  args = nil
  if opts[:filter_path]
    sql = "select t.TBL_NAME from TBLS t, SDS s
      where t.SD_ID = s.SD_ID
      and s.LOCATION like ?"
    args = "%#{opts[:filter_path]}%"
  elsif opts[:filter_name]
    sql = "select TBL_NAME from TBLS
      where TBL_NAME like ?"
    args = opts[:filter_name]
  else
    sql = "select TBL_NAME from TBLS"
  end

  results = query sql, *args
  table_names = results.map {|result| result[0]}
  
  tables = []
  table_names.each do |name|
    table = Table.new(name)

    sql = "select c.INTEGER_IDX, c.column_name, c.COMMENT,
      s.LOCATION, s.SD_ID
      from TBLS t, COLUMNS c, SDS s
      where t.SD_ID = c.SD_ID and t.SD_ID = s.SD_ID
      and t.TBL_NAME = ?"
    query sql, name do |rec|
      col_idx  = rec[0].to_i
      col_name = rec[1]
      col_cmt  = rec[2]
      tbl_loc  = rec[3]
      sd_id    = rec[4]
      table.columns[col_idx]         = col_name
      table.indexes[col_name.to_sym] = col_idx
      table.comments[col_idx]        = col_cmt
      table.path      = tbl_loc
    end

    sql = "select sp.PARAM_VALUE
      from SERDE_PARAMS sp, TBLS t
      where t.SD_ID = sp.SERDE_ID
      and PARAM_KEY = 'field.delim'
    and t.TBL_NAME = ?"
    results = query sql, name
    if results and results[0] and results[0][0]
      table.delimiter = results[0][0]
    end

    tables << table
  end
  tables
end