Module: HBase::Operation::TableOperation

Included in:
Client
Defined in:
lib/hbase/operation/table_operation.rb

Instance Method Summary collapse

Instance Method Details

#alter_table(name, *args) ⇒ Object

Raises:

  • (StandardError)

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/hbase/operation/table_operation.rb', line 46

def alter_table(name, *args)
  raise StandardError, "Table name must be of type String" unless name.instance_of? String

  request = Request::TableRequest.new(name)

  begin
    xml_data = construct_xml_stream(name, *args)
    Response::TableResponse.new(put(request.update, xml_data))
  rescue Net::ProtocolError => e
    if e.to_s.include?("TableNotFoundException")
      raise TableNotFoundError, "Table '#{name}' not exists"
    else
      raise TableFailCreateError, e.message
    end
  end
end

#create_table(name, *args) ⇒ Object

Raises:

  • (StandardError)

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
43
44
# File 'lib/hbase/operation/table_operation.rb', line 13

def create_table(name, *args)
  request = Request::TableRequest.new(nil)

  raise StandardError, "Table name must be of type String" unless name.instance_of? String

  begin
    xml_data = "<?xml version='1.0' encoding='UTF-8'?><table><name>#{name}</name><columnfamilies>"
    for arg in args
      if arg.instance_of? String
        xml_data << "<columnfamily><name>#{arg}</name></columnfamily>"
      elsif arg.instance_of? Hash
        xml_data << "<columnfamily>"
        arg.each do |k,v|
          if Model::ColumnDescriptor::AVAILABLE_OPTS.include? k
            xml_data << "<#{Model::ColumnDescriptor::AVAILABLE_OPTS[k]}>#{v}</#{Model::ColumnDescriptor::AVAILABLE_OPTS[k]}>"
          end
        end
        xml_data << "</columnfamily>"
      else
        raise StandardError, "#{arg.class.to_s} of #{arg.to_s} is not of Hash Type"
      end
    end
    xml_data << "</columnfamilies></table>"
    Response::TableResponse.new(post(request.create, xml_data))
  rescue Net::ProtocolError => e
    if e.to_s.include?("TableExistsException")
      raise TableExistsError, "Table '#{name}' already exists"
    else
      raise TableFailCreateError, e.message
    end
  end
end

#delete_table(name, columns = nil) ⇒ Object


63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hbase/operation/table_operation.rb', line 63

def delete_table(name, columns = nil)
  begin
    request = Request::TableRequest.new(name)
    Response::TableResponse.new(delete(request.delete(columns)))
  rescue Net::ProtocolError => e
    if e.to_s.include?("TableNotFoundException")
      raise TableNotFoundError, "Table '#{name}' not exists"
    elsif e.to_s.include?("TableNotDisabledException")
      raise TableNotDisabledError, "Table '#{name}' not disabled"
    end
  end
end

#destroy_table(name, columns = nil) ⇒ Object


76
77
78
79
# File 'lib/hbase/operation/table_operation.rb', line 76

def destroy_table(name, columns = nil)
  disable_table(name)
  delete_table(name, columns)
end

#disable_table(name) ⇒ Object


94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/hbase/operation/table_operation.rb', line 94

def disable_table(name)
  begin
    request = Request::TableRequest.new(name)
    Response::TableResponse.new(post(request.disable))
  rescue Net::ProtocolError => e
    if e.to_s.include?("TableNotFoundException")
      raise TableNotFoundError, "Table '#{name}' not exists"
    else
      raise TableFailDisableError, "Table '#{name}' can not be disabled"
    end
  end
end

#enable_table(name) ⇒ Object


81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/hbase/operation/table_operation.rb', line 81

def enable_table(name)
  begin
    request = Request::TableRequest.new(name)
    Response::TableResponse.new(post(request.enable))
  rescue Net::ProtocolError => e
    if e.to_s.include?("TableNotFoundException")
      raise TableNotFoundError, "Table '#{name}' not exists"
    else
      raise TableFailEnableError, "Table '#{name}' can not be enabled"
    end
  end
end

#show_table(name) ⇒ Object


4
5
6
7
8
9
10
11
# File 'lib/hbase/operation/table_operation.rb', line 4

def show_table(name)
  begin
    request = Request::TableRequest.new(name)
    table_descriptor = Response::TableResponse.new(get(request.show)).parse
  rescue Net::ProtocolError => e
    raise TableNotFoundError, "Table '#{name}' Not found"
  end
end

#table_regions(name, start_row = nil, end_row = nil) ⇒ Object


107
108
# File 'lib/hbase/operation/table_operation.rb', line 107

def table_regions(name, start_row = nil, end_row = nil)
end