首页 > 代码库 > HBase基础之Hbase shell常用操作

HBase基础之Hbase shell常用操作

一般操作

查看服务器状态

status

查看hbase版本

version

 

DDL操作

创建表

create ‘member‘,‘member_id‘,‘address‘,‘info‘

创建了3个列族,分别是member_id, address, info

知识点回顾:cf是schema的一部分,而column不是

查看表信息

describe ‘member‘

DESCRIPTION ENABLED member‘, {NAME => ‘address‘, DATA_BLOCK_ENCODING => ‘NONE‘, BLOOMFILTE true R => ‘ROW‘, REPLICATION_SCOPE => ‘0‘, VERSIONS => ‘1‘, COMPRESSION => ‘ NONE‘, MIN_VERSIONS => ‘0‘, TTL => ‘2147483647‘, KEEP_DELETED_CELLS => ‘false‘, BLOCKSIZE => ‘65536‘, IN_MEMORY => ‘false‘, BLOCKCACHE => ‘tru e‘}, {NAME => ‘info‘, DATA_BLOCK_ENCODING => ‘NONE‘, BLOOMFILTER => ‘RO W‘, REPLICATION_SCOPE => ‘0‘, VERSIONS => ‘1‘, COMPRESSION => ‘NONE‘, M IN_VERSIONS => ‘0‘, TTL => ‘2147483647‘, KEEP_DELETED_CELLS => ‘false‘, BLOCKSIZE => ‘65536‘, IN_MEMORY => ‘false‘, BLOCKCACHE => ‘true‘}, {NA ME => ‘member_id‘, DATA_BLOCK_ENCODING => ‘NONE‘, BLOOMFILTER => ‘ROW‘, REPLICATION_SCOPE => ‘0‘, VERSIONS => ‘1‘, COMPRESSION => ‘NONE‘, MIN_ VERSIONS => ‘0‘, TTL => ‘2147483647‘, KEEP_DELETED_CELLS => ‘false‘, BL OCKSIZE => ‘65536‘, IN_MEMORY => ‘false‘, BLOCKCACHE => ‘true‘} 1 row(s) in 0.1800 seconds

查询所有的表

list

删除一个列族

member表建了3个列族,但是发现member_id这个列族是多余的,因为他就是主键,所以我们要将其删除。

alter ‘member‘,‘delete‘=>‘member_id‘ 

drop表

为了测试drop,先创建一个表

create ‘tmp_table‘,‘info‘

删除表之前需要先将表disable再drop

disable ‘tmp_table‘drop ‘tmp_table‘

判断表是否enable

is_enabled ‘member‘

判断表是否disabled

is_disabled ‘member‘

 

DML操作

插入记录

格式:put 表名 row_key cf:column value

put ‘member‘,‘luogankun‘,‘info:age‘,‘27‘put ‘member‘,‘luogankun‘,‘info:birthday‘,‘1986-09-05‘put ‘member‘,‘luogankun‘,‘info:company‘,‘asinainfo-linkage‘put ‘member‘,‘luogankun‘,‘address:country‘,‘china‘put ‘member‘,‘luogankun‘,‘address:province‘,‘beijing‘put ‘member‘,‘luogankun‘,‘address:city‘,‘beijing‘put ‘member‘,‘spring‘,‘info:age‘,‘27‘put ‘member‘,‘spring‘,‘info:birthday‘,‘1986-05-14‘put ‘member‘,‘spring‘,‘info:company‘,‘asinainfo-linkage‘put ‘member‘,‘spring‘,‘address:country‘,‘china‘put ‘member‘,‘spring‘,‘address:province‘,‘hubei‘put ‘member‘,‘spring‘,‘address:city‘,‘wuhan‘put ‘member‘,‘spring‘,‘info:favorite‘,‘shopping‘

知识点回顾: column完全动态扩展,每行可以有不同的columns。

获取一个rowkey的所有数据

格式:get 表名 row_key

get ‘member‘,‘luogankun‘COLUMN                       CELL                                                                               address:city                timestamp=1409122962541, value=http://www.mamicode.com/beijing                                             address:country             timestamp=1409122962468, value=http://www.mamicode.com/china                                               address:province            timestamp=1409122962510, value=http://www.mamicode.com/beijing                                             info:age                    timestamp=1409122962328, value=http://www.mamicode.com/27                                                  info:birthday               timestamp=1409122962399, value=http://www.mamicode.com/1986-09-05                                          info:company                timestamp=1409122962434, value=http://www.mamicode.com/asinainfo-linkage 

