Hbase 查询命令 条件筛选

Hbase 查询命令 条件筛选

方便测试

建一下表

hbase(main):001:0> create 'student','c1'

不写namespace的话就是默认在default里

查询有哪些namespace

hbase(main):001:0> list_namespace

查看表的全量数据

hbase(main):002:0> scan 'default:student'

放入一些测试数据

put 'student','1001','c1:id','1001'
put 'student','1002','c1:id','1002'
put 'student','1003','c1:id','1003'
put 'student','1004','c1:id','1004'
put 'student','1005','c1:id','1005'

只查询一行

hbase(main):025:0> scan 'student',LIMIT=>1
ROW                        COLUMN+CELL
 1001                      column=c1:id, timestamp=1658911986336, value=1001

查询表的总记录数

count 'student'

按写入的时间戳查询数据

scan 'student', {COLUMN => 'c1', TIMERANGE => [1658827317000,1658913717000]}

查询值为1002的记录

hbase(main):004:0> scan 'student',FILTER=>"ValueFilter(=,'binary:1002')"
ROW                        COLUMN+CELL
 1002                      column=c1:id, timestamp=1658911989184, value=1002
1 row(s) in 0.1060 seconds

查询c1:id列的值为1002的

hbase(main):006:0> scan 'student',COLUMNS => 'c1:id',FILTER=>"ValueFilter(=,'binary:1002')"
ROW                        COLUMN+CELL
 1002                      column=c1:id, timestamp=1658911989184, value=1002
1 row(s) in 0.0340 seconds

查询值包含100的记录,就跟sql的模糊匹配一样

hbase(main):007:0> scan 'student',FILTER=>"ValueFilter(=,'substring:100')"
ROW                        COLUMN+CELL
 1001                      column=c1:id, timestamp=1658911986336, value=1001
 1002                      column=c1:id, timestamp=1658911989184, value=1002
 1003                      column=c1:id, timestamp=1658911989217, value=1003
 1004                      column=c1:id, timestamp=1658911989243, value=1004
 1005                      column=c1:id, timestamp=1658911989788, value=1005
5 row(s) in 0.0470 seconds

为了方便列的其他查询,多放入一个列

put 'student','1001','c1:sex','1'
put 'student','1002','c1:sex','2'
put 'student','1003','c1:sex','1'
put 'student','1004','c1:sex','2'
put 'student','1005','c1:sex','1'

hbase(main):015:0* scan 'student'
ROW                        COLUMN+CELL
 1001                      column=c1:id, timestamp=1658911986336, value=1001
 1001                      column=c1:sex, timestamp=1658914149713, value=1
 1002                      column=c1:id, timestamp=1658911989184, value=1002
 1002                      column=c1:sex, timestamp=1658914152500, value=2
 1003                      column=c1:id, timestamp=1658911989217, value=1003
 1003                      column=c1:sex, timestamp=1658914152535, value=1
 1004                      column=c1:id, timestamp=1658911989243, value=1004
 1004                      column=c1:sex, timestamp=1658914152563, value=2
 1005                      column=c1:id, timestamp=1658911989788, value=1005
 1005                      column=c1:sex, timestamp=1658914153242, value=1
5 row(s) in 0.0390 seconds

查询列为id打头的值

hbase(main):019:0> scan 'student',FILTER=>"ColumnPrefixFilter('id')"
ROW                        COLUMN+CELL
 1001                      column=c1:id, timestamp=1658911986336, value=1001
 1002                      column=c1:id, timestamp=1658911989184, value=1002
 1003                      column=c1:id, timestamp=1658911989217, value=1003
 1004                      column=c1:id, timestamp=1658911989243, value=1004
 1005                      column=c1:id, timestamp=1658911989788, value=1005
5 row(s) in 0.0270 seconds

各项查询的条件是可以叠加的,比如下面这个

查询列为id打头且值为1003的

hbase(main):020:0> scan 'student',FILTER=>"ColumnPrefixFilter('id') AND ValueFilter(=,'binary:1003')"
ROW                        COLUMN+CELL
 1003                      column=c1:id, timestamp=1658911989217, value=1003
1 row(s) in 0.0550 seconds

查询rowkey为100打头的

hbase(main):021:0> scan 'student',FILTER=>"PrefixFilter('100')"
ROW                        COLUMN+CELL
 1001                      column=c1:id, timestamp=1658911986336, value=1001
 1001                      column=c1:sex, timestamp=1658914149713, value=1
 1002                      column=c1:id, timestamp=1658911989184, value=1002
 1002                      column=c1:sex, timestamp=1658914152500, value=2
 1003                      column=c1:id, timestamp=1658911989217, value=1003
 1003                      column=c1:sex, timestamp=1658914152535, value=1
 1004                      column=c1:id, timestamp=1658911989243, value=1004
 1004                      column=c1:sex, timestamp=1658914152563, value=2
 1005                      column=c1:id, timestamp=1658911989788, value=1005
 1005                      column=c1:sex, timestamp=1658914153242, value=1

查询rowkey为100打头的且不同返回列信息

hbase(main):022:0> scan 'student',FILTER=>"PrefixFilter('100') AND KeyOnlyFilter()"
ROW                        COLUMN+CELL
 1001                      column=c1:id, timestamp=1658911986336, value=
 1001                      column=c1:sex, timestamp=1658914149713, value=
 1002                      column=c1:id, timestamp=1658911989184, value=
 1002                      column=c1:sex, timestamp=1658914152500, value=
 1003                      column=c1:id, timestamp=1658911989217, value=
 1003                      column=c1:sex, timestamp=1658914152535, value=
 1004                      column=c1:id, timestamp=1658911989243, value=
 1004                      column=c1:sex, timestamp=1658914152563, value=
 1005                      column=c1:id, timestamp=1658911989788, value=
 1005                      column=c1:sex, timestamp=1658914153242, value=
5 row(s) in 0.0670 seconds

从特定行开始查三行

hbase(main):006:0> scan 'student',{STARTROW=>'1002',LIMIT=>3}
ROW                        COLUMN+CELL
 1002                      column=c1:id, timestamp=1658911989184, value=1002
 1002                      column=c1:sex, timestamp=1658914152500, value=2
 1003                      column=c1:id, timestamp=1658911989217, value=1003
 1003                      column=c1:sex, timestamp=1658914152535, value=1
 1004                      column=c1:id, timestamp=1658911989243, value=1004
 1004                      column=c1:sex, timestamp=1658914152563, value=2
3 row(s) in 0.0300 seconds

获取特定的行

hbase(main):007:0> get 'student','1001'
COLUMN                     CELL
 c1:id                     timestamp=1658911986336, value=1001
 c1:sex                    timestamp=1658914149713, value=1
2 row(s) in 0.0170 seconds

默认的查询是正序,倒叙使用REVERSED => TRUE

scan 'student',{REVERSED => TRUE,LIMIT=>1}

以上这些命令基本满足大部分的查询需求了

  • 7
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MIDSUMMER_yy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值