1、创建两张测试表
hbase(main):044:0> create 'rtusers','address','info';
put 'rtusers','05','address:city','guangzhou';
put 'rtusers','05','address:contry','china';
put 'rtusers','05','address:province','zhejiang';
put 'rtusers','05','info:age','31';
put 'rtusers','05','info:birthday','1987-06-17';
put 'rtusers','05','info:company','act';
hbase(main):044:0> create 'rtusers2','address','info';
hbase(main):045:0*
hbase(main):046:0* put 'rtusers2','aibu','address:city','zhuhai';
hbase(main):047:0* put 'rtusers2','aibu','address:contry','china';
hbase(main):048:0* put 'rtusers2','aibu','address:province','guangdong';
hbase(main):049:0* put 'rtusers2','aibu','info:age','21';
hbase(main):050:0* put 'rtusers2','aibu','info:birthday','1987-06-17';
hbase(main):051:0* put 'rtusers2','aibu','info:company','jinshan';
hbase(main):057:0>
hbase(main):058:0* put 'rtusers2','bili','address:city','lasa';
hbase(main):059:0* put 'rtusers2','bili','address:contry','china';
hbase(main):060:0* put 'rtusers2','bili','address:province','xizang';
hbase(main):061:0* put 'rtusers2','bili','info:age','22';
hbase(main):062:0* put 'rtusers2','bili','info:birthday','1987-06-17';
hbase(main):063:0* put 'rtusers2','bili','info:company','jinshan';
hbase(main):064:0*
hbase(main):065:0*
hbase(main):066:0*
hbase(main):067:0* put 'rtusers2','cily','address:city','sanya';
hbase(main):068:0* put 'rtusers2','cily','address:contry','china';
hbase(main):069:0* put 'rtusers2','cily','address:province','hainan';
hbase(main):070:0* put 'rtusers2','cily','info:age','36';
hbase(main):071:0* put 'rtusers2','cily','info:birthday','1987-06-17';
hbase(main):072:0* put 'rtusers2','cily','info:company','jinshan';
2、查看scan的用法
使用 help 'scan' 命令可以查看scan的语法以及用法,关于scan命令中filter的使用规则如下:
hbase(main):010:0> scan help
{}中的语法是ruby的map的语法,FILTER必须大写,filter的参数是根据构造方法来的,也就是相当于java中的new Filter('param1','param2')等,这里只是省略了new参数而已。
当然同样可以使用ruby中new对象的方式,只是那样就必须使用全限定名称。后面会举一个全限定名称的例子。
比较器主要有以下几种:
BinaryComparator
按字节索引顺序比较指定字节数组,采用Bytes.compareTo(byte[]),比如:binary:\x00\x00\x02
BinaryPrefixComparator
跟前面相同,只是比较左端的数据是否相同,比如:binaryprefix:\x00\x00
NullComparator
判断给定的是否为空,不常用
BitComparator
按位比较 a BitwiseOp class 做异或,与,并操作,不常用
RegexStringComparator
提供一个正则的比较器,仅支持 EQUAL 和非EQUAL,比如:regexstring:ab*add
SubstringComparator
判断提供的子串是否出现在table的value中。比如:substring:great
3、限制条件
#查询父列族为address的数据,和子列为city的数据
scan 'rtusers', {COLUMNS => 'address'}
scan 'rtusers', {COLUMNS => 'address:city'}
#查询列簇family为address和info的数据
scan 'rtusers', {COLUMNS => ['address', 'info']}
限制查找条数
#查全表前十行数据
scan 'rtusers',{LIMIT=>10}
限制时间范围
#1621331699014这条数据查不到,是[)的算法
scan 'rtusers',{COLUMNS=>['address'],TIMERANGE=>[1621330867949,1621331699014],LIMIT=>10}
4、filter过滤部分
#支持的filters
hbase(main):009:0> show_filters
#PrefixFilter:rowKey前缀过滤
scan 'rtusers',{LIMIT=>10,FILTER=>"PrefixFilter('04')"}
ROW COLUMN+CELL
04 column=address:city, timestamp=1621331698963, value=shanghai
04 column=address:contry, timestamp=1621331698982, value=china
04 column=address:province, timestamp=1621331698988, value=zhejiang
04 column=info:age, timestamp=1621331698992, value=27
04 column=info:birthday, timestamp=1621331698996, value=1987-06-17
04 column=info:company, timestamp=1621331699001, value=tiktop
#模糊查询,包含0字符的rowkey都会被查询到
scan 'rtusers',{LIMIT=>10,FILTER=>"PrefixFilter('0')"}
#查value包含北京的row
scan 'rtusers',{LIMIT=>10,FILTER=>"ValueFilter(=,'substring:beijing')"}
ROW COLUMN+CELL
01 column=address:city, timestamp=1619148401805, value=beijing
01 column=address:province, timestamp=1619148401822, value=beijing
#查value包含北京的row,限定column
scan 'rtusers',{LIMIT=>10,FILTER=>"ValueFilter(=,'substring:beijing')",COLUMNS=>['address:province']}
ROW COLUMN+CELL
01 column=address:province, timestamp=1619148401822, value=beijing
#时间过滤器,只取两条数据
scan 'rtusers', { FILTER => "TimestampsFilter ( 1621330867949, 1621331699014)"}
ROW COLUMN+CELL
03 column=address:city, timestamp=1621330867949, value=nanjing
05 column=address:province, timestamp=1621331699014, value=zhejiang
#通过startrow,stoprow来进行查询(这种也比较快,实际操作中如果不能通过rowkey).是[)的算法.也就是只能查到a-b的rowkey
scan 'rtusers2',{STARTROW => 'a',STOPROW =>'c',LIMIT=>10}
#子列为拉萨,value等于lasa的数据.这里binary中的数据一定要是二进制字符串而不是具体的值.但是可以看到用字符串也ok
scan 'rtusers2',{LIMIT=>10,FILTER=>"SingleColumnValueFilter('address','city',=,'binary:lasa')"}
ROW COLUMN+CELL
bili column=address:city, timestamp=1621407842236, value=lasa
bili column=address:contry, timestamp=1621407842240, value=china
bili column=address:province, timestamp=1621407842244, value=xizang
bili column=info:age, timestamp=1621407842246, value=22
bili column=info:birthday, timestamp=1621407842249, value=1987-06-17
bili column=info:company, timestamp=1621407842255, value=jinshan
#查城市是拉萨的所有列簇中的contry为china的列
scan 'rtusers2',{LIMIT=>3,FILTER=>"(SingleColumnValueFilter('address','city',=,'binary:lasa')) AND (ColumnPrefixFilter('contry') AND ValueFilter(=,'substring:china'))"}
hbase(main):097:0* scan 'rtusers2',{LIMIT=>3,FILTER=>"(SingleColumnValueFilter('address','city',=,'binary:lasa')) AND (ColumnPrefixFilter('contry') AND ValueFilter(=,'substring:china'))"}
ROW COLUMN+CELL
bili column=address:contry, timestamp=1621407842240, value=china
#和LIMIT有异曲同工之妙,查前两条数据
scan 'rtusers2',{FILTER=>"PageFilter(2)"}
#查询rowkey中包含特定前缀的数据。测试了后缀也行,中间字符串也行,也就是相当于模糊查询吧
scan 'rtusers2',{FILTER=>"RowFilter(=,'substring:bi')"}
scan 'rtusers2',{FILTER=>"RowFilter(=,'substring:ib')"}