hbase shell操作命令大全

来源:互联网 发布:龙虾不会自然死亡 知乎 编辑:程序博客网 时间:2024/06/06 16:56
一、hbase web操作
访问地址 http://h71:60010

h71的ip配置在$HBASE_HOME/conf/hbase-site.xml中

hbase.master.info.port
HBase Master web 界面端口. 设置为 -1 意味着你不想让它运行
默认: 60010

hbase.master.info.bindAddress
HBase Master web 界面绑定的IP地址
默认: 0.0.0.0


ip映射成主机名:

linux: 在/etc/hosts中配置

windows: 在windows系统中的C:\Windows\System32\drivers\etc目录下的hosts文件中配置

192.168.8.71    h71
192.168.8.72    h72
192.168.8.73    h73


二、hbase shell 基本操作:

1.进入hbase shell console
$HBASE_HOME/bin/hbase shell
如果有kerberos认证,需要事先使用相应的keytab进行一下认证(使用kinit命令),认证成功之后再使用hbase shell进入可以使用whoami命令可查看当前用户

hbase(main):029:0> whoamihadoop (auth:SIMPLE)    groups: hadoop    hbase(main):008:0> version1.0.0-cdh5.5.2, rUnknown, Mon Jan 25 16:33:02 PST 2016
list看库中所有表
status 查看当前运行服务器状态
exits '表名字' 判断表存在

2.命名空间Namespace
在关系数据库系统中,命名空间namespace指的是一个表的逻辑分组,同一组中的表有类似的用途。命名空间的概念为即将到来的多租户特性打下基础:
配额管理(Quota Management (HBASE-8410)):限制一个namespace可以使用的资源,资源包括region和table等;
命名空间安全管理(Namespace Security Administration (HBASE-9206)):提供了另一个层面的多租户安全管理;
Region服务器组(Region server groups (HBASE-6721)):一个命名空间或一张表,可以被固定到一组regionservers上,从而保证了数据隔离性。
(1).命名空间管理
命名空间可以被创建、移除、修改。
表和命名空间的隶属关系在在创建表时决定,通过以下格式指定:
<namespace>:<table>

Example:hbase shell中创建命名空间、创建命名空间中的表、移除命名空间、修改命名空间#Create a namespacecreate_namespace 'my_ns'#create my_table in my_ns namespacecreate 'my_ns:my_table', 'fam'#drop namespacedrop_namespace 'my_ns'注意:只有当该空间不存在任何表为空的时候才可以删除,如果存在表的话应该将表删除后再删除该空间,删除表的操作:hbase(main):005:0> disable 'my_ns:my_table'hbase(main):006:0> drop 'my_ns:my_table'#alter namespacealter_namespace 'my_ns', {METHOD => 'set', 'PROPERTY_NAME' => 'PROPERTY_VALUE'}

(2)预定义的命名空间
有两个系统内置的预定义命名空间:
hbase:系统命名空间,用于包含hbase的内部表
default:所有未指定命名空间的表都自动进入该命名空间
Example:指定命名空间和默认命名空间

#namespace=my_ns and table qualifier=bar
create 'my_ns:bar', 'fam'

#namespace=default and table qualifier=bar
create 'bar', 'fam'


3.创建表:

