前言:工欲善其事必先利其器,今天給大家介紹一下HBase Shell十大花式利器,在日常運(yùn)維工作中,可以試著用起來。
1. 交互模式
也就是我們最常用到的Shell命令行的方式。
$ hbase shell
hbase(main):001:0> list
2. 非交互模式
$ echo "describe 'test1'" | hbase shell -n
# 結(jié)果輸出到文件
$ echo "describe 'test1'" | hbase shell -n > tmp.log
# 不打印輸出結(jié)果
$ echo "describe 'test'" | hbase shell -n > /dev/null 2>&1
與交互模式比較
如果我們想要知道HBase Shell命令執(zhí)行之后是否成功,那一定要使用非交互模式。因?yàn)榻换ツJ綀?zhí)行命令后總是返回0。當(dāng)執(zhí)行命令失敗后,非交互模式將返回非0數(shù)值。如下示例:
$ echo "error cmd" | hbase shell > /dev/null 2>&1
$ echo $?
0
$ echo "error cmd" | hbase shell -n > /dev/null 2>&1
$ echo $?
1
3. 使用Ruby腳本
$ hbase org.jruby.Main PATH_TO_SCRIPT
我們拿HBase bin目錄下的get-active-master.rb腳本舉例:
#!/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
執(zhí)行命令如下:
$ hbase org.jruby.Main get-active-master.rb
xxxxxxxx.xxx.com.cn
4. 使用Bash腳本
示例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
執(zhí)行腳本:
$ sh xxx.sh
5. 讀取文本文件
假如我的文本文件是sample_commands.txt,內(nèi)容如下:
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
執(zhí)行命令如下:
$ hbase shell ./sample_commands.txt
6. 執(zhí)行Java代碼
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>
7. 傳遞VM參數(shù)
可以使用HBase_Shell_OPTS環(huán)境變量將VM選項(xiàng)傳遞給HBase Shell??梢栽诃h(huán)境中設(shè)置它,例如編輯~/.bashrc,或者將其設(shè)置為啟動(dòng)HBase Shell命令的一部分。下面的示例設(shè)置了幾個(gè)與垃圾回收相關(guān)的變量,這些變量只在運(yùn)行HBase Shell的VM的生存期內(nèi)使用。該命令應(yīng)在一行上運(yùn)行,但為了可讀性,它被\字符打斷。
$ HBASE_SHELL_OPTS="-verbose:gc -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCDateStamps -XX:+PrintGCDetails -Xloggc:$HBASE_HOME/logs/gc-hbase.log" hbase shell
8. 配置覆蓋
以下版本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"
9. 表變量
HBase 0.95添加了為表提供jruby樣式的面向?qū)ο笠玫膕hell命令。以前,作用于表的所有shell命令都有一個(gè)過程樣式,該樣式始終將表的名稱作為參數(shù)。HBase 0.95引入了將表分配給jruby變量的功能。表引用可用于執(zhí)行數(shù)據(jù)讀寫操作,如放置、掃描和獲取,以及管理功能,如禁用、刪除和描述表。
例如,以前您總是指定一個(gè)表名:
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>
現(xiàn)在,可以將表分配給一個(gè)變量,并在jruby shell代碼中使用結(jié)果。
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
如果表已創(chuàng)建,則可以使用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>
列表功能也得到了擴(kuò)展,以便它以字符串形式返回表名列表。然后可以使用jruby根據(jù)這些名稱編寫表操作腳本。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>
10. 開啟debug模式
可以在shell中設(shè)置調(diào)試開關(guān),以便在運(yùn)行命令時(shí)看到更多的輸出,例如更多的異常堆棧跟蹤:
方式一:
hbase(main):007:0> debug
Debug mode is ON
hbase(main):008:0> debug
Debug mode is OFF
方式二:
$ ./bin/hbase shell -d
轉(zhuǎn)載請(qǐng)注明出處!歡迎關(guān)注本人微信公眾號(hào)【HBase工作筆記】
|