(一)简介:
HBase是一个分布式的、面向列的开源数据库。HBase是Google Bigtable的开源实现,它利用Hadoop HDFS作为其文件存储系统,利用Hadoop MapReduce来处理HBase中的海量数据,利用Zookeeper作为协同服务。
表结构:
HBase以表的形式存储数据。表有行和列组成。列划分为若干个列族/列簇(column family)。
Row Key | column-family1 | column-family2 | column-family3 | |||
column1 | column2 | column1 | column2 | column3 | column1 | |
key1 | ||||||
key2 | ||||||
key3 |
如上图所示,key1、key2、key3是三条记录的唯一的row key值,column-family1、column-family2、column-family3是三个列族,每个列族下又包括几列,比如column-family1这个列族下包括两列,名字是column1和column2。t1:ab ,t2:dx是由row key1和column-family1—column1唯一确定的一个单元cell。这个cell中有两个数据,ab和dx。两个值的时间戳不一样,分别是t1,t2,hbase会返回最新时间的值给请求者。
名词定义:
1)Row Key
与nosql数据库们一样,row key是用来检索记录的主键。访问hbase table中的行,只有三种方式:
tar zxvf hbase-0.94.10.tar.gz3. 修改数据存储目录,编辑 conf/hbase-site.xml 配置hbase.rootdir
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <property> <name>hbase.rootdir</name> <value>存储目录</value> </property> </configuration>4. 启动Hbase
./bin/start-hbase.shstarting master, logging to /home/onlyone/software/hbase-0.94.10/bin/../logs/hbase-onlyone-master-ubuntu.out
./bin/hbase shell
name | grade | course | |
math | art | ||
Tom | 6 | 99 | 100 |
Yangli | 15 |
hbase(main):001:0> create 'scores' ,'grade','course'
hbase(main):002:0> list TABLE scores test 2 row(s) in 0.1320 secondsdescribe命令来查看表结构
hbase(main):001:0> describe 'scores' DESCRIPTION ENABLED 'scores', {NAME => 'course', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => true 'NONE', REPLICATION_SCOPE => '0', VERSIONS => '3', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => '2147483647', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536', IN_MEMORY => 'false', ENCODE_ON_DISK => 'true', BLOC KCACHE => 'true'}, {NAME => 'grade', DATA_BLOCK_ENCODING => 'NONE', BLOOMF ILTER => 'NONE', REPLICATION_SCOPE => '0', VERSIONS => '3', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => '2147483647', KEEP_DELETED_CELLS => ' false', BLOCKSIZE => '65536', IN_MEMORY => 'false', ENCODE_ON_DISK => 'tru e', BLOCKCACHE => 'true'} 1 row(s) in 0.7240 secondsps:记得所有的表名、列名都需要加上引号
hbase(main):007:0> put 'scores','Tom','grade','6' 0 row(s) in 0.0790 seconds hbase(main):008:0> put 'scores','Tom','course:math','99' 0 row(s) in 0.0060 seconds hbase(main):009:0> put 'scores','Tom','course:art','100' 0 row(s) in 0.0100 seconds //扫描表数据 hbase(main):010:0> scan 'scores' ROW COLUMN+CELL Tom column=course:art, timestamp=1376493359159, value=100 Tom column=course:math, timestamp=1376493340514, value=99 Tom column=grade:, timestamp=1376493316800, value=6 1 row(s) in 0.0530 secondsput命令比较简单:
hbase(main):012:0> get 'scores' ,'Tom' COLUMN CELL course:art timestamp=1376493359159, value=100 course:math timestamp=1376493340514, value=99 grade: timestamp=1376493316800, value=6 3 row(s) in 0.0170 seconds hbase(main):013:0> get 'scores' ,'Tom','grade' COLUMN CELL grade: timestamp=1376493316800, value=6 1 row(s) in 0.0080 seconds hbase(main):014:0> get 'scores' ,'Tom','course:math' COLUMN CELL course:math timestamp=1376493340514, value=99 1 row(s) in 0.0250 seconds
hbase(main):016:0> delete 'scores' ,'Tom','grade' 0 row(s) in 0.1350 seconds hbase(main):017:0> scan 'scores' ROW COLUMN+CELL Tom column=course:art, timestamp=1376493359159, value=100 Tom column=course:math, timestamp=1376493340514, value=99 1 row(s) in 0.1380 seconds
hbase(main):018:0> count 'scores' 1 row(s) in 0.8500 seconds
shell test.hbaseshell