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.
19 # File passed to org.jruby.Main by bin/hbase. Pollutes jirb with hbase imports
20 # and hbase commands and then loads jirb. Outputs a banner that tells user
21 # where to find help, shell version, and loads up a custom hirb.
23 # In noninteractive mode, runs commands from stdin until completion or an error.
24 # On success will exit with status 0, on any problem will exit non-zero. Callers
25 # should only rely on "not equal to 0", because the current error exit code of 1
26 # will likely be updated to diffentiate e.g. invalid commands, incorrect args,
29 # TODO: Interrupt a table creation or a connection to a bad master. Currently
30 # has to time out. Below we've set down the retries for rpc and hbase but
31 # still can be annoying (And there seem to be times when we'll retry for
33 # TODO: Add support for listing and manipulating catalog tables, etc.
34 # TODO: Encoding; need to know how to go from ruby String to UTF-8 bytes
36 # Run the java magic include and import basic HBase types that will help ease
40 # Some goodies for hirb. Should these be left up to the user's discretion?
42 require 'irb/completion'
47 # Add the directory names in hbase.jruby.sources commandline option
48 # to the ruby load path so I can load up my HBase ruby modules
49 # in case we are trying to get them out of source instead of jar
51 sources = java.lang.System.getProperty('hbase.ruby.sources')
53 $LOAD_PATH.unshift Pathname.new(sources)
56 cmdline_help = <<HERE # HERE document output as shell usage
57 Usage: shell [OPTIONS] [SCRIPTFILE [ARGUMENTS]]
59 -d | --debug Set DEBUG log levels.
60 -h | --help This help.
61 -n | --noninteractive Do not run within an IRB session and exit with non-zero
62 status on first error.
63 --top-level-defs Compatibility flag to export HBase shell commands onto
65 -Dkey=value Pass hbase-*.xml Configuration overrides. For example, to
66 use an alternate zookeeper ensemble, pass:
67 -Dhbase.zookeeper.quorum=zookeeper.example.org
68 For faster fail, pass the below and vary the values:
69 -Dhbase.client.retries.number=7
70 -Dhbase.ipc.client.connect.max.retries=3
73 # Takes configuration and an arg that is expected to be key=value format.
74 # If c is empty, creates one and returns it
75 def add_to_configuration(c, arg)
77 kv.length == 2 || (raise "Expected parameter #{kv} in key=value format")
78 c = org.apache.hadoop.hbase.HBaseConfiguration.create if c.nil?
85 # strip out any config definitions that won't work with GetoptLong
87 ARGV.delete_if do |arg|
88 if arg.start_with?(D_ARG) && arg.include?('=')
89 conf_from_cli = add_to_configuration(conf_from_cli, arg[2..-1])
96 opts = GetoptLong.new(
97 ['--help', '-h', GetoptLong::NO_ARGUMENT],
98 ['--debug', '-d', GetoptLong::NO_ARGUMENT],
99 ['--noninteractive', '-n', GetoptLong::NO_ARGUMENT],
100 ['--top-level-defs', GetoptLong::NO_ARGUMENT],
101 ['-D', GetoptLong::REQUIRED_ARGUMENT],
102 ['--return-values', '-r', GetoptLong::NO_ARGUMENT]
104 opts.ordering = GetoptLong::REQUIRE_ORDER
107 log_level = org.apache.log4j.Level::ERROR
110 full_backtrace = false
111 top_level_definitions = false
113 opts.each do |opt, arg|
119 conf_from_cli = add_to_configuration(conf_from_cli, arg)
121 log_level = org.apache.log4j.Level::DEBUG
122 full_backtrace = true
124 puts 'Setting DEBUG log level...'
125 when '--noninteractive'
127 when '--return-values'
128 warn '[INFO] the -r | --return-values option is ignored. we always behave '\
129 'as though it was given.'
130 when '--top-level-defs'
131 top_level_definitions = true
135 script2run = ARGV.shift unless ARGV.empty?
137 # Make sure debug flag gets back to IRB
138 ARGV.unshift('-d') if @shell_debug
140 # Set logging level to avoid verboseness
141 org.apache.log4j.Logger.getLogger('org.apache.zookeeper').setLevel(log_level)
142 org.apache.log4j.Logger.getLogger('org.apache.hadoop.hbase').setLevel(log_level)
144 # Require HBase now after setting log levels
145 require 'hbase_constants'
148 require 'hbase_shell'
151 require 'shell/formatter'
153 # Setup the HBase module. Create a configuration.
154 @hbase = conf_from_cli.nil? ? Hbase::Hbase.new : Hbase::Hbase.new(conf_from_cli)
157 @shell = Shell::Shell.new(@hbase, interactive)
158 @shell.debug = @shell_debug
161 # Toggle shell debugging
163 # @return [Boolean] true if debug is turned on after updating the flag
167 conf.back_trace_limit = 0
168 log_level = org.apache.log4j.Level::ERROR
171 conf.back_trace_limit = 100
172 log_level = org.apache.log4j.Level::DEBUG
174 org.apache.log4j.Logger.getLogger('org.apache.zookeeper').setLevel(log_level)
175 org.apache.log4j.Logger.getLogger('org.apache.hadoop.hbase').setLevel(log_level)
180 # Print whether debug is on or off
182 puts "Debug mode is #{@shell_debug ? 'ON' : 'OFF'}\n\n"
187 # For backwards compatibility, this will export all the HBase shell commands, constants, and
188 # instance variables (@hbase and @shell) onto Ruby's top-level receiver object known as "main".
189 @shell.export_all(self) if top_level_definitions
192 require 'irb/ext/change-ws'
197 IRB.conf[:PROMPT][:CUSTOM] = {
198 PROMPT_I: '%N:%03n:%i> ',
199 PROMPT_S: '%N:%03n:%i%l ',
200 PROMPT_C: '%N:%03n:%i* ',
204 IRB.conf[:IRB_NAME] = 'hbase'
205 IRB.conf[:AP_NAME] = 'hbase'
206 IRB.conf[:PROMPT_MODE] = :CUSTOM
207 IRB.conf[:BACK_TRACE_LIMIT] = 0 unless full_backtrace
209 # Create a workspace we'll use across sessions.
210 workspace = @shell.get_workspace
212 # If script2run, try running it. If we're in interactive mode, will go on to run the shell unless
213 # script calls 'exit' or 'exit 0' or 'exit errcode'.
215 ::Shell::Shell.exception_handler(!full_backtrace) do
216 IRB::HIRB.new(workspace, IRB::HBaseLoader.file_for_load(script2run)).run
218 exit @shell.exit_code unless @shell.exit_code.nil?
222 # Output a banner message that tells users where to go for help
225 IRB::HIRB.new(workspace).run
226 exit @shell.exit_code unless interactive || @shell.exit_code.nil?