说完了Hive我们接着来看另外一个建立在Hadoop基础上的存储引擎HBase,HBase以内存作为缓存数据落地到HDFS的Key-Value数据库,因为使用内存缓存极大保障了数据的实时性和实时查询能力,在实时场景的大数据存储HBase是不可或缺的解决方案,常见又在使用这项技术的业务就是短链,比如你在微信给你的朋友发个URL最终你的朋友获取到的是微信的一个短链接(QQ淘宝都是如此),在HBase中就存储了这样一个对应关系,这一切都归功于HBase的吞吐量和实时响应速度.
附上:
Hbase官网:Apache HBase – Apache HBase™ Home
喵了个咪的博客:w-blog.cn
1. 准备工作
准备软件包
zookeeper-3.4.10.tar.gz
hbase-1.3.1-bin.tar.gz
Hbase依赖于Zookeeper和Hadoop集群所以我们在之前配置好的Hadoop集群下来配置整体的Hbase集群
服务器清单
# hadoop-1 192.168.1.101 NameNode DataNode
$ hadoop-2 192.168.1.102 DataNode
$ hadoop-3 192.168.1.103 DataNode
Zookeeper安装
> cd /app/install/
> tar -zxvf zookeeper-3.4.10.tar.gz
> mv zookeeper-3.4.10 /usr/local/
修改配置文件
> cd /usr/local/zookeeper-3.4.10/conf/
> cp zoo_sample.cfg zoo.cfg
> vim zoo.cfg
tickTime=2000
dataDir=/usr/local/zookeeper-3.4.10/data
clientPort=2181
initLimit=10
syncLimit=5
server.1=hadoop-1:2888:3888
server.2=hadoop-2:2888:3888
server.3=hadoop-3:2888:3888
所有节点修改环境变量
> vim /etc/profile
# zookeeper
export ZOOKEEPER_HOME=/usr/local/zookeeper-3.4.10
export PATH=$ZOOKEEPER_HOME/bin:$PATH
> source /etc/profile
将zookeeper目录复制到其他节点上
> scp -r /usr/local/zookeeper-3.4.10/ root@hadoop-2:/usr/local/zookeeper-3.4.10
> scp -r /usr/local/zookeeper-3.4.10/ root@hadoop-3:/usr/local/zookeeper-3.4.10
添加myid文件(每节点都需要)
> cd /usr/local/zookeeper-3.4.10
> mkdir data
> echo "1" > data/myid
注意,每个节点myid文件要不一致
启动并测试
# 在三台机器上分别执行
> zkServer.sh start
# 查看状态
[root@hadoop-1 zookeeper-3.4.10]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower
[root@hadoop-2 zookeeper-3.4.10]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: leader
[root@hadoop-3 zookeeper-3.4.10]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower
2.安装HBase
> cd /app/install/
> tar -zxvf hbase-1.3.1-bin.tar.gz
> mv hbase-1.3.1 /usr/local/
修改配置文件
> vim /usr/local/hbase-1.3.1/conf/hbase-env.sh
# 配置Java环境变量
export JAVA_HOME=/usr/local/jdk1.8
# hbase使用外部的zk
export HBASE_MANAGES_ZK=false
增加相应配置
> vim /usr/local/hbase-1.3.1/conf/hbase-site.xml
<configuration>
<!-- 指定hbase在HDFS上存储的路径 -->
<property>
<name>hbase.rootdir</name>
<value>hdfs://hadoop-1:9000/hbase</value>
</property>
<!-- 指定hbase是分布式的 -->
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<!-- 指定zk的地址,多个用“,”分割 -->
<property>
<name>hbase.zookeeper.quorum</name>
<value>hadoop-1:2181,hadoop-2:2181,hadoop-3:2181</value>
</property>
</configuration>
增加子节点
> vim /usr/local/hbase-1.3.1/conf/regionservers
hadoop-2
hadoop-3
创建hdfs中数据存放路径b
> hdfs dfs -mkdir /user/hadoop/hbase
复制到其他节点
> scp -r /usr/local/hbase-1.3.1/ root@hadoop-2:/usr/local/hbase-1.3.1
> scp -r /usr/local/hbase-1.3.1/ root@hadoop-3:/usr/local/hbase-1.3.1
# 分别赋予权限
chown -R hadoop:hadoop /usr/local/hbase-1.3.1/
所有节点配置环境变量
> vim /etc/profile
# hbase
export HBASE_HOME=/usr/local/hbase-1.3.1
export PATH=$HBASE_HOME/bin:$PATH
> source /etc/profile
启动集群
su hadoop
start-hbase.sh
通过JPS可以查看到主节点上有HMaster进程子节点上有HRegionServer进程
内网可以访问Hbase管理界面 http://hadoop-1:16010
3.基本操作
通过如下命令可以进入Hbase的shell操作界面
hbase shell
hbase(main):001:0>
一般操作
查询服务器状态
hbase(main):024:0>status
1 active master, 0 backup masters, 2 servers, 0 dead, 1.0000 average load
查询HBase版本信息
hbase(main):025:0>version
1.3.1, r930b9a55528fe45d8edce7af42fef2d35e77677a, Thu Apr 6 19:36:54 PDT 2017
二、DDL操作
1.创建一个表
hbase(main):011:0>create 'member','member_id','address','info'
0 row(s) in 1.2210seconds
2.获得表的描述
hbase(main):012:0>list
TABLE
member
1 row(s) in 0.0160seconds
hbase(main):006:0>describe 'member'
DESCRIPTION ENABLED
{NAME => 'member', FAMILIES => [{NAME=> 'address', BLOOMFILTER => 'NONE', REPLICATION_SCOPE => '0', true
VERSIONS => '3', COMPRESSION => 'NONE',TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMORY => 'fa
lse', BLOCKCACHE => 'true'}, {NAME =>'info', BLOOMFILTER => 'NONE', REPLICATION_SCOPE => '0', VERSI
ONS => '3', COMPRESSION => 'NONE', TTL=> '2147483647', BLOCKSIZE => '65536', IN_MEMORY => 'false',
BLOCKCACHE => 'true'}]}
1 row(s) in 0.0230seconds
3.删除一个列族,alter,disable,enable
我们之前建了3个列族,但是发现member_id这个列族是多余的,因为他就是主键,所以我们要将其删除。
hbase(main):003:0>alter 'member',{NAME=>'member_id',METHOD=>'delete'}
ERROR: Table memberis enabled. Disable it first before altering.
直接操作会报错,如果需要删除列族的时候必须先将表给disable掉。
hbase(main):004:0>disable 'member'
0 row(s) in 2.0390seconds
hbase(main):005:0>alter'member',{NAME=>'member_id',METHOD=>'delete'}
0 row(s) in 0.0560seconds
hbase(main):006:0>describe 'member'
DESCRIPTION ENABLED
{NAME => 'member', FAMILIES => [{NAME=> 'address', BLOOMFILTER => 'NONE', REPLICATION_SCOPE => '0',false
VERSIONS => '3', COMPRESSION => 'NONE',TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMORY => 'fa
lse', BLOCKCACHE => 'true'}, {NAME =>'info', BLOOMFILTER => 'NONE', REPLICATION_SCOPE => '0', VERSI
ONS => '3', COMPRESSION => 'NONE', TTL=> '2147483647', BLOCKSIZE => '65536', IN_MEMORY => 'false',
BLOCKCACHE => 'true'}]}
1 row(s) in 0.0230seconds
该列族已经删除,我们继续将表enable
hbase(main):008:0> enable 'member'
0 row(s) in 2.0420seconds
4.列出所有的表
hbase(main):028:0>list
TABLE
member
temp_table
2 row(s) in 0.0150seconds
5.drop一个表
hbase(main):029:0>disable 'temp_table'
0 row(s) in 2.0590seconds
hbase(main):030:0>drop 'temp_table'
0 row(s) in 1.1070seconds
6.查询表是否存在
hbase(main):021:0>exists 'member'
Table member doesexist
0 row(s) in 0.1610seconds
7.判断表是否enable
hbase(main):034:0>is_enabled 'member'
true
0 row(s) in 0.0110seconds
8.判断表是否disable
hbase(main):032:0>is_disabled 'member'
false
0 row(s) in 0.0110seconds
三、DML操作
1.插入几条记录
put'member','scutshuxue','info:age','24'
put'member','scutshuxue','info:birthday','1987-06-17'
put'member','scutshuxue','info:company','alibaba'
put'member','scutshuxue','address:contry','china'
put'member','scutshuxue','address:province','zhejiang'
put'member','scutshuxue','address:city','hangzhou'
put'member','xiaofeng','info:birthday','1987-4-17'
put'member','xiaofeng','info:favorite','movie'
put'member','xiaofeng','info:company','alibaba'
put'member','xiaofeng','address:contry','china'
put'member','xiaofeng','address:province','guangdong'
put'member','xiaofeng','address:city','jieyang'
put'member','xiaofeng','address:town','xianqiao'
2.获取一条数据
获取一个id的所有数据
hbase(main):001:0>get 'member','scutshuxue'
COLUMN CELL
address:city timestamp=1321586240244, value=hangzhou
address:contry timestamp=1321586239126, value=china
address:province timestamp=1321586239197, value=zhejiang
info:age timestamp=1321586238965, value=24
info:birthday timestamp=1321586239015, value=1987-06-17
info:company timestamp=1321586239071, value=alibaba
6 row(s) in 0.4720seconds
获取一个id,一个列族的所有数据
hbase(main):002:0>get 'member','scutshuxue','info'
COLUMN CELL
info:age timestamp=1321586238965, value=24
info:birthday timestamp=1321586239015, value=1987-06-17
info:company timestamp=1321586239071, value=alibaba
3 row(s) in 0.0210seconds
获取一个id,一个列族中一个列的所有数据
hbase(main):002:0>get 'member','scutshuxue','info:age'
COLUMN CELL
info:age timestamp=1321586238965, value=24
1 row(s) in 0.0320seconds
6.更新一条记录
将scutshuxue的年龄改成99
hbase(main):004:0>put 'member','scutshuxue','info:age' ,'99'
0 row(s) in 0.0210seconds
hbase(main):005:0>get 'member','scutshuxue','info:age'
COLUMN CELL
info:age timestamp=1321586571843, value=99
1 row(s) in 0.0180seconds
3.通过timestamp来获取两个版本的数据
hbase(main):010:0>get 'member','scutshuxue',{COLUMN=>'info:age',TIMESTAMP=>1321586238965}
COLUMN CELL
info:age timestamp=1321586238965, value=24
1 row(s) in 0.0140seconds
hbase(main):011:0>get 'member','scutshuxue',{COLUMN=>'info:age',TIMESTAMP=>1321586571843}
COLUMN CELL
info:age timestamp=1321586571843, value=99
1 row(s) in 0.0180seconds
4.全表扫描:
hbase(main):013:0>scan 'member'
ROW COLUMN+CELL
scutshuxue column=address:city, timestamp=1321586240244, value=hangzhou
scutshuxue column=address:contry, timestamp=1321586239126, value=china
scutshuxue column=address:province, timestamp=1321586239197, value=zhejiang
scutshuxue column=info:age,timestamp=1321586571843, value=99
scutshuxue column=info:birthday, timestamp=1321586239015, value=1987-06-17
scutshuxue column=info:company, timestamp=1321586239071, value=alibaba
temp column=info:age, timestamp=1321589609775, value=59
xiaofeng column=address:city, timestamp=1321586248400, value=jieyang
xiaofeng column=address:contry, timestamp=1321586248316, value=china
xiaofeng column=address:province, timestamp=1321586248355, value=guangdong
xiaofeng column=address:town, timestamp=1321586249564, value=xianqiao
xiaofeng column=info:birthday, timestamp=1321586248202, value=1987-4-17
xiaofeng column=info:company, timestamp=1321586248277, value=alibaba
xiaofeng column=info:favorite, timestamp=1321586248241, value=movie
3 row(s) in 0.0570seconds
5.删除id为temp的值的‘info:age’字段
hbase(main):016:0>delete 'member','temp','info:age'
0 row(s) in 0.0150seconds
hbase(main):018:0>get 'member','temp'
COLUMN CELL
0 row(s) in 0.0150seconds
6.删除整行
hbase(main):001:0>deleteall 'member','xiaofeng'
0 row(s) in 0.3990seconds
7.查询表中有多少行:
hbase(main):019:0>count 'member'
2 row(s) in 0.0160seconds
8.给”xiaofeng”这个id增加'info:age'字段,并使用counter实现递增
hbase(main):057:0*incr 'member','xiaofeng','info:age'
COUNTER VALUE = 1
hbase(main):058:0>get 'member','xiaofeng','info:age'
COLUMN CELL
info:age timestamp=1321590997648, value=\x00\x00\x00\x00\x00\x00\x00\x01
1 row(s) in 0.0140seconds
hbase(main):059:0>incr 'member','xiaofeng','info:age'
COUNTER VALUE = 2
hbase(main):060:0>get 'member','xiaofeng','info:age'
COLUMN CELL
info:age timestamp=1321591025110, value=\x00\x00\x00\x00\x00\x00\x00\x02
1 row(s) in 0.0160seconds
获取当前count的值
hbase(main):069:0>get_counter 'member','xiaofeng','info:age'
COUNTER VALUE = 2
9.将整张表清空:
hbase(main):035:0>truncate 'member'
Truncating 'member'table (it may take a while):
- Disabling table...
- Dropping table...
- Creating table...
0 row(s) in 4.3430seconds
可以看出,hbase是先将掉disable掉,然后drop掉后重建表来实现truncate的功能的。
4. 其他
导出Hbase数据
# 导出到hdfs
hbase org.apache.hadoop.hbase.mapreduce.Driver export member /hbase/export/member
# 导出文件列表
[hadoop@sunmi-hadoop-1 hbase-1.3.1]$ hdfs dfs -ls /hbase/export/member
Found 2 items
-rw-r--r-- 2 hadoop supergroup 0 2017-08-01 15:11 /hbase/export/member/_SUCCESS
-rw-r--r-- 2 hadoop supergroup 775 2017-08-01 15:11 /hbase/export/member/part-m-00000
# 导入需要先创建表
create 'member2','address','info'
$ 通过导出的数据导入
hbase org.apache.hadoop.hbase.mapreduce.Driver import member2 /hbase/export/member
# 查询数据
get 'member2','sc utshuxue'
预分区
类似于Hive的分区和桶的概念,用法如下
> create 't1', 'cf', SPLITS => ['20150501000000000', '20150515000000000', '20150601000000000']
或者
> create 't2', 'cf', SPLITS_FILE => '/home/hadoop/splitfile.txt'
/home/hadoop/splitfile.txt中存储内容如下:
20150501000000000
20150515000000000
20150601000000000
从HBase的Web UI中可以查看到表的分区
启动thrift 服务
Hbase 有两套Thrift调用方式 分别是Thrift1 和 thrift2 大部分开源和Thrift相结合的都是使用 thrift1 但是 Thrift2 是对于 thrift1 的简化 更适合编写代码中使用 可以通过指定端口的方式来同时运行两个服务 --infoport 9096 -p 9091 推荐thrift模式 thrift2 使用指定端口
PS:但是有些服务仅仅支持thrift1的协议比如我们后面要说的的
/usr/local/hbase-1.3.1/bin/hbase-daemon.sh --config /usr/local/hbase-1.3.1/conf foreground_start thrift --infoport 9096 -p 9091
启动 Thrift2 服务
# 开启本机的thrift服务
hbase-daemon.sh start thrift2
# 开启集群其余机器thrift服务
hbase-daemons.sh start thrift2
使用Supervisor守护进程方式前台运行
/usr/local/hbase-1.3.1/bin/hbase-daemon.sh --config /usr/local/hbase-1.3.1/conf foreground_start thrift2
注意如果程序长连接使用HBase服务会出现过一段时间断开的问题应为 超时机制 60S 超时断掉了 这个时候可以通过设置配置文件来解决,因此在conf/hbase-site.xml中添加上配置即可:
> vim /usr/local/hbase-1.3.1/conf/hbase-site.xml
<property>
<name>hbase.thrift.server.socket.read.timeout</name>
<value>6000000</value>
<description>eg:milisecond</description>
</property>
服务持续运行
一般使用Supervisor来进行持续执行,当服务因为异常原因终止之后会自己拉起来,但是运行程序的一定要是前台运行的程序,Hbase主要运行hbasemaster和hbaseregionserver就可以正常提供服务了
# hbaseregionserver
/usr/local/hbase-1.3.1/bin/hbase-daemon.sh --config /usr/local/hbase-1.3.1/conf foreground_start regionserver
# hbasemaster
/usr/local/hbase-1.3.1/bin/hbase-daemon.sh --config /usr/local/hbase-1.3.1/conf foreground_start master
5 总结
经过本节的介绍大家对HBase也有了一定的了解,HBase在集群模式下能够带来更大的性能和容量优势,但是HBase在统计汇总能力比较弱,下节将介绍HBase和Hive互相结合集成Hive的结构化方便查询统计优点也结合HBase速度的优势,并且解决Hive实时写入的问题.
注:笔者能力有限有说的不对的地方希望大家能够指出,也希望多多交流!