HBase详解
Posted 阿德小仔
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HBase详解相关的知识,希望对你有一定的参考价值。
目录
第 1 章:HBase 简介
1.1 HBase 定义
HBase 是一种分布式、可扩展、支持海量数据存储的 NoSQL 数据库。1.2 HBase 数据模型
逻辑上, HBase 的数据模型同关系型数据库很类似,数据存储在一张表中,有行有列。但从 HBase 的底层物理存储结构( K-V )来看, HBase 更像是一个 multi-dimensional map。 1.2.1 HBase 逻辑结构

1.3 HBase 基本架构

第 2 章:HBase 快速入门
2.1 HBase 安装部署
2.1.1 Zookeeper 正常部署 首先保证 Zookeeper 集群的正常部署,并启动之:[root@master zookeeper]# bin/zkServer.sh start [root@node1 zookeeper]# bin/zkServer.sh start [root@node2 zookeeper]# bin/zkServer.sh start2.1.2 Hadoop 正常部署 Hadoop 集群的正常部署并启动:
[root@master hadoop]# sbin/start-all.sh2.1.3 HBase 的解压 解压 Hbase 到指定目录:
[root@master package]# tar -zxvf hbase-1.4.6-bin.tar.gz -C /usr/local/soft/2.1.4 HBase 的配置文件 修改 HBase 对应的配置文件。 1 ) hbase-env.sh 修改内容:
export JAVA_HOME=/usr/local/soft/jdk1.8.0_171 export HBASE_MANAGES_ZK=false2 ) hbase-site.xml 修改内容:
<configuration> <property> <name>hbase.rootdir</name> <value>hdfs://master:9000/HBase</value> </property> <property> <name>hbase.cluster.distributed</name> <value>true</value> </property> <!-- 0.98 后的新变动,之前版本没有 .port, 默认端口为 60000 --> <property> <name>hbase.master.port</name> <value>16000</value> </property> <property> <name>hbase.zookeeper.quorum</name> <value>master,node1,node2</value> </property> <property> <name>hbase.zookeeper.property.dataDir</name> <value>/usr/local/soft/hbase/zkData</value> </property> </configuration>3 ) regionservers :
master node1 node24 )软连接 hadoop 配置文件到 HBase :
[root@master soft]# ln -s /usr/local/soft/hadoop-2.7.6/etc/hadoop/core-site.xml /usr/local/soft/hbase/conf/core-site.xml [root@master soft]# ln -s /usr/local/soft/hadoop-2.7.6/etc/hadoop/hdfs-site.xml /usr/local/soft/hbase/conf/hdfssite.xml2.1.5 HBase 远程发送到其他集群
[root@master soft]# scp -r hbase node1:`pwd` [root@master soft]# scp -r hbase node2:`pwd`2.1.6 HBase 服务的启动 1 .启动方式
[root@master hbase]# bin/hbase-daemon.sh start master [root@master hbase]# bin/hbase-daemon.sh start regionserver提示: 如果集群之间的节点时间不同步,会导致 regionserver 无法启动,抛出 ClockOutOfSyncException 异常。 修复提示: ①百度, linux 时间同步 ②属性: hbase.master.maxclockskew 设置更大的值
<property> <name>hbase.master.maxclockskew</name> <value>180000</value> <description>Time difference of regionserver from master</description> </property>2 .启动方式 2
[root@master hbase]# bin/start-hbase.sh对应的停止服务:
[root@master hbase]# bin/stop-hbase.sh2.1.7 查看 HBase 页面 启动成功后,可以通过“host:port”的方式来访问 HBase 管理页面,例如:
http://master:16010
2.2 HBase Shell 操作
2.2.1 基本操作 1 .进入 HBase 客户端命令行[root@master hbase]# bin/hbase shell2 .查看帮助命令
hbase(main):001:0> help3 .查看当前数据库中有哪些表
hbase(main):002:0> list2.2.2 表的操作 1 .创建表
hbase(main):002:0> create 'student','info'2 .插入数据到表
hbase(main):003:0> put 'student','1001','info:sex','male' hbase(main):004:0> put 'student','1001','info:age','18' hbase(main):005:0> put 'student','1002','info:name','Janna' hbase(main):006:0> put 'student','1002','info:sex','female' hbase(main):007:0> put 'student','1002','info:age','20'3 .扫描查看表数据
hbase(main):008:0> scan 'student' hbase(main):009:0> scan 'student',STARTROW => '1001', STOPROW => '1001' hbase(main):010:0> scan 'student',STARTROW => '1001'4 .查看表结构
hbase(main):011:0> describe ‘student’5 .更新指定字段的数据
hbase(main):012:0> put 'student','1001','info:name','Nick' hbase(main):013:0> put 'student','1001','info:age','100'6 .查看“指定行”或“指定列族 : 列”的数据
hbase(main):014:0> get 'student','1001' hbase(main):015:0> get 'student','1001','info:name'7 .统计表数据行数
hbase(main):021:0> count 'student'8 .删除数据 删除某 rowkey 的全部数据:
hbase(main):016:0> deleteall 'student','1001'删除某 rowkey 的某一列数据:
hbase(main):017:0> delete 'student','1002','info:sex'9 .清空表数据
hbase(main):018:0> truncate 'student'提示:清空表的操作顺序为先 disable ,然后再 truncate 。 10 .删除表 首先需要先让该表为 disable 状态:
hbase(main):019:0> disable 'student'然后才能 drop 这个表:
hbase(main):020:0> drop 'student'提示:如果直接 drop 表,会报错: ERROR: Table student is enabled. Disable it first. 11 .变更表信息 将 info 列族中的数据存放 3 个版本:
hbase(main):022:0> alter 'student',NAME=>'info',VERSIONS=>3 hbase(main):022:0> get 'student','1001',COLUMN=>'info:name',VERSIONS=>3
第 3 章 HBase 进阶
3.1 架构原理

