3 # Licensed to the Apache Software Foundation (ASF) under one
4 # or more contributor license agreements. See the NOTICE file
5 # distributed with this work for additional information
6 # regarding copyright ownership. The ASF licenses this file
7 # to you under the Apache License, Version 2.0 (the
8 # "License"); you may not use this file except in compliance
9 # with the License. You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
20 # Shell commands module
28 def self.command_groups
32 def self.load_command(name, group, aliases = [])
33 return if commands[name]
35 # Register command in the group
36 raise ArgumentError, "Unknown group: #{group}" unless command_groups[group]
37 command_groups[group][:commands] << name
41 require "shell/commands/#{name}"
42 klass_name = name.to_s.gsub(/(?:^|_)(.)/) { Regexp.last_match(1).upcase } # camelize
43 commands[name] = eval("Commands::#{klass_name}")
44 aliases.each do |an_alias|
45 commands[an_alias] = commands[name]
48 raise "Can't load hbase shell command: #{name}. Error: #{e}\n#{e.backtrace.join("\n")}"
52 def self.load_command_group(group, opts)
53 raise ArgumentError, "No :commands for group #{group}" unless opts[:commands]
55 command_groups[group] = {
57 command_names: opts[:commands],
58 full_name: opts[:full_name] || group,
59 comment: opts[:comment]
62 all_aliases = opts[:aliases] || {}
64 opts[:commands].each do |command|
65 aliases = all_aliases[command] || []
66 load_command(command, group, aliases)
70 #----------------------------------------------------------------------
71 # rubocop:disable Metrics/ClassLength
74 attr_accessor :interactive
75 alias interactive? interactive
80 def initialize(hbase, interactive = true)
82 self.interactive = interactive
85 # Returns Admin class from admin.rb
87 @admin ||= hbase.admin
91 @hbase_taskmonitor ||= hbase.taskmonitor
95 hbase.table(name, self)
98 def hbase_replication_admin
99 @hbase_replication_admin ||= hbase.replication_admin
102 def hbase_security_admin
103 @hbase_security_admin ||= hbase.security_admin
106 def hbase_visibility_labels_admin
107 @hbase_visibility_labels_admin ||= hbase.visibility_labels_admin
110 def hbase_quotas_admin
111 @hbase_quotas_admin ||= hbase.quotas_admin
114 def hbase_rsgroup_admin
115 @rsgroup_admin ||= hbase.rsgroup_admin
118 def export_commands(where)
119 ::Shell.commands.keys.each do |cmd|
120 # here where is the IRB namespace
121 # this method just adds the call to the specified command
122 # which just references back to 'this' shell object
123 # a decently extensible way to add commands
124 where.send :instance_eval, <<-EOF
126 ret = @shell.command('#{cmd}', *args)
134 def command_instance(command)
135 ::Shell.commands[command.to_s].new(self)
138 # call the method 'command' on the specified command
139 def command(command, *args)
140 internal_command(command, :command, *args)
143 # call a specific internal method in the command instance
144 # command - name of the command to call
145 # method_name - name of the method on the command to call. Defaults to just 'command'
146 # args - to be passed to the named method
147 def internal_command(command, method_name = :command, *args)
148 command_instance(command).command_safe(debug, method_name, *args)
153 puts 'Use "help" to get list of supported commands.'
154 puts 'Use "exit" to quit this interactive shell.'
160 def help_multi_command(command)
161 puts "Command: #{command}"
162 puts command_instance(command).help
167 def help_command(command)
168 puts command_instance(command).help
172 def help_group(group_name)
173 group = ::Shell.command_groups[group_name.to_s]
174 group[:commands].sort.each { |cmd| help_multi_command(cmd) }
184 def help(command = nil)
186 return help_command(command) if ::Shell.commands[command.to_s]
187 return help_group(command) if ::Shell.command_groups[command.to_s]
188 puts "ERROR: Invalid command or command group name: #{command}"
194 puts 'COMMAND GROUPS:'
195 ::Shell.command_groups.each do |name, group|
196 puts ' Group name: ' + name
197 puts ' Commands: ' + group[:command_names].sort.join(', ')
208 "HBase Shell, version #{org.apache.hadoop.hbase.util.VersionInfo.getVersion}, " \
209 "r#{org.apache.hadoop.hbase.util.VersionInfo.getRevision}, " \
210 "#{org.apache.hadoop.hbase.util.VersionInfo.getDate}" + "\n" \
211 "Type 'help \"COMMAND\"', (e.g. 'help \"get\"' -- the quotes are necessary) for help on a specific command.\n" \
212 "Commands are grouped. Type 'help \"COMMAND_GROUP\"', (e.g. 'help \"general\"') for help on a command group."
217 Quote all names in HBase Shell such as table and column names. Commas delimit
218 command parameters. Type <RETURN> after entering a command to run it.
219 Dictionaries of configuration used in the creation and alteration of tables are
220 Ruby Hashes. They look like this:
222 {'key1' => 'value1', 'key2' => 'value2', ...}
224 and are opened and closed with curley-braces. Key/values are delimited by the
225 '=>' character combination. Usually keys are predefined constants such as
226 NAME, VERSIONS, COMPRESSION, etc. Constants do not need to be quoted. Type
227 'Object.constants' to see a (messy) list of all constants in the environment.
229 If you are using binary keys or values and need to enter them in the shell, use
230 double-quote'd hexadecimal representation. For example:
232 hbase> get 't1', "key\\x03\\x3f\\xcd"
233 hbase> get 't1', "key\\003\\023\\011"
234 hbase> put 't1', "test\\xef\\xff", 'f1:', "\\x01\\x33\\x40"
236 The HBase shell is the (J)Ruby IRB with the above HBase-specific commands added.
237 For more on the HBase Shell, see http://hbase.apache.org/book.html
241 # rubocop:enable Metrics/ClassLength
244 # Load commands base class
245 require 'shell/commands'
248 Shell.load_command_group(
250 full_name: 'GENERAL HBASE SHELL COMMANDS',
260 Shell.load_command_group(
262 full_name: 'TABLES MANAGEMENT COMMANDS',
285 'describe' => ['desc']
289 Shell.load_command_group(
291 full_name: 'NAMESPACE MANAGEMENT COMMANDS',
298 list_namespace_tables
302 Shell.load_command_group(
304 full_name: 'DATA MANIPULATION COMMANDS',
321 Shell.load_command_group(
323 full_name: 'HBASE SURGERY TOOLS',
324 comment: "WARNING: Above commands are for 'experts'-only as misuse can damage an install",
333 is_in_maintenance_mode
345 catalogjanitor_switch
346 catalogjanitor_enabled
349 cleaner_chore_enabled
355 clear_compaction_queues
360 # TODO: remove older hlog_roll command
362 'wal_roll' => ['hlog_roll']
366 Shell.load_command_group(
368 full_name: 'CLUSTER REPLICATION TOOLS',
375 set_peer_replicate_all
378 append_peer_namespaces
379 remove_peer_namespaces
380 set_peer_exclude_namespaces
383 set_peer_exclude_tableCFs
385 list_replicated_tables
388 enable_table_replication
389 disable_table_replication
396 Shell.load_command_group(
398 full_name: 'CLUSTER SNAPSHOT TOOLS',
405 delete_table_snapshots
411 Shell.load_command_group(
413 full_name: 'ONLINE CONFIGURATION TOOLS',
420 Shell.load_command_group(
422 full_name: 'CLUSTER QUOTAS TOOLS',
426 list_quota_table_sizes
432 Shell.load_command_group(
434 full_name: 'SECURITY TOOLS',
435 comment: 'NOTE: Above commands are only applicable if running with the AccessController coprocessor',
437 list_security_capabilities
444 Shell.load_command_group(
446 full_name: 'PROCEDURES & LOCKS MANAGEMENT',
454 Shell.load_command_group(
456 full_name: 'VISIBILITY LABEL TOOLS',
457 comment: 'NOTE: Above commands are only applicable if running with the VisibilityController coprocessor',
468 Shell.load_command_group(
470 full_name: 'RSGroups',
471 comment: "NOTE: The rsgroup Coprocessor Endpoint must be enabled on the Master else commands fail with:
472 UnknownProtocolException: No registered Master Coprocessor Endpoint found for RSGroupAdminService",
481 move_namespaces_rsgroup
482 move_servers_tables_rsgroup
483 move_servers_namespaces_rsgroup
486 remove_servers_rsgroup