Class: HBase::HTable

Inherits:
Object
  • Object
show all
Includes:
XmlDecoder
Defined in:
lib/mandy/ruby-hbase/hbase_table.rb

Constant Summary collapse

DEFAULT_GET_OPTIONS =

Standard CRUD ops

{:timestamp => nil, :columns => nil}

Instance Method Summary collapse

Methods included from XmlDecoder

#parse_row_result

Constructor Details

#initialize(table_uri) ⇒ HTable

Returns a new instance of HTable.



11
12
13
14
15
16
17
# File 'lib/mandy/ruby-hbase/hbase_table.rb', line 11

def initialize(table_uri)
  @table_uri = table_uri

  @uri = URI.parse(table_uri)
  
  @host, @table_name = @uri.host, @uri.path.split("/").last
end

Instance Method Details

#column_descriptorsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mandy/ruby-hbase/hbase_table.rb', line 31

def column_descriptors
  column_families = []
  
  # get the xml for the column descriptors
  response = Net::HTTP.get_response(@uri.host, "/api/#{@table_name}", @uri.port)
  body = response.body
  
  # parse the xml into a document
  doc = XML::Parser.string(body).parse
  
  doc.find("/table/columnfamilies/columnfamily").each do |node|
    colfam = {}
    colfam[:name] = node.find_first("name").content.strip.chop
    column_families << colfam
  end
  column_families
end

#delete(row, columns = nil, timestamp = nil) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/mandy/ruby-hbase/hbase_table.rb', line 110

def delete(row, columns = nil, timestamp = nil)
  Net::HTTP.start(@uri.host, @uri.port) do |session|
    columns_query = Array(columns).compact.map{ |name| "column=#{name}" }.join("&")
    
    response = session.delete("/api/#{@table_name}/row/#{row}?#{columns_query}")  
    case response.code.to_i
      when 202
        return true
      else
        unexpected_response(response)
    end

  end
end

#get(key, options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mandy/ruby-hbase/hbase_table.rb', line 55

def get(key, options = {})
  opts = DEFAULT_GET_OPTIONS.merge(options)
  
  columns = Array(opts.delete(:columns)).compact
  timestamp = opts.delete(:timestamp)
  timestamp = (timestamp.to_f * 1000).to_i.to_s if timestamp
  
  Net::HTTP.start(@uri.host, @uri.port) do |session|
    columns_query = columns.map{ |name| "column=#{name}" }.join("&")

    ts_section = timestamp ? "/#{timestamp}" : ""

    query_string = "?" + columns_query
                  
    query = "/api/#{@table_name}/row/#{url_encode(key)}#{ts_section}#{query_string}"
    response = session.get(query, {"Accept" => "*/*"})

    case response.code.to_i
      when 200 #success!
        body = response.body
        parse_row_result(body).last
      when 204 #no data - probably an incorrect colname
        raise "Didn't get any data back - check your column names!"
      when 404
        raise RowNotFound, "Could not find row '#{key}'"
      else
        nil
    end
  end
end

#get_scanner(start_row, end_row, timestamp = nil, columns = nil) ⇒ Object

Scanning interface



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
# File 'lib/mandy/ruby-hbase/hbase_table.rb', line 128

def get_scanner(start_row, end_row, timestamp = nil, columns = nil)
  start_row_query = start_row ? "start_row=#{start_row}" : nil
  end_row_query = end_row ? "end_row=#{end_row}" : nil      
  timestamp_section = timestamp ? "/#{(timestamp.to_f * 1000).to_i}" : nil
  columns_section = columns ? columns.map{ |col| "column=#{col}" }.join("&") : nil

  query_string = [start_row_query, end_row_query, 
                  timestamp_section, columns_section].compact.join("&")

  path = ""

  # open the scanner
  Net::HTTP.start(@uri.host, @uri.port) do |session|
    response = session.post("/api/#{@table_name}/scanner?#{query_string}", 
      "", {"Accept" => "text/xml"}
    )
    
    case response.code.to_i
      when 201
        # redirect - grab the path and send
        Scanner.new(self, "http://#{@uri.host}:#{@uri.port}" + response["Location"])
      else
        unexpected_response(response)
    end
  end      
end

#nameObject



19
20
21
# File 'lib/mandy/ruby-hbase/hbase_table.rb', line 19

def name
  @table_name
end

#put(key, keys_and_values, timestamp = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/mandy/ruby-hbase/hbase_table.rb', line 86

def put(key, keys_and_values, timestamp = nil)
  Net::HTTP.start(@uri.host, @uri.port) do |session|
    xml = "<columns>"
    
    ts_section = timestamp ? "/#{(timestamp.to_f * 1000).to_i}" : ""
    
    keys_and_values.each do |name, value|
      xml << "<column><name>#{name}</name><value>#{[value.to_s].pack("m")}</value></column>"          
    end
    
    xml << "</columns>"
    
    query = "/api/#{@table_name}/row/#{url_encode(key)}#{ts_section}"
    response = session.post(query, xml, {"Content-type" => "text/xml"})
    
    case response.code.to_i
      when 200
        true
      else
        unexpected_response(response)
    end
  end
end

#start_keysObject

Meta-type requests

Raises:

  • (NotImplementedError)


26
27
28
# File 'lib/mandy/ruby-hbase/hbase_table.rb', line 26

def start_keys
  raise NotImplementedError
end