标签:equal 特定前缀 nta apach column 判断 key class row
HBase为筛选数据提供了一组过滤器,通过这个过滤器可以在HBase中的数据的多个维度(行,列,数据版本)上进行对数据的筛选操作,也就是说过滤器最终能够筛选的数据能够细化到具体的一个存储单元格上(由行键,列明,时间戳定位)。通常来说,通过行键,值来筛选数据的应用场景较多。
1.创建测试表studnet1
Vi Student1.java
import java.io.IOException; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.client.Put; public class Student1{ public static void main(String[] args){ HBaseConfiguration config = new HBaseConfiguration(); config.set("hbase.zookeeper.quorum", "h201,h202,h203"); String tablename = new String("student1"); try{ HBaseAdmin admin = new HBaseAdmin(config); if(admin.tableExists(tablename)){ admin.disableTable(tablename); admin.deleteTable(tablename); } HTableDescriptor tableDesc = new HTableDescriptor(tablename); tableDesc.addFamily(new HColumnDescriptor("cf1")); admin.createTable(tableDesc); admin.close(); HTable table = new HTable(config, Bytes.toBytes("student1")); Put put1 = new Put(Bytes.toBytes("a101")); put1.add(Bytes.toBytes("cf1"),Bytes.toBytes("name"),Bytes.toBytes("zs1")); Put put2 = new Put(Bytes.toBytes("a102")); put2.add(Bytes.toBytes("cf1"),Bytes.toBytes("name"),Bytes.toBytes("ls1")); Put put3 = new Put(Bytes.toBytes("a103")); put3.add(Bytes.toBytes("cf1"),Bytes.toBytes("name"),Bytes.toBytes("ww1")); table.put(put1); table.put(put2); table.put(put3); table.close(); } catch(IOException e) { e.printStackTrace(); } } }
1.1
RowFilter:筛选出匹配的所有的行,对于这个过滤器的应用场景,是非常直观的:使用BinaryComparator可以筛选出具有某个行键的行,或者通过改变比较运算符(CompareFilter.CompareOp.EQUAL)来筛选出符合某一条件的多条数据
RowFilter用于过滤row key
Operator |
Description |
LESS |
小于 |
LESS_OR_EQUAL |
小于等于 |
[EQUAL |
等于 |
NOT_EQUAL |
不等于 |
GREATER_OR_EQUAL |
大于等于 |
GREATER |
大于 |
NO_OP |
排除所有 |
Comparator |
Description |
BinaryComparator |
使用Bytes.compareTo()比较 |
BinaryPrefixComparator |
和BinaryComparator差不多,从前面开始比较 |
RegexStringComparator |
正则表达式 |
SubstringComparator |
把数据当成字符串,用contains()来判断 |
import java.io.IOException; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.filter.RowFilter; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.filter.BinaryComparator; import org.apache.hadoop.hbase.filter.CompareFilter; public class hss1{ public static void main(String[] args){ HBaseConfiguration config = new HBaseConfiguration(); config.set("hbase.zookeeper.quorum", "h201,h202,h203"); try{ HTable table = new HTable(config, Bytes.toBytes("student1")); Scan scan = new Scan(); Filter filter1 = new RowFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator("a101".getBytes())); scan.setFilter(filter1); ResultScanner rst = table.getScanner(scan); for (Result r:rst){ for (KeyValue kv : r.raw()) { StringBuffer s1 = new StringBuffer() .append(Bytes.toString(kv.getRow())).append(":") .append(Bytes.toString(kv.getFamily())).append(",") .append(Bytes.toString(kv.getQualifier())).append(",") .append(Bytes.toString(kv.getValue())); System.out.println(s1.toString()); } } rst.close(); table.close(); } catch(IOException e) { e.printStackTrace(); } } }
1.2
PrefixFilter:筛选出具有特定前缀的行键的数据。这个过滤器所实现的功能其实也可以由RowFilter结合RegexStringComparator来实现,不过这里提供了一种简便的使用方法
import org.apache.hadoop.hbase.filter.PrefixFilter;
Filter filter2 = new PrefixFilter(Bytes.toBytes("a"));
scan.setFilter(filter2);
1.3
RegexComparator 正则过滤
import org.apache.hadoop.hbase.filter.RegexStringComparator;
Filter filter3 = new RowFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator("^a.*"));
scan.setFilter(filter3);
标签:equal 特定前缀 nta apach column 判断 key class row
原文地址:https://www.cnblogs.com/xiguage119/p/10605721.html