HBase Shell操作包括:DDL操作和DML操作。
DDL操作命令
HBase Shell命令 | 功能描述 |
---|---|
list | 列出HBase中的所有表 |
create | 创建一张表 |
describe | 列出表的详细信息 |
alter | 修改表的列族 |
disable | 禁用表,使表无效 |
enable | 启用表,使表有效 |
drop | 删除一张表 |
exists | 判断表是否存在 |
具体操作如下:
# 查詢表信息
hbase(main):003:0> list
# 創建表空間
hbase(main):004:0> create_namespace 'cjh'
# 查看表空間
hbase(main):006:0> list_namespace
# 刪除表空間
hbase(main):008:0> drop_namespace 'cjh'
# 在cjh中創建表
hbase(main):011:0> create 'cjh:student','baseinfo', 'schoolinfo'
# 查看指定表空間下的表
hbase(main):013:0> list_namespace_tables 'cjh'
# 查看描述表信息
hbase(main):014:0> describe 'cjh:student'
# 查看指定表是否不可用
hbase(main):015:0> is_disabled 'cjh:student'
false
0 row(s) in 0.0200 seconds
# 查看指定表是否可用
hbase(main):016:0> is_enabled 'cjh:student'
true
0 row(s) in 0.0100 seconds
# 禁用表
hbase(main):018:0> disable 'cjh:student'
# 启用表
hbase(main):020:0> enable 'cjh:student'
# 删除表前必须要将表disable(禁用),否则删除会失败
hbase(main):022:0> drop 'cjh:student'
ERROR: Table cjh:student is enabled. Disable it first.
# 修改表,添加列族
hbase(main):023:0> alter 'cjh:student','teacherinfo'
# 查看表详情
hbase(main):024:0> desc 'cjh:student'
# 修改表,删除列族
hbase(main):025:0> alter 'cjh:student', NAME=>'teacherinfo', METHOD=>'delete'
修改默认的存贮版本数
hbase(main):040:0> alter 'cjh:student',NAME=>'baseinfo',VERSIONS=>3
DML操作命令
HBase Shell命令 | 功能描述 |
---|---|
put | 向指定的单元添加值 |
scan | 通过扫描表来获取数据 |
get | 获取行或者单元的值 |
count | 统计表中行的数量,一个行键为一行 |
delete | 删除指定对象的值 |
deleteall | 删除整行 |
truncate | 清空表的数据 |
具体操作如下:
# 插入数据
hbase(main):031:0> put 'cjh:student','rowkey', 'baseinfo:name','lijia'
0 row(s) in 0.0540 seconds
hbase(main):032:0> scan 'cjh:student'
ROW COLUMN+CELL
rowkey column=baseinfo:name, timestamp=1586320330442, value=lijia
1 row(s) in 0.0170 seconds
hbase(main):033:0> put 'cjh:student','rowkey','baseinfo:name','cjh'
0 row(s) in 0.0070 seconds
hbase(main):034:0> scan 'cjh:student'
ROW COLUMN+CELL
rowkey column=baseinfo:name, timestamp=1586320401611, value=cjh
1 row(s) in 0.0050 seconds
hbase(main):035:0> put 'cjh:student','rowkey','baseinfo:age','30'
0 row(s) in 0.0040 seconds
hbase(main):036:0> scan 'cjh:student'
ROW COLUMN+CELL
rowkey column=baseinfo:age, timestamp=1586320442439, value=30
rowkey column=baseinfo:name, timestamp=1586320401611, value=cjh
1 row(s) in 0.0050 seconds
hbase(main):037:0> put 'cjh:student','rowkey','baseinfo:birthday','1986-10-1'
0 row(s) in 0.0040 seconds
hbase(main):038:0> scan 'cjh:student'
ROW COLUMN+CELL
rowkey column=baseinfo:age, timestamp=1586320442439, value=30
rowkey column=baseinfo:birthday, timestamp=1586320512382, value=1986-10-1
rowkey column=baseinfo:name, timestamp=1586320401611, value=cjh
1 row(s) in 0.0150 seconds
# 删除列族中的指定列
hbase(main):028:0> delete 'cjh:student','rowkey4','schoolinfo:name'
# 删除一整行数据
hbase(main):032:0> delete 'cjh:student', 'rowkey4'
# 查询指定行‘rowkey2’的信息
hbase(main):035:0> get 'cjh:student','rowkey2'
COLUMN CELL
baseinfo:age timestamp=1586397540495, value=8
baseinfo:birthday timestamp=1586397559259, value=2012-5-1
baseinfo:name timestamp=1586397492894, value=ant
schoolinfo:location timestamp=1586397647560, value=chahaerlu
schoolinfo:name timestamp=1586397629168, value=chahaerluxiaoxue
# 查询指定行‘rowkey2’的baseinfo列族的信息
hbase(main):036:0> get 'cjh:student','rowkey2','baseinfo'
COLUMN CELL
baseinfo:age timestamp=1586397540495, value=8
baseinfo:birthday timestamp=1586397559259, value=2012-5-1
baseinfo:name timestamp=1586397492894, value=ant
3 row(s) in 0.0030 seconds
# 查询指定行‘rowkey2’的baseinfo列族的‘name’列信息
hbase(main):037:0> get 'cjh:student','rowkey2','baseinfo:name'
COLUMN CELL
baseinfo:name timestamp=1586397492894, value=ant
1 row(s) in 0.0050 seconds
# 全表扫描
hbase(main):038:0> scan 'cjh:student'
# 全表扫描,baseinfo列族的信息
hbase(main):039:0> scan 'cjh:student', COLUMN=>'baseinfo
# 全表扫描,baseinfo列族的name列信息
hbase(main):040:0> scan 'cjh:student', COLUMN=>'baseinfo:name'
ROW COLUMN+CELL
rowkey column=baseinfo:name, timestamp=1586320401611, value=cjh
rowkey2 column=baseinfo:name, timestamp=1586397492894, value=ant
rowkey3 column=baseinfo:name, timestamp=1586397737697, value=xiu
3 row(s) in 0.0050 seconds
# 全表扫描,baseinfo列族的name列信息, 从rowkey2开始
hbase(main):041:0> scan 'cjh:student', COLUMN=>'baseinfo:name', STARTROW=> 'rowkey2'
ROW COLUMN+CELL
rowkey2 column=baseinfo:name, timestamp=1586397492894, value=ant
rowkey3 column=baseinfo:name, timestamp=1586397737697, value=xiu
2 row(s) in 0.0100 seconds
# 全表扫描,baseinfo列族的name列信息, 从rowkey开始,到rowkey3(不包含)结束
base(main):047:0> scan 'cjh:student', COLUMN=>'baseinfo:name', STARTROW=> 'rowkey',STOPROW=>'rowkey3'
# 全表扫描,baseinfo列族的name列信息, 从rowkey开始,到rowkey3(不包含)结束 LIMIT限制
hbase(main):049:0> scan 'cjh:student', COLUMN=>'baseinfo:name', STARTROW=> 'rowkey',STOPROW=>'rowkey3',LIMIT=>2
ROW COLUMN+CELL
rowkey column=baseinfo:name, timestamp=1586320401611, value=cjh
rowkey2 column=baseinfo:name, timestamp=1586397492894, value=ant
# 值等于8的
hbase(main):051:0> scan 'cjh:student', FILTER=>"ValueFilter(=,'binary:8')"
ROW COLUMN+CELL
rowkey2 column=baseinfo:age, timestamp=1586397540495, value=8
1 row(s) in 0.0210 seconds
# 值等于1986-11-25的
hbase(main):054:0> scan 'cjh:student', FILTER=>"ValueFilter(=,'binary:1986-11-25')"
ROW COLUMN+CELL
rowkey3 column=baseinfo:birthday, timestamp=1586397781809, value=1986-11-25
1 row(s) in 0.0050 seconds
# 值包含(模糊查询)
hbase(main):056:0> scan 'cjh:student', FILTER=>"ValueFilter(=,'substring:g')"
ROW COLUMN+CELL
rowkey column=baseinfo:name, timestamp=1586320401611, value=cjh
rowkey3 column=schoolinfo:location, timestamp=1586397832076, value=qingliangmendajie
2 row(s) in 0.0200 seconds
hbase(main):057:0> scan 'cjh:student', FILTER=>"ValueFilter(=,'substring:1986')"
ROW COLUMN+CELL
rowkey column=baseinfo:birthday, timestamp=1586320512382, value=1986-10-1
rowkey3 column=baseinfo:birthday, timestamp=1586397781809, value=1986-11-25
2 row(s) in 0.0100 seconds
# 列前缀匹配
hbase(main):058:0> scan 'cjh:student',FILTER=>"ColumnPrefixFilter('bir')"
hbase(main):059:0> scan 'cjh:student',FILTER=>"ColumnPrefixFilter('na')"
# 多过滤查询
hbase(main):061:0> scan 'cjh:student', FILTER=>"ColumnPrefixFilter('na') AND ValueFilter(=,'substring:e')"
hbase(main):063:0> scan 'cjh:student', FILTER=>"ColumnPrefixFilter('na') AND (ValueFilter(=,'substring:ee') OR ValueFilter(=,'substring:ue'))"
# 对rowkey的前置过滤
hbase(main):069:0> scan 'cjh:student', FILTER=>"PrefixFilter('row')"
# 查找row中 包含2的rowkey信息
hbase(main):071:0> scan 'cjh:student',FILTER=>"RowFilter(=,'substring:2')"
hbase(main):073:0> import org.apache.hadoop.hbase.filter.RowFilter
hbase(main):074:0> import org.apache.hadoop.hbase.filter.RegexStringComparator
hbase(main):080:0> import org.apache.hadoop.hbase.filter.CompareFilter
scan 'cjh:student',FILTER=>RowFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'), RegexStringComparator.new('^row[\w]*\d'))
# 从HDFS导入数据到HBase
[root@lijia1 ~]# hdfs dfs -put ./hbase_import_data.csv /user/
hbase(main):082:0> creat 'cjh:customer','order'
[root@lijia1 ~]# hbase org.apache.hadoop.hbase.mapreduce.ImportTsv \
> -Dimporttsv.separator=, \
> -Dimporttsv.columns="HBASE_ROW_KEY,order:numb,order:date" \
> cjh:customer /user/hbase_import_data.csv
ps:望多多支持,后续更新中。。。