Hbase(一)简介及基本操作

Hbase(一)简介及基本操作

1 简介

1.1 什么是Hbase

​ HBase的原型是Google的BigTable论文,受到了该论文思想的启发,目前作为Hadoop的子项目来开发维护,用于支持结构化的数据存储。

​ HBase是一个高可靠性、高性能、面向列、可伸缩的分布式存储系统,利用HBASE技术可在廉价PC Server上搭建起大规模结构化存储集群。

​ HBase的目标是存储并处理大型的数据,更具体来说是仅需使用普通的硬件配置,就能够处理由成千上万的行和列所组成的大型数据。

​ HBase是Google Bigtable的开源实现,但是也有很多不同之处。比如:Google Bigtable利用GFS作为其文件存储系统,HBase利用Hadoop HDFS作为其文件存储系统;Google运行MAPREDUCE来处理Bigtable中的海量数据,HBase同样利用Hadoop MapReduce来处理HBase中的海量数据;Google Bigtable利用Chubby作为协同服务,HBase利用Zookeeper作为对应。

官方网站:http://hbase.apache.org

1.2 Hbase特点

1)海量存储

​ Hbase适合存储PB级别的海量数据,在PB级别的数据以及采用廉价PC存储的情况下,能在几十到百毫秒内返回数据。这与Hbase的极易扩展性息息相关。正式因为Hbase良好的扩展性,才为海量数据的存储提供了便利。

2)列式存储

​ 这里的列式存储其实说的是列族存储,Hbase是根据列族来存储数据的。列族下面可以有非常多的列,列族在创建表的时候就必须指定。

3)极易拓展

​ Hbase的扩展性主要体现在两个方面,一个是基于上层处理能力(RegionServer)的扩展,一个是基于存储的扩展(HDFS)。
通过横向添加RegionSever的机器,进行水平扩展,提升Hbase上层的处理能力,提升Hbsae服务更多Region的能力。

​ 备注:RegionServer的作用是管理region、承接业务的访问,这个后面会详细的介绍通过横向添加Datanode的机器,进行存储层扩容,提升Hbase的数据存储能力和提升后端存储的读写能力。

4)高并发

​ 由于目前大部分使用Hbase的架构,都是采用的廉价PC,因此单个IO的延迟其实并不小,一般在几十到上百ms之间。这里说的高并发,主要是在并发的情况下,Hbase的单个IO延迟下降并不多。能获得高并发、低延迟的服务。

5)稀疏

​ 稀疏主要是针对Hbase列的灵活性,在列族中,你可以指定任意多的列,在列数据为空的情况下,是不会占用存储空间的。

1.3 HBase架构

​ Hbase是由Client、Zookeeper、HMaster、HRegionServer、HDFS等几个组件组成,下面来介绍一下几个组件的相关功能:

1)Client

Client包含了访问Hbase的接口,另外Client还维护了对应的cache来加速Hbase的访问,比如cache的.META.元数据的信息。

2)Zookeeper

HBase通过Zookeeper来做master的高可用、RegionServer的监控、元数据的入口以及集群配置的维护等工作。具体工作如下:

  • 通过Zoopkeeper来保证集群中只有1个master在运行,如果master异常,会通过竞争机制产生新的master提供服务
  • 通过Zoopkeeper来监控RegionServer的状态,当RegionSevrer有异常的时候,通过回调的形式通知Master RegionServer上下线的信息
  • 通过Zoopkeeper存储元数据的统一入口地址

3)HMaster

HMaster节点的主要职责如下:

  • 为RegionServer分配Region
  • 维护整个集群的负载均衡
  • 维护集群的元数据信息
  • 发现失效的Region,并将失效的Region分配到正常的RegionServer上
  • 当RegionSever失效的时候,协调对应Hlog的拆分

4)HRegionServer

HregionServer直接对接用户的读写请求,是真正的“干活”的节点。它的功能概括如下:

  • 管理master为其分配的Region
  • 处理来自客户端的读写请求
  • 负责和底层HDFS的交互,存储数据到HDFS
  • 负责Region变大以后的拆分
  • 负责Storefile的合并工作