知识点回顾:htable按rowkey字典序(1,10,100,2)自动排序,每行包含任意数量的columns,columns按照columnkey(address:city,address:country,address:province,info:age,info:birthday,info:company)自动排序

获取一个id,一个列族的所有数据
格式: get 表名 row_key column

get ‘member‘,‘luogankun‘,‘info‘COLUMN                       CELL                                                                               info:age                    timestamp=1409122962328, value=http://www.mamicode.com/27                                                  info:birthday               timestamp=1409122962399, value=http://www.mamicode.com/1986-09-05                                          info:company                timestamp=1409122962434, value=http://www.mamicode.com/asinainfo-linkage 

获取一个id,一个列族中一个列的所有数据
格式:get 表名 row_key cf:column

get ‘member‘,‘luogankun‘,‘info:age‘COLUMN                       CELL                                                                               info:age                    timestamp=1409122962328, value=http://www.mamicode.com/27

更新一条记录
格式: put 表名 row_key cf:column value
将luogankun的年龄改成18

put ‘member‘,‘luogankun‘,‘info:age‘,‘18‘get ‘member‘,‘luogankun‘,‘info:age‘COLUMN                       CELL                                                                               info:age                    timestamp=1409123175384, value=http://www.mamicode.com/18 

知识点回顾:查询默认返回最新的值

通过timestamp来获取指定版本的数据
格式: get 表名 row_key {COLUMN=>‘cf:column‘,TIMESTAMP=>xxxxxx}

get ‘member‘,‘luogankun‘,{COLUMN=>‘info:age‘,TIMESTAMP=>1409122962328}COLUMN                       CELL                                                                               info:age                    timestamp=1409122962328, value=http://www.mamicode.com/27                                                 "color: #000000;">get ‘member‘,‘luogankun‘,{COLUMN=>‘info:age‘,TIMESTAMP=>1409123175384}
COLUMN CELL info:age timestamp
=1409123175384, value=http://www.mamicode.com/18

知识点回顾:每个column可以有任意数量的values,按timestamp倒序自动排序;tableName+rowkey+column+timestamp==>value

全表扫描
格式:scan 表名

scan ‘member‘ROW                          COLUMN+CELL                                                                        luogankun                   column=address:city, timestamp=1409122962541, value=http://www.mamicode.com/beijing                        luogankun                   column=address:country, timestamp=1409122962468, value=http://www.mamicode.com/china                       luogankun                   column=address:province, timestamp=1409122962510, value=http://www.mamicode.com/beijing                    luogankun                   column=info:age, timestamp=1409123175384, value=http://www.mamicode.com/18                                 luogankun                   column=info:birthday, timestamp=1409122962399, value=http://www.mamicode.com/1986-09-05                    luogankun                   column=info:company, timestamp=1409122962434, value=http://www.mamicode.com/asinainfo-linkage              spring                      column=address:city, timestamp=1409122962828, value=http://www.mamicode.com/wuhan                          spring                      column=address:country, timestamp=1409122962754, value=http://www.mamicode.com/china                       spring                      column=address:province, timestamp=1409122962787, value=http://www.mamicode.com/hubei                      spring                      column=info:age, timestamp=1409122962592, value=http://www.mamicode.com/27                                 spring                      column=info:birthday, timestamp=1409122962623, value=http://www.mamicode.com/1986-05-14                    spring                      column=info:company, timestamp=1409122962670, value=http://www.mamicode.com/asinainfo-linkage              spring                      column=info:favorite, timestamp=1409122963494, value=http://www.mamicode.com/shopping      

删除id为spring的值的‘info:age‘字段
格式:delete 表名 row_key cf:column
先查看

get ‘member‘,‘spring‘,‘info:age‘COLUMN                       CELL                                                                               info:age                    timestamp=1409122962592, value=http://www.mamicode.com/27

再删除

delete ‘member‘,‘spring‘,‘info:age‘
再查看对比是否已经删除
get ‘member‘,‘spring‘,‘info:age‘COLUMN                       CELL                                                                              0 row(s) 

查询表中有多少行
格式:count 表名

count ‘member‘

删除整行
格式: deleteall 表名 row_key

deleteall ‘member‘,‘spring‘

将整张表清空
格式: truncate 表名

truncate ‘member‘Truncating ‘member‘ table (it may take a while): - Disabling table... - Dropping table... - Creating table...

可以看出,hbase是先将掉disable掉,然后drop掉后重建表来实现truncate的功能的。

 

HBase基础之Hbase shell常用操作