HBase 命令大全详解
本文全面整理 HBase 的核心命令,涵盖表管理、数据操作、集群运维等关键场景,结合示例说明最佳实践。基于 HBase 2.4+ 版本。
一、表管理命令
1. 表创建与配置
# 基本创建(单列族)
create 'user', 'info'
# 多列族创建
create 'customer',
{NAME => 'basic', VERSIONS => 3},
{NAME => 'contact', BLOOMFILTER => 'ROW'}
# 预分区创建(避免热点)
create 'sensor_data', 'metrics',
{NUMREGIONS => 15, SPLITALGO => 'HexStringSplit'}
# 高级配置
create 'logs',
{NAME => 'data',
COMPRESSION => 'SNAPPY',
TTL => '2592000', # 30天过期
BLOCKSIZE => '65536'}
2. 表结构操作
# 查看所有表
list
# 查看表结构
describe 'user'
# 修改列族配置
alter 'user',
{NAME => 'info', VERSIONS => 5}, # 增加版本数
{NAME => 'contact', COMPRESSION => 'GZ'}
# 添加新列族
alter 'user', NAME => 'preferences'
# 删除列族
alter 'user', 'delete' => 'preferences'
# 启用/禁用表
disable 'user'
enable 'user'
# 删除表(需先禁用)
disable 'temp_table'
drop 'temp_table'
# 检查表状态
is_enabled 'user'
is_disabled 'user'
二、数据操作命令
1. 数据写入
# 单条写入
put 'user', 'u001', 'info:name', 'Alice'
put 'user', 'u001', 'info:age', '28'
put 'user', 'u001', 'contact:email', 'alice@example.com'
# 带时间戳写入
put 'user', 'u002', 'info:name', 'Bob', 1672531200000
# 计数器操作
incr 'counters', 'page_views', 'stats:views', 1 # 增加1
get_counter 'counters', 'page_views', 'stats:views' # 获取值
2. 数据查询
# 单行查询
get 'user', 'u001'
# 指定列查询
get 'user', 'u001', {COLUMN => 'info:name'}
# 多版本查询
get 'user', 'u001', {COLUMN => 'info:age', VERSIONS => 3}
# 时间范围查询
get 'user', 'u001', {TIMERANGE => [1672500000000, 1672600000000]}
# 扫描全表
scan 'user'
# 带过滤器的扫描
scan 'user',
{ROWPREFIXFILTER => 'u00'}, # 行键前缀
{COLUMNS => ['info:name', 'contact:email']},
{LIMIT => 10}
3. 数据删除
# 删除单元格
delete 'user', 'u001', 'contact:email'
# 删除整行
deleteall 'user', 'u002'
# 删除特定版本
delete 'user', 'u001', 'info:age', 1672531200000
# 清空表(保留结构)
truncate 'temp_data'
三、高级查询过滤器
1. 常用过滤器
# 行键过滤器
scan 'user', {FILTER => "RowFilter(=, 'binaryprefix:u00')"}
# 值过滤器
scan 'user', {FILTER => "ValueFilter(=, 'substring:@gmail.com')"}
# 列前缀过滤
scan 'user', {FILTER => "ColumnPrefixFilter('na')"}
# 多条件组合
scan 'user',
{FILTER => "
(ValueFilter(=, 'binary:Alice') OR
(RowFilter(>=, 'binary:u005') AND
FamilyFilter(=, 'binary:contact')
"}
2. 分页查询
# 第一页(100条)
scan 'large_table', {LIMIT => 100}
# 后续分页(使用STARTROW)
scan 'large_table', {STARTROW => 'last_row_key', LIMIT => 100}
四、Region管理命令
1. Region操作
# 查看Region分布
list_regions 'user'
# 手动分裂Region
split 'user,1670000000000.abcdefffff', 'u010' # 按指定行键分裂
# Region合并
merge_region 'ENCODED_REGIONNAME1', 'ENCODED_REGIONNAME2'
# 移动Region
move 'ENCODED_REGIONNAME', 'target-regionserver'
2. 预分区管理
# 查看分区边界
get_splits 'user'
# 修改分区数
alter 'user', METHOD => 'table_att', MAX_FILESIZE => '10737418240' # 10GB
五、集群管理命令
1. 状态检查
# 集群状态
status 'summary'
# RegionServer状态
status 'regionserver', 'rs1.example.com,16020'
# 表状态检查
hbase hbck
2. 负载均衡
# 开启/关闭自动均衡
balance_switch true
balance_switch false
# 手动触发均衡
balancer
3. 快照管理
# 创建快照
snapshot 'user', 'user_backup_202305'
# 查看快照
list_snapshots
# 从快照恢复
clone_snapshot 'user_backup_202305', 'user_restored'
# 删除快照
delete_snapshot 'user_backup_202301'
六、权限控制命令
1. 用户与权限
# 创建用户
grant 'alice', 'RW' # 读写权限
# 表级授权
grant 'bob', 'R', '@user' # 只读user表
# 列族级授权
grant 'carol', 'W', 'user', 'contact' # 只能写contact列族
# 查看权限
user_permission 'user'
# 回收权限
revoke 'bob', 'user'
2. 命名空间管理
# 创建命名空间
create_namespace 'finance'
# 在命名空间建表
create 'finance:transactions', 'data'
# 命名空间授权
grant 'auditor', 'R', '@finance'
# 删除命名空间(需先删表)
drop_namespace 'test'
七、数据导入导出
1. 批量导入工具
# TSV文件导入
hbase org.apache.hadoop.hbase.mapreduce.ImportTsv \
-Dimporttsv.separator=',' \
-Dimporttsv.columns=HBASE_ROW_KEY,info:name,info:age \
user /data/user_data.csv
# HFile格式导入(高效)
hbase org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles \
/hfile_path/user user
2. 数据导出
# 导出为SequenceFile
hbase org.apache.hadoop.hbase.mapreduce.Export \
user /export/user_data
# 导出快照
hbase org.apache.hadoop.hbase.snapshot.ExportSnapshot \
-snapshot user_snapshot \
-copy-to hdfs://backup/hbase/snapshots/
八、性能调优命令
1. 缓存与刷写
# 修改RegionServer内存配置
hbase shell <<EOF
alter 'user',
CONFIGURATION => {
'hbase.regionserver.global.memstore.size' => '0.4',
'hfile.block.cache.size' => '0.5'
}
EOF
# 手动触发MemStore刷写
flush 'user'
flush 'region_name' # 刷写特定Region
2. Compaction管理
# 手动触发Major Compaction
major_compact 'user'
major_compact 'user', 'info' # 指定列族
# 设置Compaction策略
alter 'user',
CONFIGURATION => {
'hbase.hstore.engine.class' =>
'org.apache.hadoop.hbase.regionserver.StripeStoreEngine'
}
九、故障排查命令
1. 日志与监控
# 查看RegionServer日志
tail -f /var/log/hbase/hbase-regionserver-*.log
# 获取Region信息
get 'hbase:meta', 'user,,1670000000000.abcdefffff.', 'info:regioninfo'
2. 修复工具
# 元数据修复
hbase hbck -repair
# Region一致性修复
hbase hbck -fixAssignments
# 孤立HFile清理
hbase clean --cleanZk
十、Java API示例
1. 基本连接
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "zk1,zk2,zk3");
try (Connection conn = ConnectionFactory.createConnection(config);
Table table = conn.getTable(TableName.valueOf("user"))) {
// 数据查询
Get get = new Get(Bytes.toBytes("u001"));
Result result = table.get(get);
String name = Bytes.toString(result.getValue(
Bytes.toBytes("info"), Bytes.toBytes("name")));
}
2. 批量写入
List<Put> puts = new ArrayList<>();
puts.add(new Put(Bytes.toBytes("u003"))
.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("Charlie"))
.addColumn(Bytes.toBytes("contact"), Bytes.toBytes("email"), Bytes.toBytes("charlie@example.com")));
table.put(puts); // 批量提交
最佳实践总结
-
行键设计黄金法则:
# 加盐示例:MD5(用户ID)_用户ID put 'users', '1a3e5f_user1234', 'info:name', 'Alice'
-
批量操作优化:
# 使用BufferedMutator提高写入效率 hbase org.apache.hadoop.hbase.client.BufferedMutatorExample
-
监控关键指标:
# 查看MemStore使用 hbase shell> status 'detailed' # 监控Compaction队列 echo "list_compactions" | hbase shell
-
安全操作规范:
# 修改重要表前创建快照 snapshot 'critical_data', 'pre_alter_backup' # 生产环境禁用危险命令 hbase.security.exec.permission.checks=true
-
跨集群迁移:
# 使用Snapshot迁移 hbase org.apache.hadoop.hbase.snapshot.ExportSnapshot \ -snapshot user_snapshot \ -copy-from hdfs://old-cluster/hbase \ -copy-to hdfs://new-cluster/hbase
注意事项:
- 生产环境操作前务必在测试集群验证
- 禁用表操作会影响服务可用性
- Major Compaction会显著增加IO负载
- 定期执行
hbase hbck
检查元数据健康
通过熟练掌握这些命令,您可以高效管理PB级HBase集群,应对高并发读写场景,构建稳定可靠的大数据存储系统。