3.2 写流程

3.3 MemStore Flush
MemStore 刷写时机: 1. 当某个 memstroe 的大小达到了 hbase.hregion.memstore.flush.size (默认值 128M ) ,其所在 region 的所有 memstore 都会刷写 。 当 memstore 的大小达到了: hbase.hregion.memstore.flush.size (默认值 128M ) hbase.hregion.memstore.block.multiplier (默认值 4 ) 时,会阻止继续 往该 memstore 写数据。 2. 当 region server 中 memstore 的总大小达到: java_heapsize *hbase.regionserver.global.memstore.size (默认值 0.4 ) *hbase.regionserver.global.memstore.size.lower.limit (默认值 0.95 ) region 会按照其所有 memstore 的大小顺序(由大到小)依次进行刷写。直到 region server 中所有 memstore 的总大小减小到上述值以下。 当 region server 中 memstore 的总大小达到 java_heapsize*hbase.regionserver.global.memstore.size (默认值 0.4 )时,会阻止继续往所有的 memstore 写数据。 3. 到达自动刷写的时间,也会触发 memstore flush 。自动刷新的时间间隔由该属性进行配置 hbase.regionserver.optionalcacheflushinterval (默认 1 小时)。 4. 当 WAL 文件的数量超过 hbase.regionserver.max.logs , region 会按照时间顺序依次进行刷写,直到 WAL 文件数量减小到 hbase.regionserver.max.log 以下( 该属性名已经废 弃,现无需手动设置,最大值为 32 )。3.4 读流程

3.5 StoreFile Compaction
由于 memstore 每次刷写都会生成一个新的 HFile ,且同一个字段的不同版本(timestamp) 和不同类型(Put/Delete)有可能会分布在不同的 HFile 中,因此查询时需要遍历所有的HFile。为了减少 HFile 的个数,以及清理掉过期和删除的数据,会进行 StoreFile Compaction 。Compaction 分为两种,分别是 Minor Compaction 和 Major Compaction 。 Minor Compaction 会将临近的若干个较小的 HFile 合并成一个较大的 HFile ,但 不会清理过期和 删除的数据 。Major Compaction 会将一个 Store 下的所有的 HFile 合并成一个大 HFile ,并且 会清 理掉过期和删除的数据 。3.6 Region Split


第 4 章 HBase API
4.1 环境准备
新建项目后在 pom.xml 中添加依赖:<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.1</version> </dependency> <!--hbase--> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.4.6</version> </dependency>
4.2 HBaseAPI
4.2.1 获取 Configuration 对象public static Configuration conf; static // 使用 HBaseConfiguration 的单例方法实例化 conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.152.100"); conf.set("hbase.zookeeper.property.clientPort", "2181");4.2.2 判断表是否存在
public static boolean isTableExist(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException // 在 HBase 中管理、访问表需要先创建 HBaseAdmin 对象 //Connection connection = ConnectionFactory.createConnection(conf); //HBaseAdmin admin = (HBaseAdmin) connection.getAdmin(); HBaseAdmin admin = new HBaseAdmin(conf); return admin.tableExists(tableName);4.2.3 创建表
public static void createTable(String tableName, String... columnFamily) throws MasterNotRunningException, ZooKeeperConnectionException, IOException HBaseAdmin admin = new HBaseAdmin(conf); // 判断表是否存在 if(isTableExist(tableName)) System.out.println(" 表 " + tableName + " 已存在 "); //System.exit(0); else // 创建表属性对象 , 表名需要转字节 HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName)); // 创建多个列族 for(String cf : columnFamily) descriptor.addFamily(new HColumnDescriptor(cf)); // 根据对表的配置,创建表 admin.createTable(descriptor); System.out.println(" 表 " + tableName + " 创建成功! ");4.2.4 删除表
public static void dropTable(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException HBaseAdmin admin = new HBaseAdmin(conf); if(isTableExist(tableName)) admin.disableTable(tableName); admin.deleteTable(tableName); System.out.println(" 表 " + tableName + " 删除成功! "); else System.out.println(" 表 " + tableName + " 不存在! ");4.2.5 向表中插入数据
public static void addRowData(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException // 创建 HTable 对象 HTable hTable = new HTable(conf, tableName); // 向表中插入数据 Put put = new Put(Bytes.toBytes(rowKey)); // 向 Put 对象中组装数据 put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value)); hTable.put(put); hTable.close(); System.out.println(" 插入数据成功 ");4.2.6 删除多行数据
public static void deleteMultiRow(String tableName, String... rows) throws IOException HTable hTable = new HTable(conf, tableName); List<Delete> deleteList = new ArrayList<Delete>(); for(String row : rows) Delete delete = new Delete(Bytes.toBytes(row)); deleteList.add(delete); hTable.delete(deleteList); hTable.close();4.2.7 获取所有数据
public static void getAllRows(String tableName) throws IOException HTable hTable = new HTable(conf, tableName); // 得到用于扫描 region 的对象 Scan scan = new Scan(); // 使用 HTable 得到 resultcanner 实现类的对象 ResultScanner resultScanner = hTable.getScanner(scan); for(Result result : resultScanner) Cell[] cells = result.rawCells(); for(Cell cell :以上是关于HBase详解的主要内容,如果未能解决你的问题,请参考以下文章