这里我们用一个学生成绩表作为例子,对HBase的基本操作和基本概念进行讲解:

下面是学生的成绩表:

name      grade        course:math           course:art
Tom         1                87                   97
Jerry       2               100                   80

这里grad对于表来说是一个列,course对于表来说是一个列族,这个列族由两个列组成:math和art,当然我们可以根据我们的需要在course中建立更多的列族,如computer,physics等相应的列添加入course列族.有了上面的想法和需求,我们就可以在HBase中建立相应的数据表啦!

1, 建立一个表格 scores 具有两个列族grad 和courese

hbase(main):002:0> create 'scores', 'grade', 'course'

0 row(s) in 4.1610 seconds

2,查看当先HBase中具有哪些表

hbase(main):003:0> list

scores

1 row(s) in 0.0210 seconds

3,查看表的构造

hbase(main):004:0> describe 'scores'

{NAME => 'scores', IS_ROOT => 'false', IS_META => 'false', FAMILIES => [{NAME => 'course', BLOOMFILTER => 'false', IN_MEMORY => 'false', LENGTH => '2147483647', BLOCKCACHE => 'false', VERSIONS => '3', TTL => '-1', COMPRESSION => 'NONE'}, {NAME => 'grade', BLOOMFILTER => 'false', IN_MEMORY => 'false', LENGTH => '2147483647', BLOCKCACHE => 'false', VERSIONS => '3', TTL => '-1', COMPRESSION => 'NONE'}]}

1 row(s) in 0.0130 seconds

4, 加入一行数据,行名称为 Tom 列族grad的列名为”” 值位1

hbase(main):005:0> put 'scores', 'Tom', 'grade:', '1'

0 row(s) in 0.0070 seconds

5,给Tom这一行的数据的列族添加一列 <math,87>

hbase(main):006:0> put 'scores', 'Tom', 'course:math', '87'

0 row(s) in 0.0040 seconds

6,给Tom这一行的数据的列族添加一列 <art,97>

hbase(main):007:0> put 'scores', 'Tom', 'course:art', '97'

0 row(s) in 0.0030 seconds

7, 加入一行数据,行名称为 Jerry 列族grad的列名为”” 值位2

hbase(main):008:0> put 'scores', 'Jerry', 'grade:', '2'

0 row(s) in 0.0040 seconds

8,给Jerry这一行的数据的列族添加一列 <math,100>

hbase(main):009:0> put 'scores', 'Jerry', 'course:math', '100'

0 row(s) in 0.0030 seconds

9,给Jerry这一行的数据的列族添加一列 <art,80>

hbase(main):010:0> put 'scores', 'Jerry', 'course:art', '80'

0 row(s) in 0.0050 seconds

10,查看scores表中Tom的相关数据

hbase(main):011:0> get 'scores', 'Tom'

COLUMN                       CELL

course:art                  timestamp=1224726394286, value=97

course:math                 timestamp=1224726377027, value=87

grade:                      timestamp=1224726360727, value=1

3 row(s) in 0.0070 seconds

11,查看scores表中所有数据

hbase(main):012:0> scan 'scores'

ROW                          COLUMN+CELL

Tom                         column=course:art, timestamp=1224726394286, value=97

Tom                         column=course:math, timestamp=1224726377027, value=87

Tom                         column=grade:, timestamp=1224726360727, value=1

Jerry                        column=course:art, timestamp=1224726424967, value=80

Jerry                        column=course:math, timestamp=1224726416145, value=100

Jerry                        column=grade:, timestamp=1224726404965, value=2

6 row(s) in 0.0410 seconds
01-12 08:15