Review Board 1.7.22


HBASE-5548 [shell]: Add ability to get table reference

Review Request #4668 - Created April 6, 2012 and submitted

Jesse Yates
0.96.0, 0.94.1
HBASE-5548
Reviewers
hbase
stack
hbase-git
Adding the ability to get a reference to a table in the shell. 

Previously, all commands that acted on a table would need to take the name of the table as a string, which is annoying in an OO REPL. This patch introduces the ability to get and hold a  reference to a table both on creation (via create(...)) and at will (via get_table(...)).

Further, to actually make the table useful, modifications to table specific class were made so you can have a reference and just do things like put, scan, get, etc. on that table reference. To accommodate new table functionality, table specific methods are easily added (one line) in a dynamic fashion via class methods in the Table. See examples in get, put, scan, etc.. 

There is also a lot of admin functionality tied to a table - things like disabling, dropping, describing, etc - that were added to the table class. Now you can do things like 'table.disable' and 'table.describe'. Again these were dynamically added, so new admin functionality for a table is as simple as adding the method name to one line in the Table class.  
Manual and basic unit tests.
src/main/ruby/shell.rb
Revision 53f3de8 New Change
[20] 77 lines
[+20] [+] def initialize(hbase, formatter)
78
    def hbase_admin
78
    def hbase_admin
79
      @hbase_admin ||= hbase.admin(formatter)
79
      @hbase_admin ||= hbase.admin(formatter)
80
    end
80
    end
81

    
   
81

   
82
    def hbase_table(name)
82
    def hbase_table(name)
83
      hbase.table(name, formatter)
83
      hbase.table(name, self)
84
    end
84
    end
85

    
   
85

   
86
    def hbase_replication_admin
86
    def hbase_replication_admin
87
      @hbase_replication_admin ||= hbase.replication_admin(formatter)
87
      @hbase_replication_admin ||= hbase.replication_admin(formatter)
88
    end
88
    end
89

    
   
89

   
90
    def hbase_security_admin
90
    def hbase_security_admin
91
      @hbase_security_admin ||= hbase.security_admin(formatter)
91
      @hbase_security_admin ||= hbase.security_admin(formatter)
92
    end
92
    end
93

    
   
93

   
94
    def export_commands(where)
94
    def export_commands(where)
95
      ::Shell.commands.keys.each do |cmd|
95
      ::Shell.commands.keys.each do |cmd|

    
   
96
        # here where is the IRB namespace

    
   
97
        # this method just adds the call to the specified command

    
   
98
        # which just references back to 'this' shell object

    
   
99
        # a decently extensible way to add commands
96
        where.send :instance_eval, <<-EOF
100
        where.send :instance_eval, <<-EOF
97
          def #{cmd}(*args)
101
          def #{cmd}(*args)
98
            @shell.command('#{cmd}', *args)
102
            ret = @shell.command('#{cmd}', *args)
99
            puts
103
            puts

    
   
104
            return ret
100
          end
105
          end
101
        EOF
106
        EOF
102
      end
107
      end
103
    end
108
    end
104

    
   
109

   
105
    def command_instance(command)
110
    def command_instance(command)
106
      ::Shell.commands[command.to_s].new(self)
111
      ::Shell.commands[command.to_s].new(self)
107
    end
112
    end
108

    
   
113

   

    
   
114
    #call the method 'command' on the specified command
109
    def command(command, *args)
115
    def command(command, *args)
110
      command_instance(command).command_safe(self.debug, *args)
116
      internal_command(command, :command, *args)

    
   
117
    end

    
   
118

   

    
   
119
    #call a specific internal method in the command instance

    
   
120
    # command  - name of the command to call

    
   
121
    # method_name - name of the method on the command to call. Defaults to just 'command'

    
   
122
    # args - to be passed to the named method

    
   
123
    def internal_command(command, method_name= :command, *args)

    
   
