hbase命令集(shell 命令,如建表,清空表,增删改查)

时间:2021-06-03 08:34:57
 两篇可以参考的文章,讲的不错 

http://www.cnblogs.com/nexiyi/p/hbase_shell.html 

(http://blog.iyunv.com/wulantian/article/details/41011297)
  ==============================
hbase web操作

访问地址 http://hmaster:60010,
hmaster的ip配置在$HBASE_HOME/conf/hbase-site.xml中
ip映射成主机名,在env/hosts中配置(在windows系统中的C:\Windows\System32\drivers\etc目录下的hosts文件中配置)

hbase shell 操作:

进入hbase console:hbase shell
help查看基本命令集合
list看库中所有表
status 查看当前运行服务器状态
version 版本号


1)建表

    语法:create <table>, {NAME => <family>, VERSIONS => <VERSIONS>}
语句:
hbase(main):004:0> exists 'test'
hbase(main):005:0> create 'test','cf'
hbase> create 't1', {NAME => 'f1', VERSIONS => 5}
hbase> create 't1', {NAME => 'f1'}, {NAME => 'f2'}, {NAME => 'f3'}
省略模式建立列族
hbase> create 't1', 'f1', 'f2', 'f3'
指定每个列族参数
hbase> create 't1', {NAME => 'f1', VERSIONS => 1, TTL => 2592000, BLOCKCACHE => true}
hbase> create 't1', 'f1', {SPLITS => ['10', '20', '30', '40']}
hbase> create 't1', 'f1', {SPLITS_FILE => 'splits.txt'}
hbase> # Optionally pre-split the table into NUMREGIONS, using
hbase> # SPLITALGO ("HexStringSplit", "UniformSplit" or classname)
hbase> create 't1', 'f1', {NUMREGIONS => 15, SPLITALGO => 'HexStringSplit'}
设置不同参数,提升表的读取性能。
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'}


2)建好表后,查看表结构:describe 

    得出
{NAME => 'lmj_test', 
FAMILIES => 
[
{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'}
]
}


3)清空表:truncate ‘lmj_test’
4)删除表:

        分两步,首先disable 'lmj_test',然后drop 'lmj_test'


5)修改表结构:先disable后enable

        alter 't1', {NAME => 'f1'}, {NAME => 'f2', METHOD => 'delete'}
例如:修改表test1的cf的TTL为180天
hbase(main)> disable 'test1'
hbase(main)> alter 'test1',{NAME=>'body',TTL=>'15552000'},{NAME=>'meta', TTL=>'15552000'}
hbase(main)> enable 'test1'


6)对表中记录的操作(4种行操作)

    put 增加一行
put 't1', 'r1', 'c1', 'value', ts1
put 'lmj_test','00001','adn:adn_3','aaa',1432483200000
put 'lmj_test','00001','fixeddim:appcategory_1','1',1432483200000
put 'lmj_test','00001','fixeddim:interest_15','100',1432483200000
get查询对应数据(可以指定行、列族、列、版本)
get 'lmj_test','000000104257464',{TIMESTAMP=>1432483200000}
delete 删除数据
删除指定行中指定列:
delete <table>, <rowkey>,  <family:column> , <timestamp>(必须指定列名,删除其所有版本数据)
delete 'lmj_test','000000104257464','f1:col1'
删除整行数据(可不指定列名):
deleteall <table>, <rowkey>,  <family:column> , <timestamp>
deleteall 'lmj_test','000000104257464'
scan 扫描全表,指定过滤条件,返回对应行
scan 'lxw_hbase', {LIMIT => 1}
其他条件继续添加在大括号中
以上4个操作类是 org.apache.hadoop.hbase.client的子类,参考官网API查看详细信息
count统计表中记录数
count 'lxw_hbase', {INTERVAL => 100, CACHE => 500}
#每100条显示一次,缓存区为500


7)表操作权限

    给用户分配对每个表的操作权限,有RWXCA五种,对应READ, WRITE, EXEC, CREATE, ADMIN
grant 'liu_mja','RW','lxw_hbase'    #分配给用户liu_mja表lxw_hbase的读写权限
还可以 查看权限
user_permission 'lxw_hbase'
收回权限
revoke 'liu_mja','lxw_hbase'


8)命名空间

    关系数据库系统中,命名空间namespace是表的逻辑分组,同一组中的表有类似的用途。
以下引自:http://blog.iyunv.com/wulantian/article/details/41011297
hbase的表也有命名空间的管理方式,命名空间的概念为即将到来的多租户特性打下基础:
配额管理( Quota Management (HBASE-8410)):限制一个namespace可以使用的资源,资源包括region和table等; 
命名空间安全管理( Namespace Security Administration (HBASE-9206)):提供了另一个层面的多租户安全管理; 
Region服务器组(Region server groups (HBASE-6721)):一个命名空间或一张表,可以被固定到一组 regionservers上,从而保证了数据隔离性。 
命名空间可以被创建、移除、修改。
建表时可以指定命名空间,格式如下:<namespace>:<table>
#Create a namespace
create_namespace 'my_ns'
#create my_table in my_ns namespace
create 'my_ns:my_table', 'fam'
#drop namespace
drop_namespace 'my_ns'
#alter namespace
alter_namespace 'my_ns', {METHOD => 'set', 'PROPERTY_NAME' => 'PROPERTY_VALUE'}
预定义的命名空间:
有两个系统内置的预定义命名空间
hbase   系统命名空间,用于包含hbase的内部表 
default 所有未指定命名空间的表都自动进入该命名空间
使用默认的命名空间
#namespace=default and table qualifier=bar
create 'bar', 'fam'
指定命名空间
#namespace=foo and table qualifier=bar

create 'foo:bar', 'fam'

原文网址:http://www.iyunv.com/thread-130713-1-1.html