zoukankan      html  css  js  c++  java
  • HBase数据库常用操作命令

    一个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
  • 相关阅读:
    office2010卸载出现安装程序包的语言不支持
    Red Hat 7.5 Yum Replacement
    0801作业
    0730会员注册
    0730作业2:随机生成6位的字符串验证码,要求包含数字、大小写字母
    0730作业一练习:输出学习阶段目标
    0727作业1 根据学员英文名找到学员对象
    注册
    随机数
    Map
  • 原文地址:https://www.cnblogs.com/luxh/p/3011231.html
Copyright © 2011-2022 走看看