前言:工欲善其事必先利其器,今天給大家介紹一下HBase Shell十大花式利器,在日常運維作業中,可以試著用起來,
1. 互動模式
也就是我們最常用到的Shell命令列的方式,
$ hbase shell
hbase(main):001:0> list
2. 非互動模式
$ 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數值,如下示例:
$ 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
執行命令如下:
$ 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
執行腳本:
$ sh xxx.sh
5. 讀取文本檔案
假如我的文本檔案是sample_commands.txt,內容如下:
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
執行命令如下:
$ hbase shell ./sample_commands.txt
6. 執行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引數
可以使用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
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樣式的面向物件參考的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=https://www.cnblogs.com/zpb2016/p/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=https://www.cnblogs.com/zpb2016/p/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=https://www.cnblogs.com/zpb2016/p/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>
10. 開啟debug模式
可以在shell中設定除錯開關,以便在運行命令時看到更多的輸出,例如更多的例外堆疊跟蹤:
方式一:
hbase(main):007:0> debug
Debug mode is ON
hbase(main):008:0> debug
Debug mode is OFF
方式二:
$ ./bin/hbase shell -d

轉載請注明出處!歡迎關注本人微信公眾號【HBase作業筆記】
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/16361.html
標籤:大數據
