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 CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{hbase-ruby}
3
- s.version = "0.2.4"
3
+ s.version = "0.2.5"
4
4
 
5
5
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
6
6
  s.authors = ["Ye Dingding"]
data/lib/hbase/client.rb CHANGED
@@ -38,9 +38,16 @@ module HBase
38
38
 
39
39
  private
40
40
  def safe_request(&block)
41
- response = yield
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
- when Net::HTTPSuccess then response.body
50
+ when Net::HTTPSuccess then response.body
44
51
  else
45
52
  response.error!
46
53
  end
@@ -1 +1,19 @@
1
- class HBase::Exception < Exception; end
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
- request = Request::RowRequest.new(table_name, name, timestamp)
10
- row = Response::RowResponse.new(get(request.show(columns, version))).parse
11
- row.table_name = table_name
12
- row.name = name
13
- row.timestamp = timestamp
14
- row
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
- request = Request::RowRequest.new(table_name, name, timestamp)
19
- data = []
20
- if args.instance_of? Array
21
- data = args
22
- elsif args.instance_of? Hash
23
- data = [args]
24
- else
25
- raise StandardError, "Only Array or Hash data accepted"
26
- end
27
- xml_data ="<?xml version='1.0' encoding='UTF-8'?><columns>"
28
- data.each do |d|
29
- xml_data << "<column><name>#{d[:name]}</name>"
30
- xml_data << "<value>#{[d[:value]].pack("m") rescue ''}</value></column>"
31
- end
32
- xml_data << "</columns>"
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
- Response::RowResponse.new(post(request.create, xml_data))
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
- request = Request::RowRequest.new(table_name, name, timestamp)
39
- Response::RowResponse.new(delete(request.delete(columns)))
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
- request = Request::TableRequest.new(name)
6
- table_descriptor = Response::TableResponse.new(get(request.show)).parse
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
- xml_data = "<?xml version='1.0' encoding='UTF-8'?><table><name>#{name}</name><columnfamilies>"
15
- for arg in args
16
- if arg.instance_of? String
17
- xml_data << "<columnfamily><name>#{arg}</name></columnfamily>"
18
- elsif arg.instance_of? Hash
19
- xml_data << "<columnfamily>"
20
- arg.each do |k,v|
21
- if Model::ColumnDescriptor::AVAILABLE_OPTS.include? k
22
- xml_data << "<#{Model::ColumnDescriptor::AVAILABLE_OPTS[k]}>#{v}</#{Model::ColumnDescriptor::AVAILABLE_OPTS[k]}>"
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
- xml_data << "</columnfamily>"
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 StandardError, "#{arg.class.to_s} of #{arg.to_s} is not of Hash Type"
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
- request = Request::TableRequest.new(name)
36
- Response::TableResponse.new(delete(request.delete))
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 enalbe_table(name)
40
- request = Request::TableRequest.new(name)
41
- Response::TableResponse.new(post(request.enable))
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
- request = Request::TableRequest.new(name)
46
- Response::TableResponse.new(post(request.disable))
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)
@@ -13,7 +13,7 @@ module HBase
13
13
  when :list_tables
14
14
  if raw_data.blank?
15
15
  puts "There are no available tables in the HBase"
16
- return nil
16
+ return []
17
17
  end
18
18
 
19
19
  doc = REXML::Document.new(raw_data)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sishen-hbase-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ye Dingding