Index: hbase-server/src/main/ruby/hbase/table.rb =================================================================== --- hbase-server/src/main/ruby/hbase/table.rb (revision 1380945) +++ hbase-server/src/main/ruby/hbase/table.rb (working copy) @@ -114,6 +114,7 @@ @table = org.apache.hadoop.hbase.client.HTable.new(configuration, table_name) @name = table_name @shell = shell + @converters = Hash.new() end # Note the below methods are prefixed with '_' to hide them from the average user, as @@ -188,7 +189,8 @@ def _get_internal(row, *args) get = org.apache.hadoop.hbase.client.Get.new(row.to_s.to_java_bytes) maxlength = -1 - + @converters.clear() + # Normalize args args = args.first if args.first.kind_of?(Hash) if args.kind_of?(String) || args.kind_of?(Array) @@ -300,6 +302,7 @@ limit = args.delete("LIMIT") || -1 maxlength = args.delete("MAXLENGTH") || -1 + @converters.clear() if args.any? filter = args["FILTER"] @@ -451,6 +454,7 @@ # Returns family and (when has it) qualifier for a column name def parse_column_name(column) split = org.apache.hadoop.hbase.KeyValue.parseColumn(column.to_java_bytes) + set_converter(split) if split.length > 1 return split[0], (split.length > 1) ? split[1] : nil end @@ -475,9 +479,38 @@ if kv.isDelete val = "timestamp=#{kv.getTimestamp}, type=#{org.apache.hadoop.hbase.KeyValue::Type::codeToType(kv.getType)}" else - val = "timestamp=#{kv.getTimestamp}, value=#{org.apache.hadoop.hbase.util.Bytes::toStringBinary(kv.getValue)}" + val = "timestamp=#{kv.getTimestamp}, value=#{convert(column, kv)}" end (maxlength != -1) ? val[0, maxlength] : val end + + def convert(column, kv) + #use org.apache.hadoop.hbase.util.Bytes as the default class + klazz_name = 'org.apache.hadoop.hbase.util.Bytes' + #use org.apache.hadoop.hbase.util.Bytes::toStringBinary as the default convertor + converter = 'toStringBinary' + if @converters.has_key?(column) + matches = /c\((.+)\)\.(.+)/.match(@converters[column]) + if matches.nil? + # cannot match the pattern of 'c(className).functionname' + # use the default klazz_name + converter = @converters[column] + else + klazz_name = matches[1] + converter = matches[2] + end + end + method = eval(klazz_name).method(converter) + return method.call(kv.getValue) # apply the converter + end + + def set_converter(column) + family = String.from_java_bytes(column[0]) + parts = org.apache.hadoop.hbase.KeyValue.parseColumn(column[1]) + if parts.length > 1 + @converters["#{family}:#{String.from_java_bytes(parts[0])}"] = String.from_java_bytes(parts[1]) + column[1] = parts[0] + end + end end end Index: hbase-server/src/main/ruby/shell/commands/get.rb =================================================================== --- hbase-server/src/main/ruby/shell/commands/get.rb (revision 1380945) +++ hbase-server/src/main/ruby/shell/commands/get.rb (working copy) @@ -30,6 +30,7 @@ hbase> get 't1', 'r1', {TIMERANGE => [ts1, ts2]} hbase> get 't1', 'r1', {COLUMN => 'c1'} hbase> get 't1', 'r1', {COLUMN => ['c1', 'c2', 'c3']} + hbase> get 't1', 'r1', {COLUMN => ['c1:toInt', 'c2:c(org.apache.hadoop.hbase.util.Bytes).toInt'] } hbase> get 't1', 'r1', {COLUMN => 'c1', TIMESTAMP => ts1} hbase> get 't1', 'r1', {COLUMN => 'c1', TIMERANGE => [ts1, ts2], VERSIONS => 4} hbase> get 't1', 'r1', {COLUMN => 'c1', TIMESTAMP => ts1, VERSIONS => 4} Index: hbase-server/src/main/ruby/shell/commands/scan.rb =================================================================== --- hbase-server/src/main/ruby/shell/commands/scan.rb (revision 1380945) +++ hbase-server/src/main/ruby/shell/commands/scan.rb (working copy) @@ -42,6 +42,7 @@ hbase> scan '.META.' hbase> scan '.META.', {COLUMNS => 'info:regioninfo'} hbase> scan 't1', {COLUMNS => ['c1', 'c2'], LIMIT => 10, STARTROW => 'xyz'} + hbase> scan 't1', {COLUMNS => ['c1:toInt', 'c2:c(org.apache.hadoop.hbase.util.Bytes).toInt'] } hbase> scan 't1', {COLUMNS => 'c1', TIMERANGE => [1303668804, 1303668904]} hbase> scan 't1', {FILTER => "(PrefixFilter ('row2') AND (QualifierFilter (>=, 'binary:xyz'))) AND (TimestampsFilter ( 123, 456))"} hbase> scan 't1', {FILTER => org.apache.hadoop.hbase.filter.ColumnPaginationFilter.new(1, 0)} Index: hbase-server/src/test/ruby/hbase/table_test.rb =================================================================== --- hbase-server/src/test/ruby/hbase/table_test.rb (revision 1380945) +++ hbase-server/src/test/ruby/hbase/table_test.rb (working copy) @@ -312,6 +312,21 @@ @test_table._get_internal('1') { |col, val| res[col] = val } assert_equal(res.keys.sort, [ 'x:a', 'x:b' ]) end + + define_test "get should support COLUMNS with format information" do + @test_table.put(1, "x:c", [1024].pack('N')) + @test_table.put(1, "x:d", [98].pack('N')) + begin + res = @test_table._get_internal('1', ['x:c:toInt'], ['x:d:c(org.apache.hadoop.hbase.util.Bytes).toInt']) + assert_not_nil(res) + assert_kind_of(Hash, res) + assert_not_nil(/value=1024/.match(res['x:c'])) + assert_not_nil(/value=98/.match(res['x:d'])) + ensure + @test_table.delete(1, "x:c") + @test_table.delete(1, "x:d") + end + end #-------------------------------------------------------------------------------