您现在的位置是:首页 > 文章详情

HBase Shell 十大花式玩儿法

日期:2020-04-16点击:305

前言:工欲善其事必先利其器,今天给大家介绍一下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=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>

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

900px

原文链接:https://yq.aliyun.com/articles/755684
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章