Hbase shell 基本命令教程
一、基本命令
启动 打开 hbase shell
hbase shell
1
1.1 获取帮助
获取帮助
help
获取命令的详细信息
help 'status'
1.2 查看服务器状态
status
1.3 查看版本信息
version
二、关于表的操作
2.1 查看所有表
list
2.2 创建表
命令格式: create ‘表名称’, ‘列族名称1’,‘列族名称2’,‘列名称N’
创建一张名为student的表,包含基本信息(user)列族
create "student","user";
2.3 查看表的基本信息
命令格式:desc ‘表名’
describe ‘Student’
1
2.4 表的启用/禁用
enable和disable可以启用/禁用这个表,is\_enabled和is\_disabled来检查表是否被禁用
禁用表
disable 'student'
检查表是否被禁用
is_disabled 'student'
启用表
enable 'student'
检查表是否被启用
is_enabled 'student'
2.5 检查表是否存在
exists 'student'
2.6 删除表
删除表前需要先禁用表
disable 'student'
删除表
drop 'student'
三、增删改
3.1 添加列族
命令格式: alter ‘表名’, ‘列族名’
alter 'student', 'teacher'
3.2 删除列族
命令格式:alter ‘表名’, {NAME => ‘列族名’, METHOD => ‘delete’}
alter 'Student', {NAME => 'teacherInfo', METHOD => 'delete'}
3.3 更改列族存储版本的限制
默认情况下,列族只存储一个版本的数据,如果需要存储多个版本的数据,则需要修改列族的属性。修改后可通过desc命令查看。
alter 'Student',{NAME=>'baseInfo',VERSIONS=>3}
3.4 插入数据
命令格式:put ‘表名’, ‘行键’,‘列族:列’,‘值’
注意:如果新增数据的行键值、列族名、列名与原有数据完全相同,则相当于更新操作
put "student","10001","user:id",10001
put "student","10001","user:name","honor"
put "student","10001","user:password","123456"
put "student","10001","user:gender","男"
put "student","10001","user:phone","18224799110"
put "student","10001","user:email","[email protected]"
put "student","10001","user:address","贵州省安顺市紫云县"
put "student","10002","user:id",10002
put "student","10002","user:name","张三"
put "student","10002","user:password","123456"
put "student","10002","user:gender","男"
put "student","10002","user:phone","18224711212"
put "student","10002","user:email","[email protected]"
put "student","10002","user:address","贵州省安顺市紫云县"
put "student","10003","user:id",10003
put "student","10003","user:name","王平"
put "student","10003","user:password","123456"
put "student","10003","user:gender","男"
put "student","10003","user:phone","18224799110"
put "student","10003","user:email","[email protected]"
put "student","10003","user:address","贵州省安顺市紫云县"
put "student","10004","user:id",10004
put "student","10004","user:name","李雪"
put "student","10004","user:password","123456"
put "student","10004","user:gender","女"
put "student","10004","user:phone","1223212323"
put "student","10004","user:email","[email protected]"
put "student","10004","user:address","贵州省安顺市紫云县"
put "student","10005","user:id",10005
put "student","10005","user:name","李艳"
put "student","10005","user:password","123456"
put "student","10005","user:gender","女"
put "student","10005","user:phone","153443433333"
put "student","10005","user:email","[email protected]"
put "student","10005","user:address","贵州省安顺市紫云县"
put "student","10006","user:id",10006
put "student","10006","user:name","李丽"
put "student","10006","user:password","122323456"
put "student","10006","user:gender","女"
put "student","10006","user:phone","343344322"
put "student","10006","user:email","[email protected]"
put "student","10006","user:address","贵州省安顺市紫云县"
put "student","10007","user:id",10007
put "student","10007","user:name","李宁"
put "student","10007","user:password","123456"
put "student","10007","user:gender","男"
put "student","10007","user:phone","122345323"
put "student","10007","user:email","[email protected]"
put "student","10007","user:address","贵州省安顺市紫云县"
3.5 获取指定行、指定行中的列族、列的信息
获取指定行中所有列的数据信息
get 'student','10001'
获取指定行中指定列族下所有列的数据信息
get 'student','10002','user'
获取指定行中指定列的数据信息
get 'student','10002','user:name'
3.6 删除指定行、指定行中的列
删除指定行
delete 'student','10002'
删除指定行中指定列的数据
delete 'student','10007','user:name'
四、查询
hbase中访问数据有两种基本的方式:
按指定rowkey获取数据:get方法;
按指定条件获取数据:scan方法。
scan可以设置begin和end参数来访问一个范围内所有的数据。get本质上就是begin和end相等的一种特殊的scan。
4.1Get查询
获取指定行中所有列的数据信息
get 'student','10001'
获取指定行中指定列族下所有列的数据信息
get 'student','1003','user'
# 获取指定行中指定列的数据信息
get 'student','10003','user:name'
4.2 查询整表数据
scan 'student'
4.3 查询指定列簇的数据
scan 'student', {COLUMNS=>'user'}
4.4 条件查询
查询指定列的数据
scan 'student', {COLUMNS=> 'user:birthday'}
除了列(COLUMNS)修饰词外,HBase还支持Limit(限制查询结果行数),STARTROW(ROWKEY起始行,会先根据这个key定位到region,再向后扫描)、STOPROW(结束行)、TIMERANGE(限定时间戳范围)、VERSIONS(版本数)、和FILTER(按条件过滤行)等。
如下代表从rowkey2这个rowkey开始,查找下两个行的最新3个版本的name列的数据:
scan 'student', {COLUMNS=> 'user:name',STARTROW => '10002',STOPROW => '10005',LIMIT=>5, VERSIONS=>3}
注意: 区间取值是 左闭右开
4.5 条件过滤
Filter可以设定一系列条件来进行过滤。如我们要查询值等于24的所有数据:
scan 'student', FILTER=>"ValueFilter(=,'age:20')"
值包含yale的所有数据:
scan 'Student', FILTER=>"ValueFilter(=,'substring:yale')"
列名中的前缀为birth的:
scan 'Student', FILTER=>"ColumnPrefixFilter('birth')"
FILTER中支持多个过滤条件通过括号、AND和OR进行组合:
列名中的前缀为birth且列值中包含1998的数据
scan 'Student', FILTER=>"ColumnPrefixFilter('birth') AND ValueFilter ValueFilter(=,'substring:1998')"
PrefixFilter用于对Rowkey的前缀进行判断:
scan 'Student', FILTER=>"PrefixFilter('wr')"