Hbase基础之hbase shell操作
- 命令表
- HBase DDL 和 DML 命令
- 连接集群
hbase shell
- 创建表
# 语法:create <table>, {NAME => <family>, VERSIONS => <VERSIONS>}
# 例如:创建表t1,有两个family name:f1,f2,且版本数均为2
hbase(main)> create 't1',{NAME => 'f1', VERSIONS => 2},{NAME => 'f2', VERSIONS => 2}
create 'test_db:test_table',{NAME => 'F1', VERSIONS => 2,DATA_BLOCK_ENCODING=>'FAST_DIFF'},{NAME => 'F2', VERSIONS => 2},
SPLITS => ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
- 删除表
分两步:首先disable,然后drop
hbase(main)> disable 't1'
hbase(main)> drop 't1'
- 查看表的结构
# 语法:describe <table>
# 例如:查看表t1的结构
hbase(main)> describe 't1'
- 修改表结构
# 修改表结构必须先disable
# 语法: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'
- 创建名称空间
# 类似关系型的库database
create_namespace 'test'
- 展示现有名称空间
list_namespace
- 创建表的时候添加namespace
create 'test:user','base_info'
- 显示某个名称空间下有哪些表
list_namespace_tables 'test'
权限管理
- 分配权限
# 语法 : grant <user> <permissions> <table> <column family> <column qualifier> 参数后面用逗号分隔
# 权限用五个字母表示: "RWXCA".
# READ('R'), WRITE('W'), EXEC('X'), CREATE('C'), ADMIN('A')
# 例如,给用户‘test'分配对表t1有读写的权限,
hbase(main)> grant 'test','RW','t1'
- 查看权限
# 语法:user_permission <table>
# 例如,查看表t1的权限列表
hbase(main)> user_permission 't1'
- 收回权限
# 与分配权限类似,语法:revoke <user> <table> <column family> <column qualifier>
# 例如,收回test用户在表t1上的权限
hbase(main)> revoke 'test','t1'
- 插入数据
- put ‘表名’,‘rowkey的值’,’列族:列标识符‘,’值‘
# 语法:put <table>,<rowkey>,<family:column>,<value>,<timestamp>
# 例如:给表t1的添加一行记录:rowkey是rowkey001,family name:f1,column name:col1,value:value01,timestamp:系统默认
hbase(main)> put 't1','rowkey001','f1:col1','value01'
用法比较单一。
put 'user','rowkey_10','base_info:username','Tom'
put 'user','rowkey_10','base_info:birthday','2014-07-10'
put 'user','rowkey_10','base_info:sex','1'
put 'user','rowkey_10','base_info:address','Tokyo'
put 'user','rowkey_16','base_info:username','Mike'
put 'user','rowkey_16','base_info:birthday','2014-07-10'
put 'user','rowkey_16','base_info:sex','1'
put 'user','rowkey_16','base_info:address','beijing'
put 'user','rowkey_22','base_info:username','Jerry'
put 'user','rowkey_22','base_info:birthday','2014-07-10'
put 'user','rowkey_22','base_info:sex','1'
put 'user','rowkey_22','base_info:address','Newyork'
put 'user','rowkey_24','base_info:username','Nico'
put 'user','rowkey_24','base_info:birthday','2014-07-10'
put 'user','rowkey_24','base_info:sex','1'
put 'user','rowkey_24','base_info:address','shanghai'
put 'user','rowkey_25','base_info:username','Rose'
put 'user','rowkey_25','base_info:birthday','2014-07-10'
put 'user','rowkey_25','base_info:sex','1'
put 'user','rowkey_25','base_info:address','Soul'
- 查询表中的所有数据
# 语法:scan <table>, {COLUMNS => [ <family:column>,.... ], LIMIT => num}
# 另外,还可以添加STARTROW、TIMERANGE和FITLER等高级功能
# HBase中一般存储数据量都很大 很少使用全表查询 scan会加上一些条件限制
# 例如:扫描表t1的前5条数据
hbase(main)> scan 't1',{LIMIT=>5}
查询表中的数据行数
# 语法:count <table>, {INTERVAL => intervalNum, CACHE => cacheNum}
# INTERVAL设置多少行显示一次及对应的rowkey,默认1000;CACHE每次去取的缓存区大小,默认是10,调整该参数可提高查询速度
# 例如,查询表t1中的行数,每100条显示一次,缓存区为500
hbase(main)> count 't1', {INTERVAL => 100, CACHE => 500}
- Scan查询中添加限制条件
scan '名称空间:表名', {COLUMNS => ['列族名1', '列族名2'], LIMIT => 10, STARTROW => '起始的rowkey'} # 通过COLUMNS LIMIT STARTROW 等条件缩小查询范围
#LIMIT=>2 限制输出两行
scan 'user' ,{COLUMNS =>['base_info'],LIMIT=>2}
## 返回结果
ROW COLUMN+CELL
rowkey_10 column=base_info:address, timestamp=1558323139732, value=Tokyo
rowkey_10 column=base_info:birthday, timestamp=1558323139636, value=2014-07-10
rowkey_10 column=base_info:sex, timestamp=1558323139678, value=1
rowkey_10 column=base_info:username, timestamp=1558323918953, value=Tom4
rowkey_16 column=base_info:address, timestamp=1558323139963, value=beijing
rowkey_16 column=base_info:birthday, timestamp=1558323139866, value=2014-07-10
rowkey_16 column=base_info:sex, timestamp=1558323139907, value=1
#STARTROW 限制起始的Rowkey
scan 'user' ,{COLUMNS =>['base_info'],LIMIT=>2,STARTROW=>'rowkey_16'}
#返回结果:
ROW COLUMN+CELL
rowkey_16 column=base_info:address, timestamp=1558323139963, value=beijing
rowkey_16 column=base_info:birthday, timestamp=1558323139866, value=2014-07-10
rowkey_16 column=base_info:sex, timestamp=1558323139907, value=1
rowkey_22 column=base_info:address, timestamp=1558323140188, value=Newyork
rowkey_22 column=base_info:birthday, timestamp=1558323140107, value=2014-07-10
rowkey_22 column=base_info:sex, timestamp=1558323140143, value=1
rowkey_22 column=base_info:username, timestamp=1558323140036, value=Jerry
- scan查询添加过滤器
- ROWPREFIXFILTER rowkey 前缀过滤器
scan 'user', {ROWPREFIXFILTER=>'rowkey_22'}
#显示结果
ROW COLUMN+CELL
rowkey_22 column=base_info:address, timestamp=1558323140188, value=Newyork
rowkey_22 column=base_info:birthday, timestamp=1558323140107, value=2014-07-10
rowkey_22 column=base_info:sex, timestamp=1558323140143, value=1
rowkey_22 column=base_info:username, timestamp=1558323140036, value=Jerry
1 row(s)
Took 0.0120 seconds
- 查询某个rowkey的数据
# 语法:get <table>,<rowkey>,[<family:column>,....]
# 例如:查询表t1,rowkey001中的f1下的col1的值
hbase(main)> get 't1','rowkey001', 'f1:col1'
# 或者:
hbase(main)> get 't1','rowkey001', {COLUMN=>'f1:col1'}
# 查询表t1,rowke002中的f1下的所有列值
hbase(main)> get 't1','rowkey001'
- 查询某个列簇的数据
get 'user','rowkey_16','base_info'
get 'user','rowkey_16','base_info:username'
get 'user', 'rowkey_16', {COLUMN => ['base_info:username','base_info:sex']}
- 删除表中的数据
a )删除行中的某个列值
# 语法:delete <table>, <rowkey>, <family:column> , <timestamp>,必须指定列名
# 例如:删除表t1,rowkey001中的f1:col1的数据
hbase(main)> delete 't1','rowkey001','f1:col1'
注:将删除改行f1:col1列所有版本的数据
b )删除行
# 语法:deleteall <table>, <rowkey>, <family:column> , <timestamp>,可以不指定列名,删除整行数据
# 例如:删除表t1,rowk001的数据
hbase(main)> deleteall 't1','rowkey001'
c)删除表中的所有数据
# 语法: truncate <table>
# 其具体过程是:disable table -> drop table -> create table
# 例如:删除表t1的所有数据
hbase(main)> truncate 't1'
- 清空数据
truncate 'user'
- 操作列簇
alter 'user', NAME => 'f2'
alter 'user', 'delete' => 'f2'
- HBase 追加型数据库 会保留多个版本数据
desc 'user'
Table user is ENABLED
user
COLUMN FAMILIES DESCRIPTION
{NAME => 'base_info', VERSIONS => '1', EVICT_BLOCKS_ON_CLOSE => 'false', NEW_VERSION_B
HE_DATA_ON_WRITE => 'false', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', MI
ER => 'NONE', CACHE_INDEX_ON_WRITE => 'false', IN_MEMORY => 'false', CACHE_BLOOM
se', COMPRESSION => 'NONE', BLOCKCACHE => 'false', BLOCKSIZE => '65536'}
# VERSIONS=>'1'说明最多可以显示一个版本 修改数据
put 'user','rowkey_10','base_info:username','Tom'
# 指定显示多个版本
get 'user','rowkey_10',{COLUMN=>'base_info:username',VERSIONS=>2}
# 修改可以显示的版本数量
alter 'user',NAME=>'base_info',VERSIONS=>10
- 通过时间戳查询
- 通过TIMERANGE 指定时间范围
scan 'user',{COLUMNS => 'base_info', TIMERANGE => [1558323139732, 1558323139866]}
get 'user','rowkey_10',{COLUMN=>'base_info:username',VERSIONS=>2,TIMERANGE => [1558323904130, 1558323918954]}
- 通过时间戳过滤器 指定具体时间戳的值
scan 'user',{FILTER => 'TimestampsFilter (1558323139732, 1558323139866)'}
get 'user','rowkey_10',{COLUMN=>'base_info:username',VERSIONS=>2,FILTER => 'TimestampsFilter (1558323904130, 1558323918954)'}
- 获取最近多个版本的数据
get 'user','rowkey_10',{COLUMN=>'base_info:username',VERSIONS=>10}
COLUMN CELL
base_info:username timestamp=1558323918953, value=Tom4
base_info:username timestamp=1558323904133, value=Tom3
base_info:username timestamp=1558323758696, value=Tom2
base_info:username timestamp=1558323139575, value=Tom
- 通过指定时间戳获取不同版本的数据
get 'user','rowkey_10',{COLUMN=>'base_info:username',TIMESTAMP=>1558323904133}
#返回结果如下
COLUMN CELL
base_info:username timestamp=1558323904133, value=Tom3
get 'user','rowkey_10',{COLUMN=>'base_info:username',TIMESTAMP=>1558323918953}
#返回结果如下
COLUMN CELL
base_info:username timestamp=1558323918953, value=Tom4
Region管理
1)移动region
# 语法:move 'encodeRegionName', 'ServerName'
# encodeRegionName指的regioName后面的编码,ServerName指的是master-status的Region Servers列表
# 示例
hbase(main)>move '4343995a58be8e5bbc739af1e91cd72d', 'db-41.xxx.xxx.org,60020,1390274516739'
2)开启/关闭region
# 语法:balance_switch true|false
hbase(main)> balance_switch
3)手动split
# 语法:split 'regionName', 'splitKey'
4)手动触发major compaction
#语法:
#Compact all regions in a table:
#hbase> major_compact 't1'
#Compact an entire region:
#hbase> major_compact 'r1'
#Compact a single column family within a region:
#hbase> major_compact 'r1', 'c1'
#Compact a single column family within a table:
#hbase> major_compact 't1', 'c1'
配置管理及节点重启
1)修改hdfs配置
hdfs配置位置:/etc/hadoop/conf
# 同步hdfs配置
cat /home/hadoop/slaves|xargs -i -t scp /etc/hadoop/conf/hdfs-site.xml hadoop@{}:/etc/hadoop/conf/hdfs-site.xml
#关闭:
cat /home/hadoop/slaves|xargs -i -t ssh hadoop@{} "sudo /home/hadoop/cdh4/hadoop-2.0.0-cdh4.2.1/sbin/hadoop-daemon.sh --config /etc/hadoop/conf stop datanode"
#启动:
cat /home/hadoop/slaves|xargs -i -t ssh hadoop@{} "sudo /home/hadoop/cdh4/hadoop-2.0.0-cdh4.2.1/sbin/hadoop-daemon.sh --config /etc/hadoop/conf start datanode"
2)修改hbase配置
hbase配置位置:
# 同步hbase配置
cat /home/hadoop/hbase/conf/regionservers|xargs -i -t scp /home/hadoop/hbase/conf/hbase-site.xml hadoop@{}:/home/hadoop/hbase/conf/hbase-site.xml
# graceful重启
cd ~/hbase
bin/graceful_stop.sh --restart --reload --debug inspurXXX.xxx.xxx.org