hbase-stargate 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/LICENSE +20 -0
  2. data/README.textile +79 -0
  3. data/Rakefile +27 -0
  4. data/VERSION +1 -0
  5. data/lib/stargate.rb +7 -0
  6. data/lib/stargate/client.rb +93 -0
  7. data/lib/stargate/exception.rb +21 -0
  8. data/lib/stargate/model.rb +19 -0
  9. data/lib/stargate/model/column.rb +9 -0
  10. data/lib/stargate/model/column_descriptor.rb +32 -0
  11. data/lib/stargate/model/region_descriptor.rb +9 -0
  12. data/lib/stargate/model/row.rb +18 -0
  13. data/lib/stargate/model/scanner.rb +16 -0
  14. data/lib/stargate/model/table_descriptor.rb +8 -0
  15. data/lib/stargate/operation/meta_operation.rb +20 -0
  16. data/lib/stargate/operation/row_operation.rb +86 -0
  17. data/lib/stargate/operation/scanner_operation.rb +90 -0
  18. data/lib/stargate/operation/table_operation.rb +95 -0
  19. data/lib/stargate/request.rb +7 -0
  20. data/lib/stargate/request/basic_request.rb +27 -0
  21. data/lib/stargate/request/meta_request.rb +25 -0
  22. data/lib/stargate/request/row_request.rb +34 -0
  23. data/lib/stargate/request/scanner_request.rb +25 -0
  24. data/lib/stargate/request/table_request.rb +43 -0
  25. data/lib/stargate/response.rb +7 -0
  26. data/lib/stargate/response/basic_response.rb +25 -0
  27. data/lib/stargate/response/meta_response.rb +35 -0
  28. data/lib/stargate/response/row_response.rb +47 -0
  29. data/lib/stargate/response/scanner_response.rb +41 -0
  30. data/lib/stargate/response/table_response.rb +26 -0
  31. data/spec/hbase-stargate/model/column_descriptor_spec.rb +23 -0
  32. data/spec/hbase-stargate/model/column_spec.rb +12 -0
  33. data/spec/hbase-stargate/model/region_descriptor_spec.rb +4 -0
  34. data/spec/hbase-stargate/model/row_spec.rb +12 -0
  35. data/spec/hbase-stargate/model/scanner.rb +5 -0
  36. data/spec/hbase-stargate/model/table_descriptor_spec.rb +12 -0
  37. data/spec/hbase-stargate/operation/meta_operation_spec.rb +18 -0
  38. data/spec/hbase-stargate/operation/row_operation_spec.rb +75 -0
  39. data/spec/hbase-stargate/operation/scanner_operation_spec.rb +103 -0
  40. data/spec/hbase-stargate/operation/table_operation_spec.rb +43 -0
  41. data/spec/hbase-stargate/record_spec.rb +20 -0
  42. data/spec/hbase-stargate/request/meta_request_spec.rb +10 -0
  43. data/spec/hbase-stargate/request/row_request_spec.rb +4 -0
  44. data/spec/hbase-stargate/request/scanner_request_spec.rb +4 -0
  45. data/spec/hbase-stargate/request/table_request_spec.rb +4 -0
  46. data/spec/hbase-stargate/response/meta_response_spec.rb +4 -0
  47. data/spec/hbase-stargate/response/row_response_spec.rb +4 -0
  48. data/spec/hbase-stargate/response/scanner_response_spec.rb +4 -0
  49. data/spec/hbase-stargate/response/table_response_spec.rb +4 -0
  50. data/spec/spec.opts +4 -0
  51. data/spec/spec_helper.rb +3 -0
  52. metadata +144 -0
