前言:工欲善其事必先利其器,今天给你们介绍一下HBase Shell十大花式利器,在平常运维工做中,能够试着用起来。java
也就是咱们最经常使用到的Shell命令行的方式。shell
$ hbase shell hbase(main):001:0> list
$ echo "describe 'test1'" | hbase shell -n # 结果输出到文件 $ echo "describe 'test1'" | hbase shell -n > tmp.log # 不打印输出结果 $ echo "describe 'test'" | hbase shell -n > /dev/null 2>&1
若是咱们想要知道HBase Shell命令执行以后是否成功,那必定要使用非交互模式。由于交互模式执行命令后老是返回0。当执行命令失败后,非交互模式将返回非0数值。以下示例:apache
$ echo "error cmd" | hbase shell > /dev/null 2>&1 $ echo $? 0 $ echo "error cmd" | hbase shell -n > /dev/null 2>&1 $ echo $? 1
$ hbase org.jruby.Main PATH_TO_SCRIPT
咱们拿HBase bin目录下的get-active-master.rb脚本举例:ruby
#!/usr/bin/env hbase-jruby include Java import org.apache.hadoop.hbase.HBaseConfiguration import org.apache.hadoop.hbase.ServerName import org.apache.hadoop.hbase.zookeeper.ZKUtil import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher # disable debug/info logging on this script for clarity log_level = org.apache.log4j.Level::ERROR org.apache.log4j.Logger.getLogger('org.apache.hadoop.hbase').setLevel(log_level) org.apache.log4j.Logger.getLogger('org.apache.zookeeper').setLevel(log_level) config = HBaseConfiguration.create zk = ZooKeeperWatcher.new(config, 'get-active-master', nil) begin master_address = ZKUtil.getData(zk, zk.masterAddressZNode) if master_address puts ServerName.parseFrom(master_address).getHostname() else puts 'Master not running' end ensure zk.close() end
执行命令以下:bash
$ hbase org.jruby.Main get-active-master.rb xxxxxxxx.xxx.com.cn
示例1:微信
#!/bin/bash echo "describe 'test:t1'" | hbase shell -n > tmp.log status=$? echo "The status was " $status if [ $status == 0 ]; then echo "The command succeeded" else echo "The command may have failed." fi
示例2:运维
#!/bin/bash arr=('test:t1' 'test:t2') for table in ${arr[@]} do echo \'$table\' hbase shell << EOF disable '$table' drop '$table' exit EOF done
执行脚本:oop
$ sh xxx.sh
假如个人文本文件是sample_commands.txt,内容以下:ui
create 'test', 'cf' list 'test' put 'test', 'row1', 'cf:a', 'value1' put 'test', 'row2', 'cf:b', 'value2' put 'test', 'row3', 'cf:c', 'value3' put 'test', 'row4', 'cf:d', 'value4' scan 'test' get 'test', 'row1' disable 'test' enable 'test' exit
执行命令以下:this
$ hbase shell ./sample_commands.txt
hbase(main):001:0> import java.util.Date file:/usr/hdp/2.6.5.0-292/hbase/lib/ruby/jruby-complete-1.6.8.jar!/builtin/javasupport/core_ext/object.rb:99 warning: already initialized constant Date => Java::JavaUtil::Date hbase(main):002:0> Date.new(1218920189000).toString() => "Sun Aug 17 04:56:29 CST 2008" hbase(main):004:0> import java.text.SimpleDateFormat => Java::JavaText::SimpleDateFormat hbase(main):005:0> import java.text.ParsePosition => Java::JavaText::ParsePosition hbase(main):006:0> SimpleDateFormat.new("yy/MM/dd HH:mm:ss").parse("08/08/16 20:56:29", ParsePosition.new(0)).getTime() => 1218891389000 hbase(main):003:0>
可使用HBase_Shell_OPTS环境变量将VM选项传递给HBase Shell。能够在环境中设置它,例如编辑~/.bashrc,或者将其设置为启动HBase Shell命令的一部分。下面的示例设置了几个与垃圾回收相关的变量,这些变量只在运行HBase Shell的VM的生存期内使用。该命令应在一行上运行,但为了可读性,它被\字符打断。
$ HBASE_SHELL_OPTS="-verbose:gc -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCDateStamps \ -XX:+PrintGCDetails -Xloggc:$HBASE_HOME/logs/gc-hbase.log" hbase shell
如下版本hbase-2.0.5/hbase-2.1.3/hbase-2.2.0/hbase-1.4.10/hbase-1.5.0,经过在命令行上传递以-D为前缀的键/值,能够传递或重写hbase-*.xml中指定的hbase配置,以下所示:
$ hbase shell -Dhbase.zookeeper.quorum=ZK0.remote.cluster.example.org,ZK1.remote.cluster.example.org,ZK2.remote.cluster.example.org -Draining=false ... hbase(main):001:0> @shell.hbase.configuration.get("hbase.zookeeper.quorum") => "ZK0.remote.cluster.example.org,ZK1.remote.cluster.example.org,ZK2.remote.cluster.example.org" hbase(main):002:0> @shell.hbase.configuration.get("raining") => "false"
HBase 0.95添加了为表提供jruby样式的面向对象引用的shell命令。之前,做用于表的全部shell命令都有一个过程样式,该样式始终将表的名称做为参数。HBase 0.95引入了将表分配给jruby变量的功能。表引用可用于执行数据读写操做,如放置、扫描和获取,以及管理功能,如禁用、删除和描述表。
例如,之前您老是指定一个表名:
hbase(main):000:0> create 'test', 'f' 0 row(s) in 1.0970 seconds hbase(main):001:0> put 'test', 'rold', 'f', 'v' 0 row(s) in 0.0080 seconds hbase(main):002:0> scan 'test' ROW COLUMN+CELL rold column=f:, timestamp=1378473207660, value=v 1 row(s) in 0.0130 seconds hbase(main):004:0> disable 'test' 0 row(s) in 14.8700 seconds hbase(main):005:0> drop 'test' 0 row(s) in 23.1670 seconds hbase(main):006:0>
如今,能够将表分配给一个变量,并在jruby shell代码中使用结果。
hbase(main):007 > t = create 'test', 'f' 0 row(s) in 1.0970 seconds => Hbase::Table - t hbase(main):008 > t.put 'r', 'f', 'v' 0 row(s) in 0.0640 seconds hbase(main):009 > t.scan ROW COLUMN+CELL r column=f:, timestamp=1331865816290, value=v 1 row(s) in 0.0110 seconds hbase(main):038:0> t.disable 0 row(s) in 6.2350 seconds hbase(main):039:0> t.drop 0 row(s) in 0.2340 seconds
若是表已建立,则可使用get_table方法将表分配给变量:
hbase(main):011 > create 't','f' 0 row(s) in 1.2500 seconds => Hbase::Table - t hbase(main):012:0> tab = get_table 't' 0 row(s) in 0.0010 seconds => Hbase::Table - t hbase(main):013:0> tab.put 'r1' ,'f', 'v' 0 row(s) in 0.0100 seconds hbase(main):014:0> tab.scan ROW COLUMN+CELL r1 column=f:, timestamp=1378473876949, value=v 1 row(s) in 0.0240 seconds hbase(main):015:0>
列表功能也获得了扩展,以便它以字符串形式返回表名列表。而后可使用jruby根据这些名称编写表操做脚本。list_snapshots命令的做用也相似。
hbase(main):016 > tables = list('ns:t.*') TABLE t 1 row(s) in 0.1040 seconds => #<#<Class:0x7677ce29>:0x21d377a4> hbase(main):017:0> tables.map { |t| disable t ; drop t} 0 row(s) in 2.2510 seconds => [nil] hbase(main):018:0>
能够在shell中设置调试开关,以便在运行命令时看到更多的输出,例如更多的异常堆栈跟踪:
方式一:
hbase(main):007:0> debug Debug mode is ON hbase(main):008:0> debug Debug mode is OFF
方式二:
$ ./bin/hbase shell -d
转载请注明出处!欢迎关注本人微信公众号【HBase工做笔记】