5)HDFS

HDFS为Hbase提供最终的底层数据存储服务,同时为HBase提供高可用(Hlog存储在HDFS)的支持,具体功能概括如下:

  • 提供元数据和表数据的底层分布式存储服务
  • 数据多副本,保证的高可靠和高可用性

1.4 HBase中的角色

1.4.1 HMaster

功能

1.监控RegionServer

2.处理RegionServer故障转移

3.处理元数据的变更

4.处理region的分配或转移

5.在空闲时间进行数据的负载均衡

6.通过Zookeeper发布自己的位置给客户端

1.4.2 RegionServer

功能

1.负责存储HBase的实际数据

2.处理分配给它的Region

3.刷新缓存到HDFS

4.维护Hlog

5.执行压缩

6.负责处理Region分片

1.4.3 其他组件

1.Write-Ahead Logs

​ HBase的修改记录,当对HBase读写数据的时候,数据不是直接写进磁盘,它会在内存中保留一段时间(时间以及数据量阈值可以设定)。但把数据保存在内存中可能有更高的概率引起数据丢失,为了解决这个问题,数据会先写在一个叫做Write-Ahead logfile的文件中,然后再写入内存中。所以在系统出现故障的时候,数据可以通过这个日志文件重建。

2.Region

​ Hbase表的分片,HBase表会根据RowKey值被切分成不同的region存储在RegionServer中,在一个RegionServer中可以有多个不同的region。

3.Store

​ HFile存储在Store中,一个Store对应HBase表中的一个列族。

4.MemStore

​ 内存存储,位于内存中,用来保存当前的数据操作,所以当数据保存在WAL中之后,RegsionServer会在内存中存储键值对。

5.HFile

这是在磁盘上保存原始数据的实际的物理文件,是实际的存储文件。StoreFile是以Hfile的形式存储在HDFS的。

2 操作

2.1 基本操作

# 启动/停止hbase
start-hbase.sh
stop-hbase.sh
# 进入交互界面
hbase shell
# 版本
version
# 状态
status
# 帮助
help
table_help
# 用户信息
whoami
# 查看当前数据库有哪些表
list
# 退出
quit
-- 1 进入hbase交互界面
[root@hadoop1 ~]# hbase shell
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/hbase/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/hadoop/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version 1.3.5, rb59afe7b1dc650ff3a86034477b563734e8799a9, Wed Jun  5 15:57:14 PDT 2019
-- 2 查看当前数据库中有哪些表
hbase(main):001:0> list
TABLE                                                                                           
person
-- 3 查看帮助命令
hbase(main):002:0> help
HBase Shell, version 1.3.5, rb59afe7b1dc650ff3a86034477b563734e8799a9, Wed Jun  5 15:57:14 PDT 2019
Type 'help "COMMAND"', (e.g. 'help "get"' -- the quotes are necessary) for help on a specific command.
Commands are grouped. Type 'help "COMMAND_GROUP"', (e.g. 'help "general"') for help on a command group.
COMMAND GROUPS:
  Group name: general
  Commands: status, table_help, version, whoami
  Group name: ddl
  Commands: alter, alter_async, alter_status, create, describe, disable, disable_all, drop, drop_all, enable, enable_all, exists, get_table, is_disabled, is_enabled, list, locate_region, show_filters
  Group name: namespace
  Commands: alter_namespace, create_namespace, describe_namespace, drop_namespace, list_namespace, list_namespace_tables
  Group name: dml
  Commands: append, count, delete, deleteall, get, get_counter, get_splits, incr, put, scan, truncate, truncate_preserve
  Group name: tools
  Commands: assign, balance_switch, balancer, balancer_enabled, catalogjanitor_enabled, catalogjanitor_run, catalogjanitor_switch, close_region, compact, compact_rs, flush, major_compact, merge_region, move, normalize, normalizer_enabled, normalizer_switch, split, splitormerge_enabled, splitormerge_switch, trace, unassign, wal_roll, zk_dump
  Group name: replication
  Commands: add_peer, append_peer_tableCFs, disable_peer, disable_table_replication, enable_peer, enable_table_replication, get_peer_config, list_peer_configs, list_peers, list_replicated_tables, remove_peer, remove_peer_tableCFs, set_peer_tableCFs, show_peer_tableCFs
  Group name: snapshots
  Commands: clone_snapshot, delete_all_snapshot, delete_snapshot, delete_table_snapshots, list_snapshots, list_table_snapshots, restore_snapshot, snapshot
  Group name: configuration
  Commands: update_all_config, update_config
  Group name: quotas
  Commands: list_quotas, set_quota
  Group name: security
  Commands: grant, list_security_capabilities, revoke, user_permission
  Group name: procedures
  Commands: abort_procedure, list_procedures
  Group name: visibility labels
  Commands: add_labels, clear_auths, get_auths, list_labels, set_auths, set_visibility
