一、基本操作
(1)进入HBase客户端命令行:
hbase shell
(2)查看帮助命令:
hbase(main) > help
(3)查看当前数据库中有哪些表:
hbase(main) > list
(4) 查看当前数据库中有哪些命名空间
hbase(main)> list_namespace
二、表的操作
(1)创建表:
hbase(main)> create 'student','cf1'
(2) 插入数据到表
注意:只能一行一行的插入
hbase(main) > put 'student','1001','cf1:name','wgh'
hbase(main) > put 'student','1001','cf1:sex','male'
hbase(main) > put 'student','1001','cf1:age','20'
hbase(main) > put 'student','1002','cf1:name','ymj'
hbase(main) > put 'student','1002','cf1:sex','female'
hbase(main) > put 'student','1002','cf1:age','18'
数据插入后的数据模型:
(3) 扫描查看表数据
hbase(main) > scan 'student' //查看全表
hbase(main) > scan 'student',{STARTROW => '1001', STOPROW => '1002'}
hbase(main) > scan 'student',{STARTROW => '1001'}
(4) 查看表结构
hbase(main):012:0> desc 'student'
(5) 更新指定字段的数据
hbase(main) > put 'student','1001','cf1:name','wgh'
hbase(main) > put 'student','1001','cf1:age','18'
hbase(main) > put 'student','1001','cf1:isNull','' //仅测试空值问题
(6) 查看“指定行”或“指定列族:列”的数据
hbase(main) > get 'student','1001'
hbase(main) > get 'student','1001','cf1:name'
(7) 删除数据
hbase(main) > deleteall 'student','1001' //删除某rowkey的全部数据
delete 'student','1001','cf1:isNull' //删除一个单元格
(8) 清空表数据
hbase(main) > truncate 'student'
提示:清空表的操作顺序为先disable,然后再truncate。
(9) 删除表
hbase(main) > disable 'student' //首先需要先让该表为disable状态
hbase(main) > drop 'student' //删除
检查这个表是否被禁用:
hbase(main) > is_enabled 'hbase_book'
hbase(main) > is_disabled 'hbase_book'
如果被禁用,就恢复被禁用得表 :
hbase(main) > enable 'student'
然后才能drop这个表:
hbase(main) > drop 'student'
提示:如果直接drop表,会报错:
Drop the named table. Table must first be disabled ERROR: Table student is enabled. Disable it first.
(10) 统计表数据行数
hbase(main) > count 'student'
(11) 变更表信息
将info列族中的数据存放3个版本:
hbase(main) > alter 'student',{NAME=>'cf1',VERSIONS=>3}
查看student的最新的版本的数据:
hbase(main) > get 'student','1001'
查看HBase中的多版本:
hbase(main) > get 'student','1001',{COLUMN=>'cf1:name',VERSIONS=>10}
三、常用的Shell操作
(1)satus 显示服务器状态
hbase> status 'bigdata111' //主机名
(2)exists 检查表是否存在,适用于表量特别多的情况
hbase> exists 'hbase_book'
(3)is_enabled/is_disabled 检查表是否启用或禁用
hbase> is_enabled 'hbase_book'
hbase> is_disabled 'hbase_book'
(8)alter 该命令可以改变表和列族的模式
为当前表增加列族:
hbase> alter 'hbase_book', NAME => 'CF2', VERSIONS => 2
为当前表删除列族:
hbase> alter 'hbase_book', 'delete' => 'CF2'
(9)disable禁用一张表
hbase> disable 'hbase_book'
hbase> drop 'hbase_book'
(10)delete
删除一行中一个单元格的值,例如:
hbase> delete 'hbase_book', 'rowKey', 'CF:C'
(11)truncate清空表数据,即禁用表-删除表-创建表
hbase> truncate 'hbase_book'
(12)create
创建多个列族:
hbase> create 't1', {NAME => 'f1'}, {NAME => 'f2'}, {NAME => 'f3'}