Wget https://mirrors.tuna.tsinghua.edu.cn/apache/hbase/2.4.18/hbase-2.4.18-bin.tar.gz
tar -xzvf hbase-2.4.18-bin.tar.gz
vim /etc/profile
export HBASE_HOME=/export/software/hbase-2.4.18
export HBASE_CONF_DIR=${HBASE_HOME}/conf
export HBASE_LIB_DIR=${HBASE_HOME}/lib
export PATH=$PATH:$HADOOP_HOME/bin:$HBASE_HOME/bin
修改 HBase 配置文件
/export/software/hbase-2.4.18/conf/hbase-env.sh
export JAVA_HOME=/software/jdk8
export HBASE_CLASSPATH=/export/software/hadoop-3.2.4/conf
export HBASE_DISABLE_HADOOP_CLASSPATH_LOOKUP="true"
export HBASE_MANAGES_ZK=tru
/export/software/hbase-2.4.18/conf/hbase-site.xml
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<property>
<name>hbase.tmp.dir</name>
<value>./tmp</value>
</property>
<property>
<name>hbase.unsafe.stream.capability.enforce</name>
<value>false</value>
</property>
<property>
<!-- region server 的共享 HDFS 目录,用来持久化 Hbase 查看hadoop core-site配置 -->
<name>hbase.rootdir</name>
<value>hdfs://m1:9000/hbase</value>
</property>
<property>
<name>hbase.master.info.port</name>
<value>16010</value>
</property>
<property>
<!-- hbase 的 zookeeper 集群的地址列表,用逗号分隔 -->
<name>hbase.zookeeper.quorum</name>
<value>127.0.0.1</value>
</property>
<property>
<!-- zookeeper 快照存放地址 -->
<name>hbase.zookeeper.property.dataDir</name>
<value>/export/software/hbase-2.4.18/data/zookeeper</value>
</property>
启动
start-hbase.sh
4. 测试
4.1. HBase 使用
4.1.1. 登录到 HBase 命令行
hbase shell
4.1.2. 查看、创建 HBase 命名空间
# 列出所有命名空间
list_namespace
# 创建一个命名空间
create_namespace 'hbase_ns_test_1'
4.1.3. 查看 HBase 表
# 列出所有表
list
# 列出指定命名空间下的所有表
list_namespace_tables 'hbase_ns_test_1'
4.1.4. 设计 HBase 表
-
需求
有两个程序,第一个程序会每隔一小时,将全国每个县区的天气情况记录下来,第二个程序供后人查询某个时间某个地点的天气情况。
要求设计一张HBase表,支持记录并查询 指定时间、指定地点 的 天气历史记录,时间粒度精确到小时、地点粒度精确到县区。
-
中文表名
天气历史记录表
-
英文表名
weather_record
-
HBase 表全名
即
命名空间:表名
hbase_ns_test_1:weather_record
-
列簇(Column Family)设计
天气信息:
weather_info
,存放 天气、温度、湿度、风力;时空信息:
space_time_info
,存放 时间、省、市、县区; -
行键(Row Key) 设计
-
满足查询条件
支持查询 指定时间、指定地点 的天气历史记录。那么 rowkey 必须包含 时间、省、市、县区 这4个信息。
-
唯一性
时间、省、市、县区 这4个信息拼起来可以唯一确定一条天气历史记录数据。
-
散列性(防止数据倾斜)
Hbase 在读写数据时需要通过 RowKey 找到对应的 Region。在 HBase 中,一个 Region 就相当于一个数据分片,每个 Region 都有 StartRowKey 和 StopRowKey(用来表示 Region 存储的 RowKey 的范围),HBase表里面的数据是按照 RowKey 来分散存储到不同的 Region 里面的。
散列性要求将数据记录均衡的分散到不同的 Region 中,避免热点现象、导致数据倾斜(当大量请求访问 HBase 集群的一个或少数几个节点,造成少数 RegionServer 的读写请求过多,负载过大,而其他 RegionServer 负载却很小)。
此处使用一个简单方法实现散列:反转时间 作为rowkey的开头。
时间是以年开头的,年份变化很慢;倒序之后为小时开头,小时变化较快。如将
2022101201
倒序为1021012202
。这样,rowkey 的开头就是 在 0~9 之间变化的,否则 rowkey 的开头是 2022 的 第一个数字 2。如下表所示:小时 00 01 02 ... 23 时间(正序) 2022101200 2022101201 2022101202 ... 2022101223 时间(倒序) 0021012202 1021012202 2021012202 ... 3221012202 -
最终 rowkey
rowkey组成:
时间(倒序)~省~市~县区
案例:
1021012202~GuangDong~GuangZhou~LiWan
-
-
表结构
<table>
<tr>
<td>表名</td>
<td colspan="10">天气历史记录表(weather_record)</td>
</tr>
<tr>
<td>列簇</td>
<td colspan="4">天气信息(weather_info)</td>
<td colspan="4">时空信息(space_time_info)</td>
<td rowspan="2">行键(rowkey)</td>
</tr>
<tr>
<td>字段名</td>
<td>天气(weather)</td>
<td>温度(temperature)</td>
<td>湿度(humidity)</td>
<td>风力(wind_power)</td>
<td>时间(time)</td>
<td>省级(province)</td>
<td>地市(city)</td>
<td>县区(county)</td>
</tr>
<tr>
<td></td>
<td>sunny</td>
<td>28</td>
<td>0.33</td>
<td>2</td>
<td>2022-10-12_01</td>
<td>GuangDong</td>
<td>GuangZhou</td>
<td>LiWan</td>
<td>1021012202GuangDongGuangZhou~LiWan</td>
</tr>
<tr>
<td></td>
<td>sunny</td>
<td>27</td>
<td>0.35</td>
<td>2</td>
<td>2022-10-12_01</td>
<td>GuangDong</td>
<td>GuangZhou</td>
<td>Tianhe</td>
<td>1021012202GuangDongGuangZhou~Tianhe</td>
</tr>
<tr>
<td></td>
<td>sunny</td>
<td>29</td>
<td>0.32</td>
<td>2</td>
<td>2022-10-12_02</td>
<td>GuangDong</td>
<td>GuangZhou</td>
<td>LiWan</td>
<td>2021012202GuangDongGuangZhou~LiWan</td>
</tr>
<tr>
<td></td>
<td>rain</td>
<td>19</td>
<td>0.67</td>
<td>3</td>
<td>2022-10-12_01</td>
<td>ZheJiang</td>
<td>HangZhou</td>
<td>XiHu</td>
<td>1021012202ZheJiangHangZhou~XiHu</td>
</tr>
</table>
4.1.5. 创建 HBase 表
# 查看要创建的表是否存在
exists 'hbase_ns_test_1:weather_record'
# 建表命令:create '表名','列簇名1','列簇名2',...
create 'hbase_ns_test_1:weather_record','weather_info','space_time_info'
4.1.6. 新增数据到 HBase 表中
# put '表名','行键','列簇名:列名','值'[,'时间戳']
# 第1行
put 'hbase_ns_test_1:weather_record','1021012202~GuangDong~GuangZhou~LiWan','weather_info:weather','sunny'
put 'hbase_ns_test_1:weather_record','1021012202~GuangDong~GuangZhou~LiWan','weather_info:temperature','28'
put 'hbase_ns_test_1:weather_record','1021012202~GuangDong~GuangZhou~LiWan','weather_info:humidity','0.33'
put 'hbase_ns_test_1:weather_record','1021012202~GuangDong~GuangZhou~LiWan','weather_info:wind_power','2'
put 'hbase_ns_test_1:weather_record','1021012202~GuangDong~GuangZhou~LiWan','space_time_info:time','2022-10-12_01'
put 'hbase_ns_test_1:weather_record','1021012202~GuangDong~GuangZhou~LiWan','space_time_info:province','GuangDong'
put 'hbase_ns_test_1:weather_record','1021012202~GuangDong~GuangZhou~LiWan','space_time_info:city','GuangZhou'
put 'hbase_ns_test_1:weather_record','1021012202~GuangDong~GuangZhou~LiWan','space_time_info:county','LiWan'
# 第2行
put 'hbase_ns_test_1:weather_record','1021012202~GuangDong~GuangZhou~Tianhe','weather_info:weather','sunny'
put 'hbase_ns_test_1:weather_record','1021012202~GuangDong~GuangZhou~Tianhe','weather_info:temperature','27'
put 'hbase_ns_test_1:weather_record','1021012202~GuangDong~GuangZhou~Tianhe','weather_info:humidity','0.35'
put 'hbase_ns_test_1:weather_record','1021012202~GuangDong~GuangZhou~Tianhe','weather_info:wind_power','2'
put 'hbase_ns_test_1:weather_record','1021012202~GuangDong~GuangZhou~Tianhe','space_time_info:time','2022-10-12_01'
put 'hbase_ns_test_1:weather_record','1021012202~GuangDong~GuangZhou~Tianhe','space_time_info:province','GuangDong'
put 'hbase_ns_test_1:weather_record','1021012202~GuangDong~GuangZhou~Tianhe','space_time_info:city','GuangZhou'
put 'hbase_ns_test_1:weather_record','1021012202~GuangDong~GuangZhou~Tianhe','space_time_info:county','Tianhe'
# 第3行
put 'hbase_ns_test_1:weather_record','2021012202~GuangDong~GuangZhou~LiWan','weather_info:weather','sunny'
put 'hbase_ns_test_1:weather_record','2021012202~GuangDong~GuangZhou~LiWan','weather_info:temperature','29'
put 'hbase_ns_test_1:weather_record','2021012202~GuangDong~GuangZhou~LiWan','weather_info:humidity','0.32'
put 'hbase_ns_test_1:weather_record','2021012202~GuangDong~GuangZhou~LiWan','weather_info:wind_power','2'
put 'hbase_ns_test_1:weather_record','2021012202~GuangDong~GuangZhou~LiWan','space_time_info:time','2022-10-12_02'
put 'hbase_ns_test_1:weather_record','2021012202~GuangDong~GuangZhou~LiWan','space_time_info:province','GuangDong'
put 'hbase_ns_test_1:weather_record','2021012202~GuangDong~GuangZhou~LiWan','space_time_info:city','GuangZhou'
put 'hbase_ns_test_1:weather_record','2021012202~GuangDong~GuangZhou~LiWan','space_time_info:county','LiWan'
# 第4行
put 'hbase_ns_test_1:weather_record','1021012202~ZheJiang~HangZhou~XiHu','weather_info:weather','rain'
put 'hbase_ns_test_1:weather_record','1021012202~ZheJiang~HangZhou~XiHu','weather_info:temperature','19'
put 'hbase_ns_test_1:weather_record','1021012202~ZheJiang~HangZhou~XiHu','weather_info:humidity','0.67'
put 'hbase_ns_test_1:weather_record','1021012202~ZheJiang~HangZhou~XiHu','weather_info:wind_power','3'
put 'hbase_ns_test_1:weather_record','1021012202~ZheJiang~HangZhou~XiHu','space_time_info:time','2022-10-12_01'
put 'hbase_ns_test_1:weather_record','1021012202~ZheJiang~HangZhou~XiHu','space_time_info:province','ZheJiang'
put 'hbase_ns_test_1:weather_record','1021012202~ZheJiang~HangZhou~XiHu','space_time_info:city','HangZhou'
put 'hbase_ns_test_1:weather_record','1021012202~ZheJiang~HangZhou~XiHu','space_time_info:county','XiHu'
4.1.7. 查看 HBase 表数据
-
查看表行数
# count '表名' count 'hbase_ns_test_1:weather_record'
-
查看表中全部数据
# scan '表名' scan 'hbase_ns_test_1:weather_record'
-
查看指定 rowkey 的一行数据
这里查看 “2022年10月12日01时” > ”浙江省“ > ”杭州市“ > ”西湖区“ 的天气,那么
rowkey
就是1021012202~ZheJiang~~HangZhou~XiHu
# get '表名','rowkey' get 'hbase_ns_test_1:weather_record','1021012202~ZheJiang~HangZhou~XiHu'
-
查看指定 rowkey 区间的数据
这里查看 “2022年10月12日01时” > “广东省” > “广州市” 所有县区的天气,那么
rowkey
的范围就是1021012202~GuangDong~GuangZhou~A
~1021012202~GuangDong~GuangZhou~Z
# scan '表名',{STARTROW=>'rowkey范围开始行前缀',STOPROW=>'rowkey范围结束行前缀',LIMIT=>结果集大小} scan 'hbase_ns_test_1:weather_record',{STARTROW=>'1021012202~GuangDong~GuangZhou~A',STOPROW=>'1021012202~GuangDong~GuangZhou~Z',LIMIT=>10}
4.2. HBase Web 页面
# 防火墙放行 16010 tcp 端口
firewall-cmd --zone=public --add-port=16010/tcp --permanent
# 防火墙重新加载
firewall-cmd --reload
浏览器访问部署机器IP:16010
: