sishen-hbase-ruby 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- data/hbase-ruby.gemspec +1 -1
- data/lib/hbase/client.rb +9 -2
- data/lib/hbase/exception.rb +19 -1
- data/lib/hbase/operation/row_operation.rb +48 -24
- data/lib/hbase/operation/table_operation.rb +58 -22
- data/lib/hbase/response/meta_response.rb +1 -1
- metadata +1 -1
data/hbase-ruby.gemspec
CHANGED
data/lib/hbase/client.rb
CHANGED
@@ -38,9 +38,16 @@ module HBase
|
|
38
38
|
|
39
39
|
private
|
40
40
|
def safe_request(&block)
|
41
|
-
|
41
|
+
begin
|
42
|
+
response = yield
|
43
|
+
rescue Errno::ECONNREFUSED
|
44
|
+
raise ConnectionNotEstablishedError, "can't connect to #{@url}"
|
45
|
+
rescue Timeout::Error => e
|
46
|
+
raise ConnectionTimeoutError, "execution expired. Maybe query disabled tables"
|
47
|
+
end
|
48
|
+
|
42
49
|
case response
|
43
|
-
|
50
|
+
when Net::HTTPSuccess then response.body
|
44
51
|
else
|
45
52
|
response.error!
|
46
53
|
end
|
data/lib/hbase/exception.rb
CHANGED
@@ -1 +1,19 @@
|
|
1
|
-
class HBase::Exception <
|
1
|
+
class HBase::Exception < StandardError; end
|
2
|
+
|
3
|
+
class HBase::ConnectionNotEstablishedError < HBase::Exception; end
|
4
|
+
|
5
|
+
class HBase::ConnectionTimeoutError < HBase::Exception; end
|
6
|
+
|
7
|
+
class HBase::TableNotFoundError < HBase::Exception; end
|
8
|
+
|
9
|
+
class HBase::TableExistsError < HBase::Exception; end
|
10
|
+
|
11
|
+
class HBase::TableFailCreateError < HBase::Exception; end
|
12
|
+
|
13
|
+
class HBase::TableNotDisabledError < HBase::Exception; end
|
14
|
+
|
15
|
+
class HBase::TableFailDisableError < HBase::Exception; end
|
16
|
+
|
17
|
+
class HBase::TableFailEnableError < HBase::Exception; end
|
18
|
+
|
19
|
+
class HBase::RowNotFoundError < HBase::Exception; end
|
@@ -6,37 +6,61 @@ module HBase
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def show_row(table_name, name, timestamp = nil, columns = nil, version = nil)
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
9
|
+
begin
|
10
|
+
request = Request::RowRequest.new(table_name, name, timestamp)
|
11
|
+
row = Response::RowResponse.new(get(request.show(columns, version))).parse
|
12
|
+
row.table_name = table_name
|
13
|
+
row.name = name
|
14
|
+
row.timestamp = timestamp
|
15
|
+
row
|
16
|
+
rescue Net::ProtocolError => e
|
17
|
+
if e.to_s.include?("Table")
|
18
|
+
raise TableNotFoundError, "Table '#{table_name}' Not Found"
|
19
|
+
elsif e.to_s.include?("Row")
|
20
|
+
raise RowNotFoundError, "Row '#{name}' Not Found"
|
21
|
+
end
|
22
|
+
end
|
15
23
|
end
|
16
24
|
|
17
25
|
def create_row(table_name, name, timestamp = nil, *args)
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
26
|
+
begin
|
27
|
+
request = Request::RowRequest.new(table_name, name, timestamp)
|
28
|
+
data = []
|
29
|
+
if args.instance_of? Array
|
30
|
+
data = args
|
31
|
+
elsif args.instance_of? Hash
|
32
|
+
data = [args]
|
33
|
+
else
|
34
|
+
raise StandardError, "Only Array or Hash data accepted"
|
35
|
+
end
|
36
|
+
xml_data ="<?xml version='1.0' encoding='UTF-8'?><columns>"
|
37
|
+
data.each do |d|
|
38
|
+
xml_data << "<column><name>#{d[:name]}</name>"
|
39
|
+
xml_data << "<value>#{[d[:value]].pack("m") rescue ''}</value></column>"
|
40
|
+
end
|
41
|
+
xml_data << "</columns>"
|
33
42
|
|
34
|
-
|
43
|
+
Response::RowResponse.new(post(request.create, xml_data))
|
44
|
+
rescue Net::ProtocolError => e
|
45
|
+
if e.to_s.include?("Table")
|
46
|
+
raise TableNotFoundError, "Table '#{table_name}' Not Found"
|
47
|
+
elsif e.to_s.include?("Row")
|
48
|
+
raise RowNotFoundError, "Row '#{name}' Not Found"
|
49
|
+
end
|
50
|
+
end
|
35
51
|
end
|
36
52
|
|
37
53
|
def delete_row(table_name, name, timestamp = nil, columns = nil)
|
38
|
-
|
39
|
-
|
54
|
+
begin
|
55
|
+
request = Request::RowRequest.new(table_name, name, timestamp)
|
56
|
+
Response::RowResponse.new(delete(request.delete(columns)))
|
57
|
+
rescue Net::ProtocolError => e
|
58
|
+
if e.to_s.include?("Table")
|
59
|
+
raise TableNotFoundError, "Table '#{table_name}' Not Found"
|
60
|
+
elsif e.to_s.include?("Row")
|
61
|
+
raise RowNotFoundError, "Row '#{name}' Not Found"
|
62
|
+
end
|
63
|
+
end
|
40
64
|
end
|
41
65
|
end
|
42
66
|
end
|
@@ -2,8 +2,12 @@ module HBase
|
|
2
2
|
module Operation
|
3
3
|
module TableOperation
|
4
4
|
def show_table(name)
|
5
|
-
|
6
|
-
|
5
|
+
begin
|
6
|
+
request = Request::TableRequest.new(name)
|
7
|
+
table_descriptor = Response::TableResponse.new(get(request.show)).parse
|
8
|
+
rescue Net::ProtocolError => e
|
9
|
+
raise TableNotFoundError, "Table '#{name}' Not found"
|
10
|
+
end
|
7
11
|
end
|
8
12
|
|
9
13
|
def create_table(name, *args)
|
@@ -11,39 +15,71 @@ module HBase
|
|
11
15
|
|
12
16
|
raise StandardError, "Table name must be of type String" unless name.instance_of? String
|
13
17
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
18
|
+
begin
|
19
|
+
xml_data = "<?xml version='1.0' encoding='UTF-8'?><table><name>#{name}</name><columnfamilies>"
|
20
|
+
for arg in args
|
21
|
+
if arg.instance_of? String
|
22
|
+
xml_data << "<columnfamily><name>#{arg}</name></columnfamily>"
|
23
|
+
elsif arg.instance_of? Hash
|
24
|
+
xml_data << "<columnfamily>"
|
25
|
+
arg.each do |k,v|
|
26
|
+
if Model::ColumnDescriptor::AVAILABLE_OPTS.include? k
|
27
|
+
xml_data << "<#{Model::ColumnDescriptor::AVAILABLE_OPTS[k]}>#{v}</#{Model::ColumnDescriptor::AVAILABLE_OPTS[k]}>"
|
28
|
+
end
|
23
29
|
end
|
30
|
+
xml_data << "</columnfamily>"
|
31
|
+
else
|
32
|
+
raise StandardError, "#{arg.class.to_s} of #{arg.to_s} is not of Hash Type"
|
24
33
|
end
|
25
|
-
|
34
|
+
end
|
35
|
+
xml_data << "</columnfamilies></table>"
|
36
|
+
Response::TableResponse.new(post(request.create, xml_data))
|
37
|
+
rescue Net::ProtocolError => e
|
38
|
+
if e.to_s.include?("TableExistsException")
|
39
|
+
raise TableExistsError, "Table '#{name} already exists"
|
26
40
|
else
|
27
|
-
raise
|
41
|
+
raise TableFailCreateError, e.message
|
28
42
|
end
|
29
43
|
end
|
30
|
-
xml_data << "</columnfamilies></table>"
|
31
|
-
Response::TableResponse.new(post(request.create, xml_data))
|
32
44
|
end
|
33
45
|
|
34
46
|
def delete_table(name)
|
35
|
-
|
36
|
-
|
47
|
+
begin
|
48
|
+
request = Request::TableRequest.new(name)
|
49
|
+
Response::TableResponse.new(delete(request.delete))
|
50
|
+
rescue Net::ProtocolError => e
|
51
|
+
if e.to_s.include?("TableNotFoundException")
|
52
|
+
raise TableNotFoundError, "Table '#{name}' not exists"
|
53
|
+
elsif e.to_s.include?("TableNotDisabledException")
|
54
|
+
raise TableNotDisabledError, "Table '#{name}' not disabled"
|
55
|
+
end
|
56
|
+
end
|
37
57
|
end
|
38
58
|
|
39
|
-
def
|
40
|
-
|
41
|
-
|
59
|
+
def enable_table(name)
|
60
|
+
begin
|
61
|
+
request = Request::TableRequest.new(name)
|
62
|
+
Response::TableResponse.new(post(request.enable))
|
63
|
+
rescue Net::ProtocolError => e
|
64
|
+
if e.to_s.include?("TableNotFoundException")
|
65
|
+
raise TableNotFoundError, "Table '#{name}' not exists"
|
66
|
+
else
|
67
|
+
raise TableFailEnableError, "Table '#{name}' can not be enabled"
|
68
|
+
end
|
69
|
+
end
|
42
70
|
end
|
43
71
|
|
44
72
|
def disable_table(name)
|
45
|
-
|
46
|
-
|
73
|
+
begin
|
74
|
+
request = Request::TableRequest.new(name)
|
75
|
+
Response::TableResponse.new(post(request.disable))
|
76
|
+
rescue Net::ProtocolError => e
|
77
|
+
if e.to_s.include?("TableNotFoundException")
|
78
|
+
raise TableNotFoundError, "Table '#{name}' not exists"
|
79
|
+
else
|
80
|
+
raise TableFailDisableError, "Table '#{name}' can not be disabled"
|
81
|
+
end
|
82
|
+
end
|
47
83
|
end
|
48
84
|
|
49
85
|
def table_regions(name, start_row = nil, end_row = nil)
|