前言:工欲善其事必先利其器,今天給你們介紹一下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工做筆記】