sishen-hbase-ruby 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'hoe'
3
3
 
4
- Hoe.new('sishen-hbase-ruby', '0.3.0') do |p|
4
+ Hoe.new('sishen-hbase-ruby', '0.4.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.3.0"
3
+ s.version = "0.4.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
@@ -55,7 +55,8 @@ module HBase
55
55
  end
56
56
 
57
57
  case response
58
- when Net::HTTPSuccess then response.body
58
+ when Net::HTTPSuccess
59
+ response.body.blank? ? response.header : response.body
59
60
  else
60
61
  response.error!
61
62
  end
data/lib/hbase/model.rb CHANGED
@@ -2,8 +2,6 @@ module HBase
2
2
  module Model
3
3
  class Record
4
4
  def initialize (params)
5
- return if params.nil?
6
-
7
5
  params.each do |key, value|
8
6
  name = key.to_s
9
7
  instance_variable_set("@#{name}", value) if respond_to?(name)
@@ -18,3 +16,4 @@ require 'hbase/model/column_descriptor'
18
16
  require 'hbase/model/region_descriptor'
19
17
  require 'hbase/model/row'
20
18
  require 'hbase/model/table_descriptor'
19
+ require 'hbase/model/scanner'
@@ -4,15 +4,11 @@ module HBase
4
4
  NONE = "NONE"
5
5
  RECORD = "RECORD"
6
6
  BLOCK = "BLOCK"
7
+
8
+ CTYPES = [NONE, RECORD, BLOCK]
7
9
 
8
10
  def to_compression_type(type_string)
9
- case type_string
10
- when "NONE" then NONE
11
- when "RECORD" then RECORD
12
- when "BLOCK" then BLOCK
13
- else
14
- NONE
15
- end
11
+ CTYPES.include?(type_string) ? type_string : NONE
16
12
  end
17
13
 
18
14
  module_function :to_compression_type
@@ -1,6 +1,48 @@
1
1
  module HBase
2
2
  module Operation
3
3
  module ScannerOperation
4
+ def open_scanner(table_name, columns, start_row = nil, end_row = nil, timestamp = nil)
5
+ begin
6
+ request = Request::ScannerRequest.new(table_name)
7
+ scanner_id = Response::ScannerResponse.new(post(request.open(columns, start_row, end_row, timestamp))).get_scanner_id
8
+ rescue Net::ProtocolError => e
9
+ if e.to_s.include?("TableNotFoundException")
10
+ raise TableNotFoundError, "Table #{table_name} Not Found!"
11
+ else
12
+ raise StandardError, e.to_s
13
+ end
14
+ end
15
+ end
16
+
17
+ def get_rows(scanner, limit = 1)
18
+ begin
19
+ request = Request::ScannerRequest.new(scanner.table_name)
20
+ rows = Response::ScannerResponse.new(post(request.get_rows(scanner.scanner_id, limit))).parse
21
+ rows.each do |row|
22
+ row.table_name = scanner.table_name
23
+ end
24
+ rows
25
+ rescue StandardError => e
26
+ if e.to_s.include?("TableNotFoundException")
27
+ raise TableNotFoundError, "Table #{table_name} Not Found!"
28
+ else
29
+ raise StandardError, e.to_s
30
+ end
31
+ end
32
+ end
33
+
34
+ def close_scanner(scanner)
35
+ begin
36
+ request = Request::ScannerRequest.new(scanner.table_name)
37
+ Response::ScannerResponse.new(delete(request.close(scanner.scanner_id)))
38
+ rescue StandardError => e
39
+ if e.to_s.include?("TableNotFoundException")
40
+ raise TableNotFoundError, "Table #{table_name} Not Found!"
41
+ else
42
+ raise StandardError, e.to_s
43
+ end
44
+ end
45
+ end
4
46
  end
5
47
  end
6
48
  end
@@ -2,7 +2,7 @@ module HBase
2
2
  module Request
3
3
  class MetaRequest < BasicRequest
4
4
  def initialize
5
- super("/")
5
+ super("")
6
6
  end
7
7
 
8
8
  def list_tables
@@ -1,6 +1,38 @@
1
1
  module HBase
2
2
  module Request
3
3
  class ScannerRequest < BasicRequest
4
+ attr_reader :table_name
5
+
6
+ def initialize(table_name)
7
+ @table_name = CGI.escape(table_name)
8
+ path = "/#{@table_name}/scanner"
9
+ super(path)
10
+ end
11
+
12
+ def open(columns, start_row = nil, end_row = nil, timestamp = nil)
13
+ search = []
14
+ if columns.is_a? String
15
+ search << "column=#{columns}"
16
+ elsif columns.is_a? Array
17
+ search << columns.collect { |col| "column=#{col}" }.join("&")
18
+ else
19
+ raise StandardError, "Only string or array type allowed for columns attr."
20
+ end
21
+
22
+ search << "start_row=#{CGI.escape(start_row)}" if start_row
23
+ search << "end_row=#{CGI.escape(end_row)}" if end_row
24
+ search << "timestamp=#{CGI.escape(timestamp)}" if timestamp
25
+
26
+ @path << "?" << search.join('&')
27
+ end
28
+
29
+ def get_rows(scanner_id, limit = 1)
30
+ @path << "/#{scanner_id}?limit=#{limit}"
31
+ end
32
+
33
+ def close(scanner_id)
34
+ @path << "/#{scanner_id}"
35
+ end
4
36
  end
5
37
  end
6
38
  end
@@ -4,3 +4,4 @@ require 'hbase/response/basic_response'
4
4
  require 'hbase/response/meta_response'
5
5
  require 'hbase/response/table_response'
6
6
  require 'hbase/response/row_response'
7
+ require 'hbase/response/scanner_response'
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.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ye Dingding