124
      command_instance(command).command_safe(self.debug,method_name, *args)
111
    end
125
    end
112

    
   
126

   
113
    def print_banner
127
    def print_banner
114
      puts "HBase Shell; enter 'help<RETURN>' for list of supported commands."
128
      puts "HBase Shell; enter 'help<RETURN>' for list of supported commands."
115
      puts 'Type "exit<RETURN>" to leave the HBase Shell'
129
      puts 'Type "exit<RETURN>" to leave the HBase Shell'
[+20] [20] 93 lines
[+20] [+] def help_footer
209
  'general',
223
  'general',
210
  :full_name => 'GENERAL HBASE SHELL COMMANDS',
224
  :full_name => 'GENERAL HBASE SHELL COMMANDS',
211
  :commands => %w[
225
  :commands => %w[
212
    status
226
    status
213
    version
227
    version

    
   
228
    table_help
214
  ]
229
  ]
215
)
230
)
216

    
   
231

   
217
Shell.load_command_group(
232
Shell.load_command_group(
218
  'ddl',
233
  'ddl',
[+20] [20] 13 lines
[+20] def help_footer
232
    exists
247
    exists
233
    list
248
    list
234
    show_filters
249
    show_filters
235
    alter_status
250
    alter_status
236
    alter_async
251
    alter_async

    
   
252
    get_table
237
  ]
253
  ]
238
)
254
)
239

    
   
255

   
240
Shell.load_command_group(
256
Shell.load_command_group(
241
  'dml',
257
  'dml',
[+20] [20] 60 lines
src/main/ruby/hbase/hbase.rb
Revision 2c37840 New Change
 
src/main/ruby/hbase/table.rb
Revision 41dcf21 New Change
 
src/main/ruby/shell/commands.rb
Revision af6df33 New Change
 
src/main/ruby/shell/commands/count.rb
Revision 6596441 New Change
 
src/main/ruby/shell/commands/create.rb
Revision 14c1b0f New Change
 
src/main/ruby/shell/commands/delete.rb
Revision 12bc405 New Change
 
src/main/ruby/shell/commands/deleteall.rb
Revision 5731b60 New Change
 
src/main/ruby/shell/commands/get.rb
Revision 754c3d6 New Change
 
src/main/ruby/shell/commands/get_counter.rb
Revision 3cbe226 New Change
 
src/main/ruby/shell/commands/get_table.rb
New File
 
src/main/ruby/shell/commands/incr.rb
Revision 38a2fc5 New Change
 
src/main/ruby/shell/commands/put.rb
Revision dde0433 New Change
 
src/main/ruby/shell/commands/scan.rb
Revision e58aaac New Change
 
src/main/ruby/shell/commands/table_help.rb
New File
 
src/test/ruby/hbase/admin_test.rb
Revision 0c2672b New Change
 
  1. src/main/ruby/shell.rb: Loading...
  2. src/main/ruby/hbase/hbase.rb: Loading...
  3. src/main/ruby/hbase/table.rb: Loading...
  4. src/main/ruby/shell/commands.rb: Loading...
  5. src/main/ruby/shell/commands/count.rb: Loading...
  6. src/main/ruby/shell/commands/create.rb: Loading...
  7. src/main/ruby/shell/commands/delete.rb: Loading...
  8. src/main/ruby/shell/commands/deleteall.rb: Loading...
  9. src/main/ruby/shell/commands/get.rb: Loading...
  10. src/main/ruby/shell/commands/get_counter.rb: Loading...
  11. src/main/ruby/shell/commands/get_table.rb: Loading...
  12. src/main/ruby/shell/commands/incr.rb: Loading...
  13. src/main/ruby/shell/commands/put.rb: Loading...
  14. src/main/ruby/shell/commands/scan.rb: Loading...
  15. src/main/ruby/shell/commands/table_help.rb: Loading...
  16. src/test/ruby/hbase/admin_test.rb: Loading...