hbase-jruby 0.1.1-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,40 @@
1
+ <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3
+ <modelVersion>4.0.0</modelVersion>
4
+
5
+ <groupId>com.daumcorp.datatech.hbase.client</groupId>
6
+ <artifactId>hbase-project</artifactId>
7
+ <version>cdh3u5</version>
8
+ <packaging>jar</packaging>
9
+
10
+ <name>hbase-jruby</name>
11
+
12
+ <properties>
13
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14
+ <hbase.version>0.90.6-cdh3u5</hbase.version>
15
+ <hadoop.version>0.20.2-cdh3u5</hadoop.version>
16
+ </properties>
17
+
18
+ <repositories>
19
+ <repository>
20
+ <id>cloudera-releases</id>
21
+ <url>https://repository.cloudera.com/artifactory/cloudera-repos</url>
22
+ <releases>
23
+ <enabled>true</enabled>
24
+ </releases>
25
+ <snapshots>
26
+ <enabled>false</enabled>
27
+ </snapshots>
28
+ </repository>
29
+ </repositories>
30
+
31
+ <dependencies>
32
+ <dependency>
33
+ <groupId>org.apache.hbase</groupId>
34
+ <artifactId>hbase</artifactId>
35
+ <version>${hbase.version}</version>
36
+ <scope>compile</scope>
37
+ </dependency>
38
+ </dependencies>
39
+ </project>
40
+
@@ -0,0 +1,47 @@
1
+ <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3
+ <modelVersion>4.0.0</modelVersion>
4
+
5
+ <groupId>com.daumcorp.datatech.hbase.client</groupId>
6
+ <artifactId>hbase-project</artifactId>
7
+ <version>cdh4</version>
8
+ <packaging>jar</packaging>
9
+
10
+ <name>hbase-jruby</name>
11
+
12
+ <properties>
13
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14
+ <hadoop.version>2.0.0-cdh4.1.2</hadoop.version>
15
+ <hbase.version>0.92.1-cdh4.1.2</hbase.version>
16
+ </properties>
17
+
18
+ <repositories>
19
+ <repository>
20
+ <id>cloudera-releases</id>
21
+ <url>https://repository.cloudera.com/artifactory/cloudera-repos</url>
22
+ <releases>
23
+ <enabled>true</enabled>
24
+ </releases>
25
+ <snapshots>
26
+ <enabled>false</enabled>
27
+ </snapshots>
28
+ </repository>
29
+ </repositories>
30
+
31
+ <dependencies>
32
+ <dependency>
33
+ <groupId>org.apache.hadoop</groupId>
34
+ <artifactId>hadoop-common</artifactId>
35
+ <version>${hadoop.version}</version>
36
+ <scope>compile</scope>
37
+ </dependency>
38
+
39
+ <dependency>
40
+ <groupId>org.apache.hbase</groupId>
41
+ <artifactId>hbase</artifactId>
42
+ <version>${hbase.version}</version>
43
+ <scope>compile</scope>
44
+ </dependency>
45
+ </dependencies>
46
+ </project>
47
+
@@ -0,0 +1,382 @@
1
+ require 'bigdecimal'
2
+
3
+ # Represents a row returned by HBase
4
+ class HBase
5
+ # @author Junegunn Choi <junegunn.c@gmail.com>
6
+ class Result
7
+ include Enumerable
8
+
9
+ # Returns the rowkey of the row
10
+ # @param [Symbol] type The type of the rowkey
11
+ # Can be one of :string, :symbol, :fixnum, :float, :bignum, :bigdecimal, :boolean and :raw.
12
+ # @return [String, byte[]]
13
+ def rowkey type = :string
14
+ Util.from_bytes type, @result.getRow
15
+ end
16
+
17
+ # Enumerates through cells
18
+ def each
19
+ if block_given?
20
+ @result.raw.each do |kv|
21
+ yield Cell.new(kv)
22
+ end
23
+ else
24
+ self
25
+ end
26
+ end
27
+
28
+ # Returns Hash representation of the row.
29
+ # @param [Hash] schema Schema used to parse byte arrays (column family, qualifier and the value)
30
+ # @return [Hash] Hash representation of the row indexed by ColumnKey
31
+ def to_hash schema = {}
32
+ schema = parse_schema schema
33
+
34
+ HASH_TEMPLATE.clone.tap { |ret|
35
+ @result.getNoVersionMap.each do |cf, cqmap|
36
+ cqmap.each do |cq, val|
37
+ name = ColumnKey.new(cf, cq)
38
+ type = schema[name]
39
+ ret[name] = type ? Util.from_bytes(type, val) : val
40
+ end
41
+ end
42
+ }
43
+ end
44
+
45
+ # Returns Hash representation of the row.
46
+ # Each column value again is represented as a Hash indexed by timestamp of each version.
47
+ # @param [Hash] schema Schema used to parse byte arrays (column family, qualifier and the value)
48
+ # @return [Hash<Hash>] Hash representation of the row indexed by ColumnKey
49
+ def to_hash_with_versions schema = {}
50
+ schema = parse_schema schema
51
+
52
+ HASH_TEMPLATE.clone.tap { |ret|
53
+ @result.getMap.each do |cf, cqmap|
54
+ cqmap.each do |cq, tsmap|
55
+ name = ColumnKey.new(cf, cq)
56
+ type = schema[name]
57
+
58
+ ret[name] =
59
+ Hash[
60
+ tsmap.map { |ts, val|
61
+ [ ts, type ? Util.from_bytes(type, val) : val ]
62
+ }
63
+ ]
64
+ end
65
+ end
66
+ }
67
+ end
68
+
69
+ # @overload raw(column)
70
+ # Returns the latest column value as a byte array
71
+ # @param [String, HBase::ColumnKey] column "FAMILY:QUALIFIER" expression or ColumnKey object.
72
+ # @return [byte[]] Byte array representation of the latest value
73
+ # @overload raw(columns)
74
+ # For each column specified,
75
+ # returns the latest column value as a byte array
76
+ # @param [<String|HBase::ColumnKey>] column "FAMILY:QUALIFIER" expression or ColumnKey object.
77
+ # @return [Array<byte[]>] Byte array representations of the latest values
78
+ def raw cols
79
+ ret = get_values [*cols]
80
+
81
+ case cols
82
+ when Array
83
+ ret
84
+ else
85
+ ret.first
86
+ end
87
+ end
88
+
89
+ # @overload raws(column)
90
+ # Returns all versions of column values as byte arrays in a Hash indexed by their timestamps
91
+ # @param [String, HBase::ColumnKey] column "FAMILY:QUALIFIER" expression or ColumnKey object.
92
+ # @return [Hash<Fixnum, byte[]>]
93
+ # @overload raws(columns)
94
+ # For each column specified,
95
+ # returns all versions of column values as byte arrays in a Hash indexed by their timestamps
96
+ # @param [Array<String|HBase::ColumnKey>] columns Array of "FAMILY:QUALIFIER" expressions and ColumnKey objects.
97
+ # @return [Array<Hash<Fixnum, byte[]>>]
98
+ def raws cols
99
+ ret = get_values [*cols], true
100
+
101
+ case cols
102
+ when Array
103
+ ret
104
+ else
105
+ ret.first
106
+ end
107
+ end
108
+
109
+ # @overload string(column)
110
+ # Returns the latest column value as a String
111
+ # @param [String, HBase::ColumnKey] column "FAMILY:QUALIFIER" expression or ColumnKey object.
112
+ # @return [String]
113
+ # @overload string(columns)
114
+ # For each column specified,
115
+ # returns the latest column value as a String
116
+ # @param [Array<String|HBase::ColumnKey>] columns Array of "FAMILY:QUALIFIER" expressions and ColumnKey objects.
117
+ # @return [Array<String>]
118
+ def string cols
119
+ decode_values :string, cols
120
+ end
121
+ alias str string
122
+
123
+ # @overload strings(column)
124
+ # Returns all versions of column values as Strings in a Hash indexed by their timestamps
125
+ # @param [String, HBase::ColumnKey] column "FAMILY:QUALIFIER" expression or ColumnKey object.
126
+ # @return [Hash<Fixnum, String>]
127
+ # @overload strings(columns)
128
+ # For each column specified,
129
+ # returns all versions of column values as Strings in a Hash indexed by their timestamps
130
+ # @param [Array<String|HBase::ColumnKey>] columns Array of "FAMILY:QUALIFIER" expressions and ColumnKey objects.
131
+ # @return [Array<Hash<Fixnum, String>>]
132
+ def strings cols
133
+ decode_values :string, cols, true
134
+ end
135
+ alias strs strings
136
+
137
+ # @overload symbol(column)
138
+ # Returns the latest column value as a Symbol
139
+ # @param [String, HBase::ColumnKey] column "FAMILY:QUALIFIER" expression or ColumnKey object.
140
+ # @return [Symbol]
141
+ # @overload symbol(columns)
142
+ # For each column specified,
143
+ # returns the latest column values as a Symbol
144
+ # @param [Array<String|HBase::ColumnKey>] columns Array of "FAMILY:QUALIFIER" expressions and ColumnKey objects.
145
+ # @return [Array<Symbol>]
146
+ def symbol cols
147
+ decode_values :symbol, cols
148
+ end
149
+ alias sym symbol
150
+
151
+ # @overload symbols(column)
152
+ # Returns all versions of column values as Symbols in a Hash indexed by their timestamps
153
+ # @param [String, HBase::ColumnKey] column "FAMILY:QUALIFIER" expression or ColumnKey object.
154
+ # @return [Hash<Fixnum, Symbol>]
155
+ # @overload symbols(columns)
156
+ # For each column specified,
157
+ # returns all versions of column values as Symbols in a Hash indexed by their timestamps
158
+ # @param [Array<String|HBase::ColumnKey>] columns Array of "FAMILY:QUALIFIER" expressions and ColumnKey objects.
159
+ # @return [Array<Hash<Fixnum, Symbol>>]
160
+ def symbols cols
161
+ decode_values :symbol, cols, true
162
+ end
163
+ alias syms symbols
164
+
165
+ # @overload fixnum(column)
166
+ # Returns the latest column value as a Fixnum
167
+ # @param [String, HBase::ColumnKey] column "FAMILY:QUALIFIER" expression or ColumnKey object.
168
+ # @return [Fixnum]
169
+ # @overload fixnum(columns)
170
+ # For each column specified,
171
+ # returns the latest column values as a Fixnum
172
+ # @param [Array<String|HBase::ColumnKey>] columns Array of "FAMILY:QUALIFIER" expressions and ColumnKey objects.
173
+ # @return [Array<Fixnum>]
174
+ def fixnum cols
175
+ decode_values :fixnum, cols
176
+ end
177
+ alias integer fixnum
178
+ alias int fixnum
179
+
180
+ # @overload fixnums(column)
181
+ # Returns all versions of column values as Fixnums in a Hash indexed by their timestamps
182
+ # @param [String, HBase::ColumnKey] column "FAMILY:QUALIFIER" expression or ColumnKey object.
183
+ # @return [Hash<Fixnum, Fixnum>]
184
+ # @overload fixnums(columns)
185
+ # For each column specified,
186
+ # returns all versions of column values as Fixnums in a Hash indexed by their timestamps
187
+ # @param [Array<String|HBase::ColumnKey>] columns Array of "FAMILY:QUALIFIER" expressions and ColumnKey objects.
188
+ # @return [Array<Hash<Fixnum, Fixnum>>]
189
+ def fixnums cols
190
+ decode_values :fixnum, cols, true
191
+ end
192
+ alias integers fixnums
193
+ alias ints fixnums
194
+
195
+ # @overload bignum(column)
196
+ # Returns the latest column value as a Bignum
197
+ # @param [String, HBase::ColumnKey] column "FAMILY:QUALIFIER" expression or ColumnKey object.
198
+ # @return [Bignum]
199
+ # @overload bignum(columns)
200
+ # For each column specified,
201
+ # returns the latest column values as a Bignum
202
+ # @param [Array<String|HBase::ColumnKey>] columns Array of "FAMILY:QUALIFIER" expressions and ColumnKey objects.
203
+ # @return [Array<Bignum>]
204
+ def bignum cols
205
+ decode_values :bignum, cols
206
+ end
207
+ alias biginteger bignum
208
+ alias bigint bignum
209
+
210
+ # @overload bignums(column)
211
+ # Returns all versions of column values as Bignums in a Hash indexed by their timestamps
212
+ # @param [String, HBase::ColumnKey] column "FAMILY:QUALIFIER" expression or ColumnKey object.
213
+ # @return [Hash<Fixnum, Bignum>]
214
+ # @overload bignums(columns)
215
+ # For each column specified,
216
+ # returns all versions of column values as Bignums in a Hash indexed by their timestamps
217
+ # @param [Array<String|HBase::ColumnKey>] columns Array of "FAMILY:QUALIFIER" expressions and ColumnKey objects.
218
+ # @return [Array<Hash<Fixnum, Bignum>>]
219
+ def bignums cols
220
+ decode_values :bignum, cols, true
221
+ end
222
+ alias bigintegers bignums
223
+ alias bigints bignums
224
+
225
+ # @overload bigdecimal(column)
226
+ # Returns the latest column value as a BigDecimal
227
+ # @param [String, HBase::ColumnKey] column "FAMILY:QUALIFIER" expression or ColumnKey object.
228
+ # @return [BigDecimal]
229
+ # @overload bigdecimal(columns)
230
+ # For each column specified,
231
+ # returns the latest column values as a BigDecimal
232
+ # @param [Array<String|HBase::ColumnKey>] columns Array of "FAMILY:QUALIFIER" expressions and ColumnKey objects.
233
+ # @return [Array<BigDecimal>]
234
+ def bigdecimal cols
235
+ decode_values :bigdecimal, cols
236
+ end
237
+
238
+ # @overload bigdecimals(column)
239
+ # Returns all versions of column values as BigDecimals in a Hash indexed by their timestamps
240
+ # @param [String, HBase::ColumnKey] column "FAMILY:QUALIFIER" expression or ColumnKey object.
241
+ # @return [Hash<Fixnum, BigDecimal>]
242
+ # @overload bigdecimals(columns)
243
+ # For each column specified,
244
+ # returns all versions of column values as BigDecimals in a Hash indexed by their timestamps
245
+ # @param [Array<String|HBase::ColumnKey>] columns Array of "FAMILY:QUALIFIER" expressions and ColumnKey objects.
246
+ # @return [Array<Hash<Fixnum, BigDecimal>>]
247
+ def bigdecimals cols
248
+ decode_values :bigdecimal, cols, true
249
+ end
250
+
251
+ # @overload float(column)
252
+ # Returns the latest column value as a Float
253
+ # @param [String, HBase::ColumnKey] column "FAMILY:QUALIFIER" expression or ColumnKey object.
254
+ # @return [Float]
255
+ # @overload float(columns)
256
+ # For each column specified,
257
+ # returns the latest column values as a Float
258
+ # @param [Array<String|HBase::ColumnKey>] columns Array of "FAMILY:QUALIFIER" expressions and ColumnKey objects.
259
+ # @return [Array<Float>]
260
+ def float cols
261
+ decode_values :float, cols
262
+ end
263
+ alias double float
264
+
265
+ # @overload floats(column)
266
+ # Returns all versions of column values as Floats in a Hash indexed by their timestamps
267
+ # @param [String, HBase::ColumnKey] column "FAMILY:QUALIFIER" expression or ColumnKey object.
268
+ # @return [Hash<Fixnum, Float>]
269
+ # @overload floats(columns)
270
+ # For each column specified,
271
+ # returns all versions of column values as Floats in a Hash indexed by their timestamps
272
+ # @param [Array<String|HBase::ColumnKey>] columns Array of "FAMILY:QUALIFIER" expressions and ColumnKey objects.
273
+ # @return [Array<Hash<Fixnum, Float>>]
274
+ def floats cols
275
+ decode_values :float, cols, true
276
+ end
277
+ alias doubles floats
278
+
279
+ # @overload boolean(column)
280
+ # Returns the latest column value as a boolean value
281
+ # @param [String, HBase::ColumnKey] column "FAMILY:QUALIFIER" expression or ColumnKey object.
282
+ # @return [true, false]
283
+ # @overload boolean(columns)
284
+ # For each column specified,
285
+ # returns the latest column values as a boolean value
286
+ # @param [Array<String|HBase::ColumnKey>] columns Array of "FAMILY:QUALIFIER" expressions and ColumnKey objects.
287
+ # @return [Array<true|false>]
288
+ def boolean cols
289
+ decode_values :boolean, cols
290
+ end
291
+ alias bool boolean
292
+
293
+ # @overload booleans(column)
294
+ # Returns all versions of column values as boolean values in a Hash indexed by their timestamps
295
+ # @param [String, HBase::ColumnKey] column "FAMILY:QUALIFIER" expression or ColumnKey object.
296
+ # @return [Hash<Fixnum, true|false>]
297
+ # @overload booleans(columns)
298
+ # For each column specified,
299
+ # returns all versions of column values as boolean values in a Hash indexed by their timestamps
300
+ # @param [Array<String|HBase::ColumnKey>] columns Array of "FAMILY:QUALIFIER" expressions and ColumnKey objects.
301
+ # @return [Array<Hash<Fixnum, true|false>>]
302
+ def booleans cols
303
+ decode_values :boolean, cols, true
304
+ end
305
+ alias bools booleans
306
+
307
+ private
308
+ HASH_TEMPLATE = {}.tap { |h|
309
+ h.instance_eval do
310
+ def [] key
311
+ ck =
312
+ case key
313
+ when ColumnKey
314
+ key
315
+ else
316
+ cf, cq = Util.parse_column_name key
317
+ ColumnKey.new(cf, cq)
318
+ end
319
+ super ck
320
+ end
321
+ end
322
+ }
323
+
324
+ def get_values cols, with_versions = false
325
+ raise ArgumentError, "No column expressions specified" if cols.empty?
326
+ cols.map { |col|
327
+ cf, cq = Util.parse_column_name(col)
328
+ if with_versions
329
+ Hash[ allmap[cf][cq] ]
330
+ else
331
+ @result.getValue cf, cq
332
+ end
333
+ }
334
+ end
335
+
336
+ def decode_values type, cols, with_versions = false
337
+ ret = get_values([*cols], with_versions).map { |v|
338
+ if with_versions
339
+ v.each do |k, raw|
340
+ v[k] = Util.from_bytes type, raw
341
+ end
342
+ v
343
+ else
344
+ Util.from_bytes type, v
345
+ end
346
+ }
347
+ case cols
348
+ when Array
349
+ ret
350
+ else
351
+ ret.first
352
+ end
353
+ end
354
+
355
+ # @param [org.apache.hadoop.hbase.client.Result] java_result
356
+ def initialize java_result
357
+ @result = java_result
358
+ @allmap = nil
359
+ end
360
+
361
+ def allmap
362
+ @allmap ||= @result.getMap
363
+ end
364
+
365
+ def parse_schema schema
366
+ {}.tap { |ret|
367
+ schema.each do |name, type|
368
+ ck =
369
+ case name
370
+ when ColumnKey
371
+ name
372
+ else
373
+ cf, cq = Util.parse_column_name(name)
374
+ ColumnKey.new(cf, cq)
375
+ end
376
+ ret[ck] = type
377
+ end
378
+ }
379
+ end
380
+ end#Result
381
+ end#HBase
382
+