SHELL USAGE:
Quote all names in HBase Shell such as table and column names.  Commas delimit
command parameters.  Type <RETURN> after entering a command to run it.
Dictionaries of configuration used in the creation and alteration of tables are
Ruby Hashes. They look like this:
  {'key1' => 'value1', 'key2' => 'value2', ...}
and are opened and closed with curley-braces.  Key/values are delimited by the
'=>' character combination.  Usually keys are predefined constants such as
NAME, VERSIONS, COMPRESSION, etc.  Constants do not need to be quoted.  Type
'Object.constants' to see a (messy) list of all constants in the environment.

If you are using binary keys or values and need to enter them in the shell, use
double-quote'd hexadecimal representation. For example:
  hbase> get 't1', "key\x03\x3f\xcd"
  hbase> get 't1', "key\003\023\011"
  hbase> put 't1', "test\xef\xff", 'f1:', "\x01\x33\x40"
The HBase shell is the (J)Ruby IRB with the above HBase-specific commands added.
For more on the HBase Shell, see http://hbase.apache.org/book.html

2.2 表操作

小案例熟悉下

create 'tbl_user','cf'
describe 'tbl_user'
put 'tbl_user','003','cf:name','bigdata'
get 'tbl_user','003'
scan 'tbl_user'
flush 'tbl_user'
drop 'tbl_user'   -- drop前 disable 'tbl_user'

1 创建表

hbase(main):004:0> create 'student','info'
0 row(s) in 1.4590 seconds
=> Hbase::Table - student

2 插入数据到表

hbase(main):006:0> put 'student','1001','info:sex','male'
0 row(s) in 0.1220 seconds
hbase(main):007:0> put 'student','1001','info:age','20'
0 row(s) in 0.0160 seconds
hbase(main):009:0> scan 'student'
ROW                                         COLUMN+CELL
 1001                                       column=info:age, timestamp=1587888866072, value=20
 1001                                       column=info:sex, timestamp=1587888848526, value=male 1 row(s) in 0.0590 seconds
hbase(main):010:0> put 'student','1002','info:name','nml'
0 row(s) in 0.0110 seconds
hbase(main):011:0> put 'student','1002','info:sex','female'
0 row(s) in 0.0080 seconds
hbase(main):012:0> put 'student','1002','info:age','25'
0 row(s) in 0.0040 seconds

3 扫描查看表数据

hbase(main):013:0> scan 'student'
ROW                                         COLUMN+CELL
 1001                                       column=info:age, timestamp=1587888866072, value=20
 1001                                       column=info:sex, timestamp=1587888848526, value=male
 1002                                       column=info:age, timestamp=1587888977838, value=25
 1002                                       column=info:name, timestamp=1587888938159, value=nml
 1002                                       column=info:sex, timestamp=1587888960602, value=female                                                                      
2 row(s) in 0.0170 seconds

hbase(main):015:0> scan 'student',{STARTROW => '1001',STOPROW => '1001'}
ROW                                         COLUMN+CELL
 1001                                       column=info:age, timestamp=1587888866072, value=20
 1001                                       column=info:sex, timestamp=1587888848526, value=male
