hbaserb 0.0.1

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.
@@ -0,0 +1,37 @@
1
+ module HBaseRb
2
+
3
+ class Client
4
+ def initialize(server, port=9090)
5
+ socket = Thrift::Socket.new(server, port)
6
+ @transport = Thrift::BufferedTransport.new(socket)
7
+ @transport.open
8
+ protocol = Thrift::BinaryProtocol.new(@transport)
9
+ @client = Apache::Hadoop::Hbase::Thrift::Hbase::Client.new(protocol)
10
+ end
11
+
12
+ def table_names
13
+ @client.getTableNames
14
+ end
15
+
16
+ def has_table?(name)
17
+ table_names.include? name
18
+ end
19
+
20
+ def get_table(tablename)
21
+ raise HBaseRb::NoSuchTable if not has_table? tablename
22
+ HBaseRb::Table.new @client, tablename
23
+ end
24
+
25
+ def create_table(tablename, *column_family_names)
26
+ column_family_names.map! { |name| Apache::Hadoop::Hbase::Thrift::ColumnDescriptor.new(:name => name) }
27
+ @client.createTable tablename, column_family_names
28
+ get_table tablename
29
+ end
30
+
31
+ def close
32
+ @transport.close
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,3 @@
1
+ module HBaseRb
2
+ class NoSuchTable < StandardError; end
3
+ end
@@ -0,0 +1,39 @@
1
+ module Apache
2
+ module Hadoop
3
+ module Hbase
4
+ module Thrift
5
+
6
+
7
+ class TCell
8
+ def to_i16
9
+ val, = value.unpack('n')
10
+ (val > 0x7fff) ? (0 - ((val - 1) ^ 0xffff)) : val
11
+ end
12
+
13
+ def to_i32
14
+ val, = value.unpack('N')
15
+ (val > 0x7fffffff) ? (0 - ((val - 1) ^ 0xffffffff)) : val
16
+ end
17
+
18
+ def to_i64
19
+ hi, lo = value.unpack('N2')
20
+ if (hi > 0x7fffffff)
21
+ hi ^= 0xffffffff
22
+ lo ^= 0xffffffff
23
+ 0 - (hi << 32) - lo - 1
24
+ else
25
+ (hi << 32) + lo
26
+ end
27
+ end
28
+
29
+ def to_double
30
+ value.unpack('G').first
31
+ end
32
+
33
+ end
34
+
35
+
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,39 @@
1
+ module HBaseRb
2
+
3
+ class Scanner
4
+ def initialize(client, scanner_id)
5
+ @client = client
6
+ @sid = scanner_id
7
+ if block_given?
8
+ n = next_row
9
+ while n.length > 0
10
+ yield n.first
11
+ n = next_row
12
+ end
13
+ close
14
+ end
15
+ end
16
+
17
+ def next_row
18
+ call :scannerGet
19
+ end
20
+
21
+ def close
22
+ call :scannerClose
23
+ end
24
+
25
+ def each
26
+ n = next_row
27
+ while n.length > 0
28
+ yield n.first
29
+ n = next_row
30
+ end
31
+ end
32
+
33
+ private
34
+ def call(method, *args)
35
+ @client.send method, @sid, *args
36
+ end
37
+ end
38
+
39
+ end
@@ -0,0 +1,78 @@
1
+ module HBaseRb
2
+
3
+ class Table
4
+ def initialize(client, name)
5
+ @client = client
6
+ @name = name
7
+ end
8
+
9
+ def column_families
10
+ call :getColumnDescriptors
11
+ end
12
+
13
+ def regions
14
+ call :getTableRegions
15
+ end
16
+
17
+ def get(row, column)
18
+ call :get, row.to_s, column
19
+ end
20
+
21
+ # get the last value for the given row / column
22
+ def get_last(row, column, default=nil)
23
+ r = get(row, column)
24
+ (r.length > 0) ? r.first.value : default
25
+ end
26
+
27
+ def get_row(row)
28
+ call :getRow, row.to_s
29
+ end
30
+
31
+ def delete
32
+ call :disableTable
33
+ call :deleteTable
34
+ end
35
+
36
+ def delete_row(row)
37
+ call :deleteAllRow, row.to_s
38
+ end
39
+
40
+ def delete_cells(row, column)
41
+ call :deleteAll, row.to_s, column
42
+ end
43
+
44
+ def atomic_increment(row, column, value=1)
45
+ call :atomicIncrement, row.to_s, column, value
46
+ end
47
+
48
+ # pass in no params to scan whole table
49
+ def create_scanner(row=nil, *columns, &block)
50
+ row ||= ""
51
+ columns = (columns.length > 0) ? columns : column_families.keys
52
+ sid = call :scannerOpen, row.to_s, columns
53
+ Scanner.new @client, sid, &block
54
+ end
55
+
56
+ # mutations is a key / value pair to insert / update for the given row
57
+ # the keys are in the form "family:column"
58
+ def mutate_row(row, mutations)
59
+ mutations = mutations.map { |k,v| Apache::Hadoop::Hbase::Thrift::Mutation.new(:column => k, :value => v) }
60
+ call :mutateRow, row, mutations
61
+ end
62
+
63
+ def to_s
64
+ s = ""
65
+ create_scanner { |r|
66
+ cols = r.columns.map { |k,v| "#{k}: #{v.value}" }.join(", ")
67
+ s += "#{r.row}: #{cols}\n"
68
+ }
69
+ s
70
+ end
71
+
72
+ private
73
+ def call(method, *args)
74
+ @client.send method, @name, *args
75
+ end
76
+ end
77
+
78
+ end
data/lib/hbaserb.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'hbaserb/client'
2
+ require 'hbaserb/exceptions'
3
+ require 'hbaserb/table'
4
+ require 'hbaserb/scanner'
5
+ require 'hbaserb/extensions'
6
+
7
+ $:.unshift File.join(File.dirname(__FILE__), 'thrift')
8
+ require 'hbase'
9
+ require 'hbase_constants'
10
+