语法:create <table>, {NAME => <family>, VERSIONS => <VERSIONS>}
具体命令:
create 't1', {NAME => 'f1', VERSIONS => 5}
create 't1', {NAME => 'f1'}, {NAME => 'f2'}, {NAME => 'f3'}
省略模式建立列族:
create 't1', 'f1', 'f2', 'f3'
指定每个列族参数:
create 't1', {NAME => 'f1', VERSIONS => 1, TTL => 2592000, BLOCKCACHE => true}
create 't1', 'f1', {SPLITS => ['10', '20', '30', '40']}
(Hbase Region 的分裂(Split) ,请参考:http://blog.sina.com.cn/s/blog_79c0eb870102wdvg.html,http://www.codeweblog.com/hbase-split%E7%9A%84%E4%B8%89%E7%A7%8D%E6%96%B9%E5%BC%8F/,http://blog.csdn.net/dcswin/article/details/52335293)
设置不同参数,提升表的读取性能:
    create 'lmj_test',
        {NAME => 'adn', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROWCOL', REPLICATION_SCOPE => '0', COMPRESSION => 'SNAPPY', VERSIONS => '1', TTL => '15768000', MIN_VERSIONS => '0', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536', ENCODE_ON_DISK => 'true', IN_MEMORY => 'false', BLOCKCACHE => 'false'}, 
        {NAME => 'fixeddim', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROWCOL', REPLICATION_SCOPE => '0', COMPRESSION => 'SNAPPY', VERSIONS => '1', TTL => '15768000', MIN_VERSIONS => '0', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536', ENCODE_ON_DISK => 'true', IN_MEMORY => 'false', BLOCKCACHE => 'false'}, 
        {NAME => 'social', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROWCOL', REPLICATION_SCOPE => '0', COMPRESSION => 'SNAPPY', VERSIONS => '1', TTL => '15768000', MIN_VERSIONS => '0', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536', ENCODE_ON_DISK => 'true', IN_MEMORY => 'false', BLOCKCACHE => 'false'}
每个参数属性都有性能意义,通过合理化的设置可以提升表的性能:
    create 'lmj_test',
        {NAME => 'adn', BLOOMFILTER => 'ROWCOL', VERSIONS => '1', TTL => '15768000', MIN_VERSIONS => '0', COMPRESSION => 'SNAPPY', BLOCKCACHE => 'false'},
        {NAME => 'fixeddim',BLOOMFILTER => 'ROWCOL', VERSIONS => '1', TTL => '15768000', MIN_VERSIONS => '0', COMPRESSION => 'SNAPPY', BLOCKCACHE => 'false'},
        {NAME => 'social',BLOOMFILTER => 'ROWCOL', VERSIONS => '1', TTL => '15768000', MIN_VERSIONS => '0',COMPRESSION => 'SNAPPY', BLOCKCACHE => 'false'}

hbase(main):179:0> create 'scores','grade','course'

4.在创建的表中插入数据:
hbase(main):180:0> put 'scores','zhangsan01','course:math','99'
hbase(main):181:0> put 'scores','zhangsan01','course:art','90'
hbase(main):182:0> put 'scores','zhangsan01','grade:','101'
hbase(main):184:0> put 'scores','zhangsan02','course:math','66'
hbase(main):185:0> put 'scores','zhangsan02','course:art','60'
hbase(main):186:0> put 'scores','zhangsan02','grade:','102'
hbase(main):201:0> put 'scores','lisi01','course:math','89'
hbase(main):202:0> put 'scores','lisi01','course:art','89'
hbase(main):203:0> put 'scores','lisi01','grade:','201'


5.数据查询请参考我的另一篇文章:http://blog.csdn.net/m0_37739193/article/details/73615016


6.delete 删除数据
删除指定行中指定列:
语法:delete <table>, <rowkey>,  <family:column> , <timestamp>(必须指定列名,删除其所有版本数据)
delete 'scores','zhangsan01','course:math'
删除整行数据(可不指定列名):
语法:deleteall <table>, <rowkey>,  <family:column> , <timestamp>
deleteall 'scores','zhangsan02'
(Put,Delete,Get,Scan这四个类都是org.apache.hadoop.hbase.client的子类,可以到官网API去查看详细信息)


7.count统计表中记录数
count 'scores', {INTERVAL => 100, CACHE => 500}
#每100条显示一次,缓存区为500


8.清空表:truncate 'scores'


9.修改表结构:先disable后enable

例如:修改表scores的cf的TTL为180天hbase(main):017:0> disable 'scores'hbase(main):018:0> alter 'scores',{NAME=>'grade',TTL=>'15552000'},{NAME=>'course', TTL=>'15552000'}Updating all regions with the new schema...1/1 regions updated.Done.Updating all regions with the new schema...1/1 regions updated.Done.0 row(s) in 2.2200 seconds改变多版本号:hbase(main):029:0> alter 'scores',{NAME=>'grade',VERSIONS=>3}Updating all regions with the new schema...0/1 regions updated.1/1 regions updated.Done.0 row(s) in 2.4020 seconds(网上都说修改表结构必须先先disable后enable,但是我没有做这个操作,直接alter也成功了啊,不知道这样做有没有什么影响,目前还不太了解)hbase(main):020:0> enable 'scores'

10.表操作权限

(1).分配权限

grant 'hadoop','RW','scores'    #分配给用户hadoop表scores的读写权限注意:一开始我分配权限的时候总是报错:hbase(main):038:0> grant 'hadoop','RW','scores'ERROR: DISABLED: Security features are not available

解决:
[hadoop@h71 ~]$ vi hbase-1.0.0-cdh5.5.2/conf/hbase-site.xml

添加:<property>  <name>hbase.superuser</name>  <value>root,hadoop</value></property><property>  <name>hbase.coprocessor.region.classes</name>  <value>org.apache.hadoop.hbase.security.access.AccessController</value></property><property>  <name>hbase.coprocessor.master.classes</name>  <value>org.apache.hadoop.hbase.security.access.AccessController</value></property><property>  <name>hbase.rpc.engine</name>  <value>org.apache.hadoop.hbase.ipc.SecureRpcEngine</value></property><property>  <name>hbase.security.authorization</name>  <value>true</value></property>

同步hbase配置(我的hbase集群为h71(主),h72(从),h73(从)):
[hadoop@h71 ~]$ cat /home/hadoop/hbase-1.0.0-cdh5.5.2/conf/regionservers|xargs -i -t scp /home/hadoop/hbase-1.0.0-cdh5.5.2/conf/hbase-site.xml hadoop@{}:/home/hadoop/hbase-1.0.0-cdh5.5.2/conf/hbase-site.xml
scp /home/hadoop/hbase-1.0.0-cdh5.5.2/conf/hbase-site.xml hadoop@h72:/home/hadoop/hbase-1.0.0-cdh5.5.2/conf/hbase-site.xml 
hbase-site.xml                                                                                                                                                                                             100% 2038     2.0KB/s   00:00    
scp /home/hadoop/hbase-1.0.0-cdh5.5.2/conf/hbase-site.xml hadoop@h73:/home/hadoop/hbase-1.0.0-cdh5.5.2/conf/hbase-site.xml 
hbase-site.xml                                                                                                                                                                                             100% 2038     2.0KB/s   00:00
重启hbase集群


HBase提供的五个权限标识符:RWXCA,分别对应着READ('R'), WRITE('W'), EXEC('X'), CREATE('C'), ADMIN('A')
HBase提供的安全管控级别包括:
Superuser:拥有所有权限的超级管理员用户。通过hbase.superuser参数配置
Global:全局权限可以作用在集群所有的表上。
Namespace :命名空间级。
Table:表级。
ColumnFamily:列簇级权限。
Cell:单元级。
和关系数据库一样,权限的授予和回收都使用grant和revoke,但格式有所不同。grant语法格式:
grant user permissions table column_family column_qualifier


(2)查看权限

hbase(main):010:0> user_permission 'scores'User                                                         Namespace,Table,Family,Qualifier:Permission                                                                                                                                      hadoop                                                      default,scores,,: [Permission: actions=READ,WRITE]                                                                                                                              1 row(s) in 0.2530 seconds
(3)收回权限
hbase(main):006:0> revoke 'hadoop','scores'


11.hbase shell脚本
既然是shell命令,当然也可以把所有的hbase shell命令写入到一个文件内,想Linux shell脚本程序那样去顺序的执行所有命令。如同写linux shell,把所有hbase shell命令书写在一个文件内,然后执行如下命令即可:

[hadoop@h71 hbase-1.0.0-cdh5.5.2]$ vi hehe.txt(这个文件名随便起,正规点的话可以起test.hbaseshell)create 'hui','cf'listdisable 'hui'drop 'hui'list[hadoop@h71 hbase-1.0.0-cdh5.5.2]$ bin/hbase shell hehe.txt


参考:
http://blog.csdn.net/longshenlmj/article/details/48317567#hbase-web???¦??
http://blog.csdn.net/u010967382/article/details/37878701?utm_source=tuicool&utm_medium=referral
http://www.cnblogs.com/nexiyi/p/hbase_shell.html

http://blog.csdn.net/lzm1340458776/article/details/43492415

原创粉丝点击