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.'
155 puts 'For Reference, please visit: http://hbase.apache.org/book.html#shell'
161 def help_multi_command(command)
162 puts "Command: #{command}"
163 puts command_instance(command).help
168 def help_command(command)
169 puts command_instance(command).help
173 def help_group(group_name)
174 group = ::Shell.command_groups[group_name.to_s]
175 group[:commands].sort.each { |cmd| help_multi_command(cmd) }
185 def help(command = nil)
187 return help_command(command) if ::Shell.commands[command.to_s]
188 return help_group(command) if ::Shell.command_groups[command.to_s]
189 puts "ERROR: Invalid command or command group name: #{command}"
195 puts 'COMMAND GROUPS:'
196 ::Shell.command_groups.each do |name, group|
197 puts ' Group name: ' + name
198 puts ' Commands: ' + group[:command_names].sort.join(', ')
209 "HBase Shell, version #{org.apache.hadoop.hbase.util.VersionInfo.getVersion}, " \
210 "r#{org.apache.hadoop.hbase.util.VersionInfo.getRevision}, " \
211 "#{org.apache.hadoop.hbase.util.VersionInfo.getDate}" + "\n" \
212 "Type 'help \"COMMAND\"', (e.g. 'help \"get\"' -- the quotes are necessary) for help on a specific command.\n" \
213 "Commands are grouped. Type 'help \"COMMAND_GROUP\"', (e.g. 'help \"general\"') for help on a command group."
218 Quote all names in HBase Shell such as table and column names. Commas delimit
219 command parameters. Type <RETURN> after entering a command to run it.
220 Dictionaries of configuration used in the creation and alteration of tables are
221 Ruby Hashes. They look like this:
223 {'key1' => 'value1', 'key2' => 'value2', ...}
225 and are opened and closed with curley-braces. Key/values are delimited by the
226 '=>' character combination. Usually keys are predefined constants such as
227 NAME, VERSIONS, COMPRESSION, etc. Constants do not need to be quoted. Type
228 'Object.constants' to see a (messy) list of all constants in the environment.
230 If you are using binary keys or values and need to enter them in the shell, use
231 double-quote'd hexadecimal representation. For example:
233 hbase> get 't1', "key\\x03\\x3f\\xcd"
234 hbase> get 't1', "key\\003\\023\\011"
235 hbase> put 't1', "test\\xef\\xff", 'f1:', "\\x01\\x33\\x40"
237 The HBase shell is the (J)Ruby IRB with the above HBase-specific commands added.
238 For more on the HBase Shell, see http://hbase.apache.org/book.html
242 # rubocop:enable Metrics/ClassLength
245 # Load commands base class
246 require 'shell/commands'
249 Shell.load_command_group(
251 full_name: 'GENERAL HBASE SHELL COMMANDS',
261 Shell.load_command_group(
263 full_name: 'TABLES MANAGEMENT COMMANDS',
287 'describe' => ['desc']
291 Shell.load_command_group(
293 full_name: 'NAMESPACE MANAGEMENT COMMANDS',
300 list_namespace_tables
304 Shell.load_command_group(
306 full_name: 'DATA MANIPULATION COMMANDS',
323 Shell.load_command_group(
325 full_name: 'HBASE SURGERY TOOLS',
326 comment: "WARNING: Above commands are for 'experts'-only as misuse can damage an install",
335 is_in_maintenance_mode
336 clear_slowlog_responses
341 get_slowlog_responses
351 catalogjanitor_switch
352 catalogjanitor_enabled
355 cleaner_chore_enabled
359 snapshot_cleanup_switch
360 snapshot_cleanup_enabled
363 clear_compaction_queues
371 list_decommissioned_regionservers
372 decommission_regionservers
373 recommission_regionserver
375 # TODO: remove older hlog_roll command
377 'wal_roll' => ['hlog_roll']
381 Shell.load_command_group(
383 full_name: 'CLUSTER REPLICATION TOOLS',
390 set_peer_replicate_all
393 append_peer_namespaces
394 remove_peer_namespaces
395 set_peer_exclude_namespaces
396 append_peer_exclude_namespaces
397 remove_peer_exclude_namespaces
400 set_peer_exclude_tableCFs
401 append_peer_exclude_tableCFs
402 remove_peer_exclude_tableCFs
404 list_replicated_tables
407 enable_table_replication
408 disable_table_replication
412 transit_peer_sync_replication_state
416 Shell.load_command_group(
418 full_name: 'CLUSTER SNAPSHOT TOOLS',
425 delete_table_snapshots
431 Shell.load_command_group(
433 full_name: 'ONLINE CONFIGURATION TOOLS',
440 Shell.load_command_group(
442 full_name: 'CLUSTER QUOTAS TOOLS',
446 list_quota_table_sizes
451 enable_exceed_throttle_quota
452 disable_exceed_throttle_quota
456 Shell.load_command_group(
458 full_name: 'SECURITY TOOLS',
459 comment: 'NOTE: Above commands are only applicable if running with the AccessController coprocessor',
461 list_security_capabilities
468 Shell.load_command_group(
470 full_name: 'PROCEDURES & LOCKS MANAGEMENT',
477 Shell.load_command_group(
479 full_name: 'VISIBILITY LABEL TOOLS',
480 comment: 'NOTE: Above commands are only applicable if running with the VisibilityController coprocessor',
491 Shell.load_command_group(
493 full_name: 'RSGroups',
494 comment: "NOTE: The rsgroup Coprocessor Endpoint must be enabled on the Master else commands fail with:
495 UnknownProtocolException: No registered Master Coprocessor Endpoint found for RSGroupAdminService",
504 move_namespaces_rsgroup
505 move_servers_tables_rsgroup
506 move_servers_namespaces_rsgroup
509 remove_servers_rsgroup