Hbase shell命令练习

准备

启动HBase,要确认已启动Hadoop集群,Zookeeper

[root@node5 hadoop-2.5.1]# start-hbase.sh

进入Hbase

[root@node5 ~]# hbase shell

显示Hbase中的表

hbase(main):001:0> list

1、创建user表,包含info、data两个列族

创建表方式一:

hbase(main):002:0> create 'user','info','data'

创建表方式二:

create 'user',{NAME => 'info',VERSIONS => '3'}
hbase(main):009:0> list
hbase(main):011:0> create 'm_user', {NAME => 'info', VERSIONS => '3'},{NAME => 'data', VERSIONS => '3'}

2、向user表中插入信息,row key为rk0001,列族info中添加name列标识符,值为matrix

hbase(main):015:0> put 'user','rk0001','info:name','matrix'

3、向user表中插入信息,row key为rk0001,列族info中添加gender列标示符,值为female

hbase(main):016:0> put 'user','rk0001','info:gender','female'

4、向user表中插入信息,row key为rk0001,列族info中添加age列标示符,值为20

hbase(main):017:0> put 'user','rk0001','info:age',20

5、向user表中插入信息,row key为rk0001,列族data中添加pic列标示符,值为picture

hbase(main):019:0> put 'user','rk0001','data:pic','picture'

6、获取user表中row key为rk0001的所有信息

hbase(main):022:0> get 'user','rk0001'

7、获取user表中row key为rk0001,info列族的所有信息

hbase(main):023:0> get 'user','rk0001','info'

8、获取user表中row key为rk0001,info列族的name、gender、age标示符的信息

hbase(main):024:0> get 'user','rk0001','info:name','info:gender','info:age'

9、获取user表中row key为rk0001,info、data列族的信息

hbase(main):025:0> get 'user','rk0001','info','data'
hbase(main):027:0> get 'user','rk0001',{COLUMN => ['info','data']}
hbase(main):029:0> get 'user','rk0001',{COLUMN => ['info:name','data:pic']}

10、获取user表中row key为rk0001,列族为info、版本号最新5个的信息

hbase(main):030:0> get 'user','rk0001',{COLUMN => 'info' ,VERSION => 2}
hbase(main):031:0> get 'user','rk0001',{COLUMN => 'info:name',VERSION => 5}

11、查询user表中所有信息

hbase(main):032:0> scan 'user'

11.1、查询user表中列族为info的信息

hbase(main):033:0> scan 'user', {COLUMNS => 'info'}
hbase(main):033:0> scan 'user', {COLUMNS => 'info'}
hbase(main):034:0> scan 'user', {COLUMNS => 'info', RAW => true, VERSIONS => 5}

11.2、查询user表中列族为info和data的信息

hbase(main):035:0> scan 'user', {COLUMNS => ['info', 'data']}
hbase(main):036:0> scan 'user', {COLUMNS => ['info:name', 'data:pic']}

12、查询user表中列族为info、列标示符为name的信息

hbase(main):037:0> scan 'user', {COLUMNS => 'info:name'}

13、查询user表中列族为info、列标示符为name的信息,并且版本最新的5个

hbase(main):038:0> scan 'user', {COLUMNS => 'info:name', VERSIONS => 5}

14、查询user表中列族为info和data且列标示符中含有a字符的信息

hbase(main):041:0> scan 'user', {COLUMNS => ['info', 'data'], FILTER => "(QualifierFilter(=,'substring:a'))"}

15、查询user表中列族为info,rk范围是[rk0001, rk0003)的数据

hbase(main):050:0> scan 'user', {COLUMNS => 'info', STARTROW => 'rk0001', ENDROW => 'rk0003'}

16、查询user表中row key以rk字符开头的

hbase(main):051:0> scan 'user',{FILTER=>"PrefixFilter('rk')"}

17、查询user表中指定范围的数据

hbase(main):056:0* scan 'user', {TIMERANGE => [1458441570629, 1458441753608]}

18、删除数据

18.1、删除user表row key为rk0001,列标示符为info:name的数据

hbase(main):057:0> delete 'user', 'rk0001', 'info:name'

18.2、删除user表row key为rk0001,列标示符为info:name,timestamp为1392383705316的数据

hbase(main):059:0> delete 'user', 'rk0001', 'data:pic', 1458441753608

19、清空user表中的数据

hbase(main):060:0> truncate 'user'

20、修改表结构

20.1、首先停用user表(新版本不用)

hbase(main):061:0> disable 'user'

20.2、添加两个列族f1和f2

hbase(main):062:0> alter 'user', NAME => 'f1'

hbase(main):063:0> alter 'user', NAME => 'f2'

20.3、启用表

hbase(main):064:0> enable 'user'

20.1、查看表结构

hbase(main):066:0> desc 'user'

disable ‘user’(新版本不用)

20.4、删除一个列族:

alter ‘user’, NAME => ‘f1’, METHOD => ‘delete’ 或 alter ‘user’, ‘delete’ => ‘f1’

hbase(main):067:0> alter 'user', NAME => 'f1', METHOD => 'delete'

20.1、添加列族f1同时删除列族f2

hbase(main):070:0> alter 'user', {NAME => 'f1'}, {NAME => 'f2', METHOD => 'delete'}

20.5、删除列族f1:

hbase(main):071:0> alter 'user', 'delete' => 'f1'

20.6、将user表的f1列族版本号改为5

hbase(main):073:0> alter 'user', NAME => 'info', VERSIONS => 5

20.7、删除表

hbase(main):074:0> disable 'user'

hbase(main):075:0> drop 'user'

在Hbase Shell中运行出错:

ERROR: java.util.concurrent.ExecutionException: org.apache.hadoop.ipc.RemoteException(java.io.IOException): File /hbase/.tmp/data/default/user/c5d0c0ae3199462216dbf4642987f4d5/.regioninfo could only be replicated to 0 nodes instead of minReplication (=1).  There are 3 datanode(s) running and no node(s) are excluded in this operation.


ERROR: Can't get master address from ZooKeeper; znode data == null
重新格式化namenode。。。。