hbase的过滤器查询-创新互联
hbase的过滤器有很多:大致分为两大类:比较过滤器和专用过滤器,过滤器的作用是在服务端判断数据是否满足条件,然后只将满足条件的数据返回给客户端;
hbase 过滤器的比较运算符:
LESS ----- <
LESS_OR_EQUAL ----- <=
EQUAL ----- =
NOT_EQUAL ----- <>
GREATER_OR_EQUAL ----- >=
GREATER ----- >
NO_OP #排除所有
HBase 过滤器的比较器(指定比较机制):
BinaryComparator 按字节索引顺序比较指定字节数组,采用 Bytes.compareTo(byte[])
BinaryPrefixComparator 跟前面相同,只是比较左端的数据是否相同
NullComparator 判断给定的是否为空
BitComparator 按位比较
RegexStringComparator 提供一个正则的比较器,仅支持 EQUAL 和非 EQUAL
SubstringComparator 判断提供的子串是否出现在 value 中。
1. 比较过滤器
//行键过滤器 RowFilter
Filter filter1 = new RowFilter(CompareOp.LESS_OR_EQUAL, new
BinaryComparator(Bytes.toBytes("user0000")));
scan.setFilter(filter1);
//列簇过滤器 FamilyFilter
Filter filter1 = new FamilyFilter(CompareOp.LESS, new
BinaryComparator(Bytes.toBytes("base_info")));
scan.setFilter(filter1);
//列过滤器 QualifierFilter
Filter filter = new QualifierFilter(CompareOp.LESS_OR_EQUAL, new
BinaryComparator(Bytes.toBytes("name")));
scan.setFilter(filter1);
//值过滤器 ValueFilter
Filter filter = new ValueFilter(CompareOp.EQUAL, new SubstringComparator("zhangsan") );
scan.setFilter(filter1);
//时间戳过滤器 TimestampsFilter
List tss = new ArrayList();
tss.add(1495398833002l);
Filter filter1 = new TimestampsFilter(tss);
scan.setFilter(filter1);
2.专用过滤器
//单列值过滤器 SingleColumnValueFilter ----会返回满足条件的整行
SingleColumnValueFilter filter = new SingleColumnValueFilter(
Bytes.toBytes("colfam1"),
Bytes.toBytes("col-5"),
CompareFilter.CompareOp.NOT_EQUAL,
new SubstringComparator("val-5"));
filter.setFilterIfMissing(true); //如果不设置为 true,则那些不包含指定 column 的行也会返回
scan.setFilter(filter1);
//单列值排除器 SingleColumnValueExcludeFilter -----返回排除了该列的结果
SingleColumnValueExcludeFilter filter = new SingleColumnValueExcludeFilter(
Bytes.toBytes("colfam1"),
Bytes.toBytes("col-5"),
CompareFilter.CompareOp.NOT_EQUAL,
new SubstringComparator("val-5"));
filter.setFilterIfMissing(true); //如果不设置为 true,则那些不包含指定 column 的行也会返回
scan.setFilter(filter1);
//前缀过滤器 PrefixFilter----针对行键
Filter filter = new PrefixFilter(Bytes.toBytes("row1"));
scan.setFilter(filter1);
//列前缀过滤器 ColumnPrefixFilter
Filter filter = new ColumnPrefixFilter(Bytes.toBytes("qual2"));
scan.setFilter(filter1);
实战案例:
public class HBase_Filter01 {
private static String ZK_KEY = "hbase.zookeeper.quorum";
private static String ZK_VALUE = "hadoop01:2181,hadoop02:2181,hadoop03:2181";
private static Configuration conf;
private static Connection connection;
private static Admin admin;
static {
conf = HBaseConfiguration.create();
conf.set(ZK_KEY,ZK_VALUE);
try {
connection= ConnectionFactory.createConnection(conf);
admin=connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Scan scan=new Scan();
ValueFilter filter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("a"));
scan.setFilter(filter);
TableName tableName =TableName.valueOf("user_info");
try {
Table table = connection.getTable(tableName);
ResultScanner scanner = table.getScanner(scan);
Iterator iterator=scanner.iterator();
while(iterator.hasNext()){
Result result = iterator.next();
System.out.println(result.list());
byte[] row = result.getRow();
System.out.println(new String(row));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
本文题目:hbase的过滤器查询-创新互联
文章路径:http://myzitong.com/article/docipd.html