北在北方

太白枝头看,花开不计年,杯中浮日月,楼外是青天。

导航

HBase数据库常用操作命令

Posted on 2013-04-13 18:36  CN.programmer.Luxh  阅读(1289)  评论(0编辑  收藏  举报

一个student表,表的逻辑模型如下: 行键为学号,时间戳是自动添加的,列族name是名字,列族score是各科分数。行键是唯一的。

行键 时间戳 列族name 列族score
20130001 T2 'zhangsan'  
T3   score:Chinese='90'
T4   score:English='80'
20130002 T5 'lisi'  
T6   score:Chinese='95'
...... Tn    

 

 进入HBase的shell:

[coder@h1 hbase-0.90.5]$ bin/hbase shell
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version 0.90.5, r1212209, Fri Dec  9 05:40:36 UTC 2011

hbase(main):001:0> 

1、创建student表

hbase(main):002:0> create 'student','name','score'
0 row(s) in 2.0570 seconds

hbase(main):003:0>

  student:表名

  name:列族

  score:列族

2、查看已创建的表

hbase(main):001:0> list
TABLE                                                                                                                          
student                                                                                                                        
1 row(s) in 0.7590 seconds

hbase(main):002:0> 

3、向表中插入数据

  使用put命令插入数据,参数为表名、行名(行键)、列名、值,其中列名需要列族作为前缀。

  1)向student表中插入学号(行键)为2013001、名字为zhangsan的记录

hbase(main):001:0> put 'student','2013001','name:','zhangsan'
0 row(s) in 1.1090 seconds

  2)查看表的记录

hbase(main):002:0> scan 'student'
ROW                              COLUMN+CELL                                                                                   
 2013001                         column=name:, timestamp=1365846723665, value=zhangsan                                         
1 row(s) in 0.2110 seconds

  3)给学号为2013001的学生记录分数

hbase(main):003:0> put 'student','2013001','score:Chinese','90'
0 row(s) in 0.0360 seconds

  

hbase(main):005:0> put 'student','2013001','score:English','80'
0 row(s) in 0.0290 seconds

 

hbase(main):006:0> scan 'student'
ROW                              COLUMN+CELL                                                                                   
 2013001                         column=name:, timestamp=1365846723665, value=zhangsan                                         
 2013001                         column=score:Chinese, timestamp=1365846877328, value=90                                       
 2013001                         column=score:English, timestamp=1365846957089, value=80                                       
1 row(s) in 0.0580 seconds

  4)查看表中记录总数

hbase(main):007:0> count 'student'
1 row(s) in 0.0990 seconds

4、查看一行记录

  使用get命令,参数为表名、行键

hbase(main):007:0> get 'student','2013001'
COLUMN                           CELL                                                                                          
 name:                           timestamp=1365846723665, value=zhangsan                                                       
 score:Chinese                   timestamp=1365846877328, value=90                                                             
 score:English                   timestamp=1365846957089, value=80                                                             
3 row(s) in 0.0350 seconds

5、判读一个表是否存在

hbase(main):001:0> exists 'student'
Table student does exist                                                                                                       
0 row(s) in 1.0400 seconds

hbase(main):002:0> exists 'student2'
Table student2 does not exist                                                                                                  
0 row(s) in 0.0150 seconds

6、更新一条记录

hbase(main):001:0> put 'student','2013001','score:Chinese','100'
0 row(s) in 0.8070 seconds

hbase(main):002:0> get 'student','2013001'
COLUMN                           CELL                                                                                          
 name:                           timestamp=1365846723665, value=zhangsan                                                       
 score:Chinese                   timestamp=1365848492778, value=100                                                            
 score:English                   timestamp=1365846957089, value=80                                                             
3 row(s) in 0.0690 seconds

hbase(main):003:0> 

7、获取某一列的值

hbase(main):002:0> get 'student','2013001','score'
COLUMN                           CELL                                                                                          
 score:Chinese                   timestamp=1365848492778, value=100                                                            
 score:English                   timestamp=1365846957089, value=80                                                             
2 row(s) in 0.0340 seconds

hbase(main):003:0> get 'student','2013001','score:Chinese'
COLUMN                           CELL                                                                                          
 score:Chinese                   timestamp=1365848492778, value=100                                                            
1 row(s) in 0.0320 seconds

hbase(main):004:0> 

8、删除整行

  把行键为2013002的这一条记录删掉

hbase(main):001:0> scan 'student'
ROW                              COLUMN+CELL                                                                                   
 2013001                         column=name:, timestamp=1365846723665, value=zhangsan                                         
 2013001                         column=score:Chinese, timestamp=1365848492778, value=100                                      
 2013001                         column=score:English, timestamp=1365846957089, value=80                                       
 2013002                         column=name:, timestamp=1365847639588, value=lisi                                             
 2013002                         column=score:Chinese, timestamp=1365847666351, value=70                                       
 2013002                         column=score:English, timestamp=1365847709403, value=85                                       
2 row(s) in 0.7380 seconds

hbase(main):002:0> deleteall 'student','2013002'
0 row(s) in 0.0150 seconds

hbase(main):003:0> scan 'student'               
ROW                              COLUMN+CELL                                                                                   
 2013001                         column=name:, timestamp=1365846723665, value=zhangsan                                         
 2013001                         column=score:Chinese, timestamp=1365848492778, value=100                                      
 2013001                         column=score:English, timestamp=1365846957089, value=80                                       
1 row(s) in 0.0390 seconds

hbase(main):004:0> 

9、删除表

  删除表之前需要先disable表

hbase(main):005:0> disable 'user'
0 row(s) in 2.1600 seconds

hbase(main):006:0> drop 'user'   
0 row(s) in 1.3750 seconds