sishen-hbase-ruby 0.2.8 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.txt CHANGED
@@ -16,6 +16,8 @@ require 'hbase'
16
16
  client = HBase::Client.new("http://localhost:60010/api") # this url is the default.
17
17
  tables = client.list_tables # list available tables
18
18
 
19
+ table = client.create_table('users', 'habbit') # create a table whose column_family is habbit
20
+
19
21
  table = client.show_table('users') # show the meta info of table users
20
22
 
21
23
  row = client.show_row('users', 'sishen') # show the data of row 'sishen' in table 'users'
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'hoe'
3
3
 
4
- Hoe.new('sishen-hbase-ruby', '0.2.6') do |p|
4
+ Hoe.new('sishen-hbase-ruby', '0.3.0') do |p|
5
5
  p.rubyforge_name = 'sishen-hbase-ruby'
6
6
  p.author = 'Ye Dingding'
7
7
  p.email = 'yedingding@gmail.com'
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.8"
3
+ s.version = "0.3.0"
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
@@ -20,7 +20,7 @@ module HBase
20
20
  end
21
21
 
22
22
  unless @url.path =~ /^\/api/
23
- @url += (@url[-1] == '/' ? "api" : "/api")
23
+ @url.path += (@url.path[-1] == '/' ? "api" : "/api")
24
24
  end
25
25
 
26
26
  # Not actually opening the connection yet, just setting up the persistent connection.
@@ -40,6 +40,10 @@ module HBase
40
40
  safe_request { @connection.delete(@url.path + path) }
41
41
  end
42
42
 
43
+ def put(path, data = nil)
44
+ safe_request { @connection.put(@url.path + path, data, {'Content-Type' => 'text/xml'}) }
45
+ end
46
+
43
47
  private
44
48
  def safe_request(&block)
45
49
  begin
@@ -36,17 +36,34 @@ module HBase
36
36
  Response::TableResponse.new(post(request.create, xml_data))
37
37
  rescue Net::ProtocolError => e
38
38
  if e.to_s.include?("TableExistsException")
39
- raise TableExistsError, "Table '#{name} already exists"
39
+ raise TableExistsError, "Table '#{name}' already exists"
40
40
  else
41
41
  raise TableFailCreateError, e.message
42
42
  end
43
43
  end
44
44
  end
45
45
 
46
- def delete_table(name)
46
+ def alter_table(name, *args)
47
+ raise StandardError, "Table name must be of type String" unless name.instance_of? String
48
+
49
+ request = Request::TableRequest.new(name)
50
+
51
+ begin
52
+ xml_data = construct_xml_stream(name, *args)
53
+ Response::TableResponse.new(put(request.update, xml_data))
54
+ rescue Net::ProtocolError => e
55
+ if e.to_s.include?("TableNotFoundException")
56
+ raise TableNotFoundError, "Table '#{name}' not exists"
57
+ else
58
+ raise TableFailCreateError, e.message
59
+ end
60
+ end
61
+ end
62
+
63
+ def delete_table(name, columns = nil)
47
64
  begin
48
65
  request = Request::TableRequest.new(name)
49
- Response::TableResponse.new(delete(request.delete))
66
+ Response::TableResponse.new(delete(request.delete(columns)))
50
67
  rescue Net::ProtocolError => e
51
68
  if e.to_s.include?("TableNotFoundException")
52
69
  raise TableNotFoundError, "Table '#{name}' not exists"
@@ -84,6 +101,29 @@ module HBase
84
101
 
85
102
  def table_regions(name, start_row = nil, end_row = nil)
86
103
  end
104
+
105
+ private
106
+ def construct_xml_stream(name, *args)
107
+ xml_data = "<?xml version='1.0' encoding='UTF-8'?><table><name>#{name}</name><columnfamilies>"
108
+ for arg in args
109
+ if arg.instance_of? String
110
+ xml_data << "<columnfamily><name>#{arg}</name></columnfamily>"
111
+ elsif arg.instance_of? Hash
112
+ xml_data << "<columnfamily>"
113
+ arg.each do |k,v|
114
+ if Model::ColumnDescriptor::AVAILABLE_OPTS.include? k
115
+ xml_data << "<#{Model::ColumnDescriptor::AVAILABLE_OPTS[k]}>#{v}</#{Model::ColumnDescriptor::AVAILABLE_OPTS[k]}>"
116
+ end
117
+ end
118
+ xml_data << "</columnfamily>"
119
+ else
120
+ raise StandardError, "#{arg.class.to_s} of #{arg.to_s} is not of Hash Type"
121
+ end
122
+ end
123
+ xml_data << "</columnfamilies></table>"
124
+
125
+ xml_data
126
+ end
87
127
  end
88
128
  end
89
129
  end
@@ -21,6 +21,10 @@ module HBase
21
21
  @path << "/"
22
22
  end
23
23
 
24
+ def update
25
+ @path << "/#{name}"
26
+ end
27
+
24
28
  def enable
25
29
  @path << "/#{name}/enable"
26
30
  end
@@ -29,8 +33,19 @@ module HBase
29
33
  @path << "/#{name}/disable"
30
34
  end
31
35
 
32
- def delete
36
+ def delete(columns = nil)
33
37
  @path << "/#{name}"
38
+ if columns
39
+ if columns.is_a? String
40
+ columns = [columns]
41
+ elsif columns.is_a? Array
42
+ else
43
+ raise StandardError, "Only String or Array type allows for columns"
44
+ end
45
+ params = columns.collect { |column| "column=#{column}" }.join('%')
46
+ @path << "?#{params}"
47
+ end
48
+ @path
34
49
  end
35
50
  end
36
51
  end
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.8
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ye Dingding