前言:本文详细介绍了 HBase QualifierFilter 过滤器 Java&Shell API 的使用,并贴出了相关示例代码以供参考。QualifierFilter 基于列名进行过滤,在工作中涉及到需要通过HBase 列名进行数据过滤时可以考虑使用它。比较器细节及原理请参照之前的更文:
一。Java Api
头部代码
/**
* 用于列名(Qualifier)过滤。
*/
public class QualifierFilterDemo {
private static boolean isok = false;
private static String tableName = "test";
private static String[] cfs = new String[]{"f"};
private static String[] data = new String[]{
"row-1:f:name:Wang", "row-1:f:age:20",
"row-2:f:name:Zhou", "row-2:f:age:10",
"row-3:f:gender:男", "row-3:f:name:Li",
"row-4:f:namana:xyz", "row-4:f:age:Zhao"
};
public static void main(String[] args) throws IOException {
MyBase myBase = new MyBase();
Connection connection = myBase.createConnection();
if (isok) {
myBase.deleteTable(connection, tableName);
myBase.createTable(connection, tableName, cfs);
// 造数据
myBase.putRows(connection, tableName, data);
}
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
中部代码
向右滑动滚动条可查看输出结果。
1. BinaryComparator 构造过滤器
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("age"))); // [row-1:f:age, row-2:f:age, row-4:f:age]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes("name"))); // [row-1:f:age, row-2:f:age, row-3:f:gender, row-4:f:age, row-4:f:namana]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.GREATER, new BinaryComparator(Bytes.toBytes("gender"))); // [row-1:f:name, row-2:f:name, row-3:f:name, row-4:f:namana]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryComparator(Bytes.toBytes("name"))); // [row-1:f:name, row-2:f:name, row-3:f:name]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.LESS, new BinaryComparator(Bytes.toBytes("gender"))); // [row-1:f:age, row-2:f:age, row-4:f:age]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes("gender"))); // [row-1:f:age, row-2:f:age, row-3:f:gender, row-4:f:age]
2. BinaryPrefixComparator 构造过滤器
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new BinaryPrefixComparator(Bytes.toBytes("nam"))); // [row-1:f:name, row-2:f:name, row-3:f:name, row-4:f:namana]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.NOT_EQUAL, new BinaryPrefixComparator(Bytes.toBytes("nam"))); // [row-1:f:age, row-2:f:age, row-3:f:gender, row-4:f:age]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.GREATER, new BinaryPrefixComparator(Bytes.toBytes("g"))); // [row-1:f:name, row-2:f:name, row-3:f:name, row-4:f:namana]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryPrefixComparator(Bytes.toBytes("n"))); // [row-1:f:name, row-2:f:name, row-3:f:name, row-4:f:namana]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.LESS, new BinaryPrefixComparator(Bytes.toBytes("m"))); // [row-1:f:age, row-2:f:age, row-3:f:gender, row-4:f:age]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryPrefixComparator(Bytes.toBytes("a"))); // [row-1:f:age, row-2:f:age, row-4:f:age]
3. SubstringComparator 构造过滤器
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("g")); // [row-1:f:age, row-2:f:age, row-3:f:gender, row-4:f:age]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.NOT_EQUAL, new SubstringComparator("n")); // [row-1:f:age, row-2:f:age, row-4:f:age]
4. RegexStringComparator 构造过滤器
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.NOT_EQUAL, new RegexStringComparator("nam")); // [row-1:f:age, row-2:f:age, row-3:f:gender, row-4:f:age]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("nam")); // [row-1:f:name, row-2:f:name, row-3:f:name, row-4:f:namana]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("n[a-z]m")); // [row-1:f:name, row-2:f:name, row-3:f:name, row-4:f:namana]
尾部代码
scan.setFilter(qualifierFilter);
ResultScanner scanner = table.getScanner(scan);
Iterator<Result> iterator = scanner.iterator();
LinkedList<String> rowkeys = new LinkedList<>();
while (iterator.hasNext()) {
Result result = iterator.next();
String rowkey = Bytes.toString(result.getRow());
rowkeys.add(rowkey);
}
System.out.println(rowkeys);
scanner.close();
table.close();
connection.close();
}
}
二。Shell Api
1. BinaryComparator 构造过滤器