1 row(s) in 0.0120 seconds

hbase(main):003:0> scan 'student',{STARTROW => '1001'}
ROW                                         COLUMN+CELL
 1001                                       column=info:age, timestamp=1587888866072, value=20
 1001                                       column=info:sex, timestamp=1587888848526, value=male
 1002                                       column=info:age, timestamp=1587888977838, value=25
 1002                                       column=info:name, timestamp=1587888938159, value=nml
 1002                                       column=info:sex, timestamp=1587888960602, value=female                                                                      
2 row(s) in 0.1590 seconds

4 查看表结构

hbase(main):004:0> describe 'student'
Table student is ENABLED
student
COLUMN FAMILIES DESCRIPTION
{NAME => 'info', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSIO
N => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}                                                                 
1 row(s) in 0.0420 seconds

5 更新指定字段的数据

hbase(main):005:0> put 'student','1001','info:name','ljx'
0 row(s) in 0.0850 seconds
hbase(main):006:0> put 'student','1001','info:age','23'
0 row(s) in 0.0080 seconds

6 查看“指定行”或“指定列族:列”的数据

hbase(main):007:0> get 'student','1001'
COLUMN                                      CELL
 info:age                                   timestamp=1587889299043, value=23
 info:name                                  timestamp=1587889283222, value=ljx
 info:sex                                   timestamp=1587888848526, value=male
1 row(s) in 0.0290 seconds
hbase(main):008:0> get 'student','1001','info:name'
COLUMN                                      CELL
 info:name                                  timestamp=1587889283222, value=ljx
1 row(s) in 0.0130 seconds

7 统计表数据行数

hbase(main):009:0> count 'student'
2 row(s) in 0.0290 seconds
=> 2

8 删除数据

-- 删除某rowkey的全部数据
hbase(main):011:0> deleteall 'student','1001'
0 row(s) in 0.0170 seconds
hbase(main):012:0> scan 'student'
ROW                                         COLUMN+CELL
 1002                                       column=info:age, timestamp=1587888977838, value=25
 1002                                       column=info:name, timestamp=1587888938159, value=nml
 1002                                       column=info:sex, timestamp=1587888960602, value=female                                                                      
1 row(s) in 0.0110 seconds
-- 删除某rowkey的某一列的数据
hbase(main):013:0> delete 'student','1002','info:sex'
0 row(s) in 0.0180 seconds
hbase(main):014:0> scan 'student'
ROW                                         COLUMN+CELL
 1002                                       column=info:age, timestamp=1587888977838, value=25
 1002                                       column=info:name, timestamp=1587888938159, value=nml
1 row(s) in 0.0150 seconds

9 清空表数据
提示:清空表的操作顺序为先disable,然后再truncate

hbase(main):015:0> truncate 'student'
Truncating 'student' table (it may take a while):
 - Disabling table...
 - Truncating table...
0 row(s) in 3.7440 seconds
hbase(main):016:0> scan 'student'
ROW                                         COLUMN+CELL
0 row(s) in 0.2160 seconds

10 删除表 ——先disable再drop

hbase(main):018:0> disable 'student'
0 row(s) in 2.2520 seconds
hbase(main):019:0> drop 'student'
0 row(s) in 1.2750 seconds
hbase(main):020:0> list
TABLE
person
phone
test
user
user_mr
5 row(s) in 0.0080 seconds
=> ["person", "phone", "test", "user", "user_mr"]

11 变更表信息
将info列族中的数据存放3个版本

hbase(main):026:0> alter 'student',{NAME => 'info',VERSION => 3}
Unknown argument ignored for column family info: 1.8.7
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 2.1830 seconds

hbase(main):027:0> get 'student','1001',{COLUMN=>'info:name',VERSIONS=>3}
COLUMN                                      CELL
 info:name                                  timestamp=1587889658149, value=nml
1 row(s) in 0.0180 seconds
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值