You can use the HBase shell from within operating system script interpreters like the Bash shell which is the default command interpreter for most Linux and UNIX distributions. The following guidelines use Bash syntax, but could be adjusted to work with C-style shells such as csh or tcsh, and could probably be modified to work with the Microsoft Windows script interpreter as well. Submissions are welcome.
Spawning HBase Shell commands in this way is slow, so keep that in mind when you are deciding when combining HBase operations with the operating system command line is appropriate.
Example 4.1. Passing Commands to the HBase Shell
You can pass commands to the HBase Shell in non-interactive mode (see ???) using the echo
command and the |
(pipe) operator. Be sure to escape characters
in the HBase commands which would otherwise be interpreted by the shell. Some
debug-level output has been truncated from the example below.
$echo "describe 'test1'" | ./hbase shell -n
Version 0.98.3-hadoop2, rd5e65a9144e315bb0a964e7730871af32f5018d5, Sat May 31 19:56:09 PDT 2014 describe 'test1' DESCRIPTION ENABLED 'test1', {NAME => 'cf', DATA_BLOCK_ENCODING => 'NON true E', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIO NS => '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536', IN_MEMORY => 'false' , BLOCKCACHE => 'true'} 1 row(s) in 3.2410 seconds
To suppress all output, echo it to /dev/null:
$ echo "describe 'test'" | ./hbase shell -n > /dev/null 2>&1
Example 4.2. Checking the Result of a Scripted Command
Since scripts are not designed to be run interactively, you need a way to check
whether your command failed or succeeded. The HBase shell uses the standard
convention of returning a value of 0
for successful commands, and
some non-zero value for failed commands. Bash stores a command's return value in a
special environment variable called $?
. Because that variable is
overwritten each time the shell runs any command, you should store the result in a
different, script-defined variable.
This is a naive script that shows one way to store the return value and make a decision based upon it.
#!/bin/bash echo "describe 'test'" | ./hbase shell -n > /dev/null 2>&1 status=$? echo "The status was " $status if ($status == 0); then echo "The command succeeded" else echo "The command may have failed." fi return $status
Getting an exit code of 0 means that the command you scripted definitely succeeded. However, getting a non-zero exit code does not necessarily mean the command failed. The command could have succeeded, but the client lost connectivity, or some other event obscured its success. This is because RPC commands are stateless. The only way to be sure of the status of an operation is to check. For instance, if your script creates a table, but returns a non-zero exit value, you should check whether the table was actually created before trying again to create it.