@@ -0,0 +1,41 @@
1
+ module Stargate
2
+ module Response
3
+ class ScannerResponse < BasicResponse
4
+ attr_reader :method
5
+
6
+ def initialize(raw_data, method)
7
+ @method = method
8
+ super(raw_data)
9
+ end
10
+
11
+ def parse_content(raw_data)
12
+ case @method
13
+ when :open_scanner
14
+ case raw_data
15
+ when Net::HTTPCreated
16
+ Stargate::Model::Scanner.new(:scanner_url => raw_data["Location"])
17
+ else
18
+ if raw_data.message.include?("TableNotFoundException")
19
+ raise TableNotFoundError, "Table #{table_name} Not Found!"
20
+ else
21
+ raise StandardError, "Unable to open scanner. Received the following message: #{raw_data.message}"
22
+ end
23
+ end
24
+ when :get_rows
25
+ # Dispatch it to RowResponse, since that method is made
26
+ # to deal with rows already.
27
+ RowResponse.new(raw_data, :show_row).parse
28
+ when :close_scanner
29
+ case raw_data
30
+ when Net::HTTPOK
31
+ return true
32
+ else
33
+ raise StandardError, "Unable to close scanner. Received the following message: #{raw_data.message}"
34
+ end
35
+ else
36
+ puts "method '#{@method}' not supported yet"
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,26 @@
1
+ module Stargate
2
+ module Response
3
+ class TableResponse < BasicResponse
4
+ def parse_content(raw_data)
5
+ table = JSON.parse(raw_data)
6
+ name = table["name"].strip
7
+
8
+ column_families = []
9
+ table["ColumnSchema"].each do |columnfamily|
10
+ colname = columnfamily["name"].strip
11
+ compression = columnfamily["COMPRESSION"].strip
12
+ bloomfilter = (columnfamily["BLOOMFILTER"].strip =~ /^true$/i ? true : false)
13
+ max_versions = columnfamily["VERSIONS"].strip.to_i
14
+
15
+ column_descriptor = Model::ColumnDescriptor.new(:name => colname,
16
+ :compression => Model::CompressionType.to_compression_type(compression),
17
+ :bloomfilter => bloomfilter,
18
+ :max_versions => max_versions)
19
+ column_families << column_descriptor
20
+ end
21
+
22
+ Model::TableDescriptor.new(:name => name, :column_families => column_families)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,23 @@
1
+ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
2
+
3
+ describe Stargate::Model::CompressionType do
4
+
5
+ it "should be return the correct type" do
6
+ Stargate::Model::CompressionType.to_compression_type("RECORD").should == "RECORD"
7
+ Stargate::Model::CompressionType.to_compression_type("BLOCK").should == "BLOCK"
8
+ Stargate::Model::CompressionType.to_compression_type("NONE").should == "NONE"
9
+ Stargate::Model::CompressionType.to_compression_type("OTHER").should == "NONE"
10
+ end
11
+
12
+ end
13
+
14
+ describe Stargate::Model::ColumnDescriptor do
15
+
16
+ it "should be have such attributes" do
17
+ column = Stargate::Model::ColumnDescriptor.new({})
18
+ %w{name compression bloomfilter maximum_cell_size max_versions}.each do |method|
19
+ column.should respond_to(method)
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,12 @@
1
+ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
2
+
3
+ describe Stargate::Model::Column do
4
+
5
+ it "should be have such attributes" do
6
+ column = Stargate::Model::Column.new({})
7
+ %w{name value timestamp}.each do |method|
8
+ column.should respond_to(method)
9
+ end
10
+ end
11
+
12
+ end
@@ -0,0 +1,4 @@
1
+ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
2
+
3
+ describe Stargate::Model::RegionDescriptor do
4
+ end
@@ -0,0 +1,12 @@
1
+ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
2
+
3
+ describe Stargate::Model::Row do
4
+
5
+ it "should be have such attributes" do
6
+ obj = Stargate::Model::Row.new({})
7
+ %w{name table_name columns}.each do |method|
8
+ obj.should respond_to(method)
9
+ end
10
+ end
11
+
12
+ end
@@ -0,0 +1,5 @@
1
+ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
2
+
3
+ describe Stargate::Model::Scanner do
4
+
5
+ end
@@ -0,0 +1,12 @@
1
+ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
2
+
3
+ describe Stargate::Model::TableDescriptor do
4
+
5
+ it "should be have such attributes" do
6
+ obj = Stargate::Model::TableDescriptor.new({})
7
+ %w{name column_families}.each do |method|
8
+ obj.should respond_to(method)
9
+ end
10
+ end
11
+
12
+ end
@@ -0,0 +1,18 @@
1
+ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
2
+
3
+ describe Stargate::Operation::MetaOperation do
4
+
5
+ before do
6
+ url = ENV["STARGATE_URL"].nil? ? "http://localhost:8080" : ENV["STARGATE_URL"]
7
+ @client = Stargate::Client.new(url)
8
+ end
9
+
10
+ it "should return tables" do
11
+ tables = @client.list_tables
12
+ tables.should respond_to(:size)
13
+ tables.each do |table|
14
+ table.should.is_a? Stargate::Model::TableDescriptor
15
+ end
16
+ end
17
+
18
+ end
@@ -0,0 +1,75 @@
1
+ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
2
+
3
+ describe Stargate::Operation::RowOperation do
4
+ before :all do
5
+ url = ENV["STARGATE_URL"].nil? ? "http://localhost:8080" : ENV["STARGATE_URL"]
6
+ @client = Stargate::Client.new(url)
7
+
8
+ table = @client.create_table("test-hbase-stargate", "col1")
9
+ end
10
+
11
+ it "should create a row called 'row1'" do
12
+ lambda {
13
+ @client.create_row("test-hbase-stargate", "row1", nil, { :name => "col1:", :value => "row1-col1" }).should be_true
14
+ }.should_not raise_error
15
+ end
16
+
17
+ it "should create a row named 'row2' with timestamp value" do
18
+ timestamp = (Time.now - (5*60)).to_i
19
+ lambda {
20
+ @client.create_row("test-hbase-stargate", "row2", timestamp, { :name => "col1:cell1", :value => "row2-col1-cell1" }).should be_true
21
+ }.should_not raise_error
22
+
23
+ row = @client.show_row("test-hbase-stargate", "row2")
24
+ row.should be_a_kind_of(Stargate::Model::Row)
25
+ row.name.should == "row2"
26
+
27
+ columns = row.columns
28
+ columns.size.should == 1
29
+ columns.first.name.should == "col1:cell1"
30
+ columns.first.value.should == "row2-col1-cell1"
31
+ columns.first.timestamp.should == timestamp
32
+ end
33
+
34
+ it "should show the rows 'row1'" do
35
+ row = @client.show_row("test-hbase-stargate", "row1")
36
+ row.should.is_a? Stargate::Model::Row
37
+ row.table_name.should == "test-hbase-stargate"
38
+ row.name.should == "row1"
39
+ row.columns.size.should == 1
40
+ row.columns.each do |col|
41
+ col.should.is_a? Stargate::Model::Column
42
+ col.name.should == "col1:"
43
+ col.value.should == "row1-col1"
44
+ end
45
+ end
46
+
47
+ it "should delete rows when timestamps are defined" do
48
+ row1 = @client.show_row("test-hbase-stargate", "row1")
49
+ timestamp = row1.columns.map(&:timestamp).uniq.first
50
+
51
+ lambda {
52
+ @client.delete_row('test-hbase-stargate', 'row1', timestamp).should be_true
53
+ }.should_not raise_error
54
+
55
+ lambda {
56
+ @client.show_row('test-hbase-stargate', 'row1')
57
+ }.should raise_error
58
+ end
59
+
60
+ it "should delete rows without a timestamp provided" do
61
+ row2 = @client.show_row("test-hbase-stargate", "row2")
62
+
63
+ lambda {
64
+ @client.delete_row('test-hbase-stargate', 'row2').should be_true
65
+ }.should_not raise_error
66
+
67
+ lambda {
68
+ @client.show_row('test-hbase-stargate', 'row2')
69
+ }.should raise_error
70
+ end
71
+
72
+ after :all do
73
+ table = @client.destroy_table("test-hbase-stargate")
74
+ end
75
+ end
@@ -0,0 +1,103 @@
1
+ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
2
+
3
+ describe Stargate::Operation::ScannerOperation do
4
+ before :all do
5
+ url = ENV["STARGATE_URL"].nil? ? "http://localhost:8080" : ENV["STARGATE_URL"]
6
+ @client = Stargate::Client.new(url)
7
+
8
+ table = @client.create_table("test-hbase-stargate", "col1")
9
+
10
+ @client.create_row('test-hbase-stargate', 'row1', nil, {:name => 'col1:', :value => "row1-col1"})
11
+ @client.create_row('test-hbase-stargate', 'row2', nil, {:name => 'col1:', :value => "row2-col1"})
12
+ @client.create_row('test-hbase-stargate', 'row3', nil, {:name => 'col1:', :value => "row3-col1"})
13
+ end
14
+
15
+ it "should throw TableNotFoundError if a scanner is requested for an non-existant table" do
16
+ lambda {
17
+ scanner = @client.open_scanner("test-nonexistant-table")
18
+ }.should raise_error
19
+ end
20
+
21
+ it "should open a scanner and close it successfully" do
22
+ scanner = @client.open_scanner("test-hbase-stargate")
23
+ scanner.should.is_a? Stargate::Model::Scanner
24
+
25
+ lambda {
26
+ URI.parse("scanner.scanner_url")
27
+ }.should_not raise_error
28
+
29
+ lambda {
30
+ @client.close_scanner(scanner).should be_true
31
+ }.should_not raise_error
32
+ end
33
+
34
+ it "should scan the whole table when given no options and no limit" do
35
+ scanner = @client.open_scanner("test-hbase-stargate")
36
+
37
+ rows = @client.get_rows(scanner)
38
+ rows.size.should == 3
39
+ rows.each do |row|
40
+ row.should be_an_instance_of Stargate::Model::Row
41
+ ["row1", "row2", "row3"].should include(row.name)
42
+ end
43
+
44
+ @client.close_scanner(scanner).should be_true
45
+ end
46
+
47
+ it "should scan the whole table but limit the results when given a limit" do
48
+ scanner = @client.open_scanner("test-hbase-stargate")
49
+
50
+ rows = @client.get_rows(scanner, 2)
51
+ rows.size.should == 2
52
+ rows.each do |row|
53
+ row.should be_an_instance_of Stargate::Model::Row
54
+ ["row1", "row2"].should include(row.name)
55
+ end
56
+
57
+ @client.close_scanner(scanner).should be_true
58
+ end
59
+
60
+ it "should return all rows when given a batch size larger than the number of rows" do
61
+ scanner = @client.open_scanner("test-hbase-stargate", {:batch => 5})
62
+
63
+ rows = @client.get_rows(scanner)
64
+ rows.size.should == 3
65
+ rows.each do |row|
66
+ row.should be_an_instance_of Stargate::Model::Row
67
+ ["row1", "row2", "row3"].should include(row.name)
68
+ end
69
+
70
+ @client.close_scanner(scanner).should be_true
71
+ end
72
+
73
+ it "should scan the correct row when given a start_row" do
74
+ scanner = @client.open_scanner("test-hbase-stargate", {:start_row => "row2", :batch => 5})
75
+
76
+ rows = @client.get_rows(scanner)
77
+ rows.size.should == 2
78
+ rows.each do |row|
79
+ row.should be_an_instance_of Stargate::Model::Row
80
+ ["row2", "row3"].should include(row.name)
81
+ end
82
+
83
+ @client.close_scanner(scanner).should be_true
84
+ end
85
+
86
+ it "should scan the correct row when given an end_row" do
87
+ # The end_row defined is exclusive, i.e. end_row does not get returned in scanner
88
+ scanner = @client.open_scanner("test-hbase-stargate", {:end_row => "row3", :batch => 5})
89
+
90
+ rows = @client.get_rows(scanner)
91
+ rows.size.should == 2
92
+ rows.each do |row|
93
+ row.should be_an_instance_of Stargate::Model::Row
94
+ ["row1", "row2"].should include(row.name)
95
+ end
96
+
97
+ @client.close_scanner(scanner).should be_true
98
+ end
99
+
100
+ after :all do
101
+ @client.destroy_table("test-hbase-stargate")
102
+ end
103
+ end
@@ -0,0 +1,43 @@
1
+ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
2
+
3
+ describe Stargate::Operation::TableOperation do
4
+ before :all do
5
+ url = ENV["STARGATE_URL"].nil? ? "http://localhost:8080" : ENV["STARGATE_URL"]
6
+ @client = Stargate::Client.new(url)
7
+ end
8
+
9
+ it "should create a table called test-hbase-stargate" do
10
+ table = @client.create_table('test-hbase-stargate', { :name => 'habbit',
11
+ :max_version => 3,
12
+ :compression => Stargate::Model::CompressionType::NONE,
13
+ :in_memory => false,
14
+ :block_cache => false,
15
+ :ttl => -1,
16
+ :max_cell_size => 2147483647,
17
+ :bloomfilter => false
18
+ })
19
+ table.should.is_a? Stargate::Model::TableDescriptor
20
+ end
21
+
22
+ it "should show the table info of 'test-hbase-stargate'" do
23
+ table = @client.show_table('test-hbase-stargate')
24
+ table.should.is_a? Stargate::Model::TableDescriptor
25
+ table.name.should == "test-hbase-stargate"
26
+ table.column_families.should respond_to(:each)
27
+ table.column_families.map(&:name).should include("habbit")
28
+ table.column_families.each do |cf|
29
+ cf.should.is_a? Stargate::Model::ColumnDescriptor
30
+ end
31
+ end
32
+
33
+ it "should delete the table 'test-hbase-stargate'" do
34
+ lambda {
35
+ table = @client.destroy_table("test-hbase-stargate")
36
+ }.should_not raise_error
37
+
38
+ lambda {
39
+ table = @client.show_table("test-hbase-stargate")
40
+ }.should raise_error(Stargate::TableNotFoundError)
41
+ end
42
+
43
+ end
@@ -0,0 +1,20 @@
1
+ require File.join(File.dirname(__FILE__), "..", "spec_helper")
2
+
3
+ describe Stargate::Model::Record do
4
+
5
+ describe "Init" do
6
+
7
+ it "should be successfuly when has the such instance variable" do
8
+ class Stargate::Model::Record ; attr_accessor :a ; end
9
+ record = Stargate::Model::Record.new({:a => 1})
10
+ record.a.should == 1
11
+ end
12
+
13
+ it "should be not set the attribute when no such instance variable" do
14
+ record = Stargate::Model::Record.new({:a => 1})
15
+ lambda{record.sent(:a)}.should raise_error NoMethodError
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,10 @@
1
+ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
2
+
3
+ describe Stargate::Request::MetaRequest do
4
+
5
+ it "should get create_table path correctly" do
6
+ mr = Stargate::Request::MetaRequest.new
7
+ mr.create_table.should == "/tables"
8
+ end
9
+
10
+ end
@@ -0,0 +1,4 @@
1
+ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
2
+
3
+ describe Stargate::Request::RowRequest do
4
+ end
@@ -0,0 +1,4 @@
1
+ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
2
+
3
+ describe Stargate::Request::ScannerRequest do
4
+ end
@@ -0,0 +1,4 @@
1
+ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
2
+
3
+ describe Stargate::Request::TableRequest do
4
+ end