Skip to content

HBase Shell

HBase Shell 是 HBase 的命令行接口,用于管理和操作 HBase 数据库。

基本命令

表管理

bash
# 创建表
create 'user_profile', 'info', 'address'

# 查看表列表
list

# 查看表结构
describe 'user_profile'

# 禁用表
disable 'user_profile'

# 删除表
drop 'user_profile'

数据操作

bash
# 插入数据
put 'user_profile', 'user001', 'info:name', 'Alice'
put 'user_profile', 'user001', 'info:age', '25'
put 'user_profile', 'user001', 'address:city', 'Beijing'

# 查询单行数据
get 'user_profile', 'user001'

# 查询指定列族
get 'user_profile', 'user001', 'info'

# 查询指定列
get 'user_profile', 'user001', 'info:name'

# 扫描表
scan 'user_profile'

# 扫描指定范围
scan 'user_profile', {STARTROW => 'user001', ENDROW => 'user010'}

# 删除指定列
delete 'user_profile', 'user001', 'info:age'

# 删除整行
deleteall 'user_profile', 'user001'

# 计数
count 'user_profile'

过滤器

bash
# 行键过滤器
scan 'user_profile', {FILTER => "RowFilter(=, 'regexstring:user.*')"}

# 列名过滤器
scan 'user_profile', {FILTER => "ColumnPrefixFilter('info')"}

# 值过滤器
scan 'user_profile', {FILTER => "ValueFilter(=, 'binary:Alice')"}

# 多个过滤器
scan 'user_profile', {FILTER => "RowFilter(=, 'regexstring:user.*') AND ValueFilter(=, 'binary:Alice')"}

批量操作

bash
# 批量插入
put 'user_profile', 'user002', 'info:name', 'Bob'
put 'user_profile', 'user002', 'info:age', '30'
put 'user_profile', 'user003', 'info:name', 'Charlie'
put 'user_profile', 'user003', 'info:age', '35'

# 批量获取
get 'user_profile', ['user001', 'user002', 'user003']

# 批量删除
deleteall 'user_profile', 'user002'
deleteall 'user_profile', 'user003'

高级命令

表配置

bash
# 修改表配置
alter 'user_profile', {NAME => 'info', VERSIONS => 5}

# 添加列族
alter 'user_profile', {NAME => 'preferences'}

# 删除列族
alter 'user_profile', {NAME => 'preferences', METHOD => 'delete'}

压缩

bash
# 手动触发 Major Compact
major_compact 'user_profile'

# 手动触发 Minor Compact
compact 'user_profile'

快照

bash
# 创建快照
snapshot 'user_profile', 'user_profile_snapshot'

# 查看快照列表
list_snapshots

# 从快照恢复
restore_snapshot 'user_profile_snapshot'

# 删除快照
delete_snapshot 'user_profile_snapshot'

权限管理

bash
# 授予权限
grant 'user', 'RWXCA', 'user_profile'

# 撤销权限
revoke 'user', 'user_profile'

# 查看权限
user_permission 'user_profile'

实用技巧

格式化输出

bash
# 禁用自动格式化
scan 'user_profile', {FORMATTER => 'toString'}

# 自定义输出格式
scan 'user_profile', {COLUMNS => ['info:name'], FORMATTER => 'toString'}

性能优化

bash
# 禁用 WAL(提高写入性能,但有数据丢失风险)
put 'user_profile', 'user001', 'info:name', 'Alice', {WAL => false}

# 设置扫描缓存
scan 'user_profile', {CACHE => 100}

# 设置批量大小
scan 'user_profile', {BATCH => 10}

调试信息

bash
# 查看 Region 信息
status 'detailed'

# 查看表的 Region 分布
describe 'user_profile', {FORMAT => 'json'}

# 查看集群状态
status