diff --git hbase-shell/src/main/ruby/hbase.rb hbase-shell/src/main/ruby/hbase.rb index fd053bc..802fa02 100644 --- hbase-shell/src/main/ruby/hbase.rb +++ hbase-shell/src/main/ruby/hbase.rb @@ -59,6 +59,7 @@ module HBaseConstants NUMREGIONS = 'NUMREGIONS' CONFIGURATION = org.apache.hadoop.hbase.HConstants::CONFIGURATION ATTRIBUTES="ATTRIBUTES" + VISIBILITY="VISIBILITY" # Load constants from hbase java API def self.promote_constants(constants) diff --git hbase-shell/src/main/ruby/hbase/table.rb hbase-shell/src/main/ruby/hbase/table.rb index 3685eca..ca07ff2 100644 --- hbase-shell/src/main/ruby/hbase/table.rb +++ hbase-shell/src/main/ruby/hbase/table.rb @@ -132,12 +132,20 @@ EOF family, qualifier = parse_column_name(column) if args.any? attributes = args[ATTRIBUTES] - set_attributes(p, attributes) if attributes + set_attributes(p, attributes) if attributes + visibility = args[VISIBILITY] + set_cell_visibility(p, visibility) if visibility end #Case where attributes are specified without timestamp if timestamp.kind_of?(Hash) timestamp.each do |k, v| - set_attributes(p, v) if v + if v.kind_of?(Hash) + set_attributes(p, v) if v + end + if v.kind_of?(String) + set_cell_visibility(p, v) if v + end + end timestamp = nil end @@ -231,6 +239,7 @@ EOF maxlength = args.delete(MAXLENGTH) if args[MAXLENGTH] filter = args.delete(FILTER) if args[FILTER] attributes = args[ATTRIBUTES] + visibility = args[VISIBILITY] unless args.empty? columns = args[COLUMN] || args[COLUMNS] if args[VERSIONS] @@ -264,6 +273,8 @@ EOF else if attributes set_attributes(get, attributes) if attributes + elsif visibility + set_authorizations(scan, visibility) if visibility else # May have passed TIMESTAMP and row only; wants all columns from ts. unless ts = args[TIMESTAMP] || tr = args[TIMERANGE] @@ -276,7 +287,8 @@ EOF get.setTimeStamp(ts.to_i) if args[TIMESTAMP] get.setTimeRange(args[TIMERANGE][0], args[TIMERANGE][1]) if args[TIMERANGE] end - set_attributes(get, attributes) if attributes + set_attributes(get, attributes) if attributes + set_authorizations(scan, visibility) if visibility end unless filter.class == String @@ -351,7 +363,8 @@ EOF timerange = args[TIMERANGE] raw = args["RAW"] || false attributes = args[ATTRIBUTES] - + visibility = args[VISIBILITY] + # Normalize column names columns = [columns] if columns.class == String unless columns.kind_of?(Array) @@ -387,6 +400,7 @@ EOF scan.setTimeRange(timerange[0], timerange[1]) if timerange scan.setRaw(raw) set_attributes(scan, attributes) if attributes + set_authorizations(scan, visibility) if visibility else scan = org.apache.hadoop.hbase.client.Scan.new end @@ -437,6 +451,16 @@ EOF end end + def set_cell_visibility(oprattr, visibility) + oprattr.setCellVisibility(org.apache.hadoop.hbase.security.visibility.CellVisibility.new(visibility.to_s)) + end + + def set_authorizations(oprattr, authorizations) + raise(ArgumentError, "#{"authorizations"} must be a Array type type") unless authorizations.kind_of?(Array) + auths = [ authorizations ].flatten.compact + oprattr.setAuthorizations(oprattr, org.apache.hadoop.hbase.security.visibility.Authorizations.new(auths.to_java(:string))) + end + #---------------------------- # Add general administration utilities to the shell # each of the names below adds this method name to the table diff --git hbase-shell/src/main/ruby/shell/commands/get.rb hbase-shell/src/main/ruby/shell/commands/get.rb index 7ac4033..8ea0f5f 100644 --- hbase-shell/src/main/ruby/shell/commands/get.rb +++ hbase-shell/src/main/ruby/shell/commands/get.rb @@ -37,6 +37,7 @@ a dictionary of column(s), timestamp, timerange and versions. Examples: hbase> get 't1', 'r1', 'c1', 'c2' hbase> get 't1', 'r1', ['c1', 'c2'] hbsase> get 't1','r1', {COLUMN => 'c1', ATTRIBUTES => {'mykey'=>'myvalue'}} + hbsase> get 't1','r1', {COLUMN => 'c1', VISIBILITY => {['PRIVATE','SECRET']}} Besides the default 'toStringBinary' format, 'get' also supports custom formatting by column. A user can define a FORMATTER by adding it to the column name in the get diff --git hbase-shell/src/main/ruby/shell/commands/put.rb hbase-shell/src/main/ruby/shell/commands/put.rb index 56f8c09..312f60f 100644 --- hbase-shell/src/main/ruby/shell/commands/put.rb +++ hbase-shell/src/main/ruby/shell/commands/put.rb @@ -30,6 +30,7 @@ row 'r1' under column 'c1' marked with the time 'ts1', do: hbase> put 't1', 'r1', 'c1', 'value', ts1 hbase> put 't1', 'r1', 'c1', 'value', {ATTRIBUTES=>{'mykey'=>'myvalue'} hbase> put 't1', 'r1', 'c1', 'value', ts1, {ATTRIBUTES=>{'mykey'=>'myvalue'} + hbase> put 't1', 'r1', 'c1', 'value', ts1, {VISIBILITY=>'PRIVATE|SECRET'} The same commands also can be run on a table reference. Suppose you had a reference t to table 't1', the corresponding command would be: diff --git hbase-shell/src/main/ruby/shell/commands/scan.rb hbase-shell/src/main/ruby/shell/commands/scan.rb index 129961f..f391b2e 100644 --- hbase-shell/src/main/ruby/shell/commands/scan.rb +++ hbase-shell/src/main/ruby/shell/commands/scan.rb @@ -49,6 +49,7 @@ Some examples: org.apache.hadoop.hbase.filter.ColumnPaginationFilter.new(1, 0)} For setting the Operation Attributes hbase> scan 't1', { COLUMNS => ['c1', 'c2'], ATTRIBUTES => {'mykey' => 'myvalue'}} + hbase> scan 't1', { COLUMNS => ['c1', 'c2'], VISIBILITY => {['PRIVATE','SECRET']} For experts, there is an additional option -- CACHE_BLOCKS -- which switches block caching for the scanner on (true) or off (false). By default it is enabled. Examples: diff --git hbase-shell/src/test/ruby/hbase/table_test.rb hbase-shell/src/test/ruby/hbase/table_test.rb index 0f63476..dd51cbf 100644 --- hbase-shell/src/test/ruby/hbase/table_test.rb +++ hbase-shell/src/test/ruby/hbase/table_test.rb @@ -138,6 +138,10 @@ module Hbase define_test "put should work with attributes" do @test_table.put("123", "x:a", 4, {ATTRIBUTES=>{'mykey'=>'myvalue'}}) end + + define_test "put should work with attribute" do + @test_table.put("123", "x:a", 4, {VISIBILITY=>'mykey'}) + end #------------------------------------------------------------------------------- define_test "delete should work without timestamp" do @@ -219,6 +223,9 @@ module Hbase @test_table.put(3, "x:a", 21, {ATTRIBUTES=>{'mykey'=>'myvalue'}}) @test_table.put(3, "x:b", 22, @test_ts, {ATTRIBUTES=>{'mykey'=>'myvalue'}}) + + @test_table.put(4, "x:a", 31, {VISIBILITY=>'mykey'}) + @test_table.put(4, "x:b", 32, @test_ts, {VISIBILITY=>'mykey'}) end @@ -252,6 +259,14 @@ module Hbase assert_not_nil(res['x:a']) assert_not_nil(res['x:b']) end + + define_test "get should work for data written with Visibility" do + res = @test_table._get_internal('4') + assert_not_nil(res) + assert_kind_of(Hash, res) + assert_not_nil(res['x:a']) + assert_not_nil(res['x:b']) + end define_test "get should work with integer keys" do res = @test_table._get_internal(1)