下面列出了怎么用org.apache.hadoop.hbase.filter.ValueFilter的API类实例代码及写法,或者点击链接到github查看源代码。
public static void filterLimitValueRange(String projectId, String instanceId, String tableId) {
// A filter that matches cells whose values are between the given values
ValueFilter valueGreaterFilter =
new ValueFilter(
CompareFilter.CompareOp.GREATER_OR_EQUAL,
new BinaryComparator(Bytes.toBytes("PQ2A.190405")));
ValueFilter valueLesserFilter =
new ValueFilter(
CompareFilter.CompareOp.LESS_OR_EQUAL,
new BinaryComparator(Bytes.toBytes("PQ2A.190406")));
FilterList filter = new FilterList(FilterList.Operator.MUST_PASS_ALL);
filter.addFilter(valueGreaterFilter);
filter.addFilter(valueLesserFilter);
Scan scan = new Scan().setFilter(filter);
readWithFilter(projectId, instanceId, tableId, scan);
}
public static void readFilter(String projectId, String instanceId, String tableId) {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
Table table = connection.getTable(TableName.valueOf(tableId));
ValueFilter valueFilter =
new ValueFilter(CompareOp.EQUAL, new RegexStringComparator("PQ2A.*"));
Scan scan = new Scan().setFilter(valueFilter);
ResultScanner rows = table.getScanner(scan);
for (Result row : rows) {
printRow(row);
}
} catch (IOException e) {
System.out.println(
"Unable to initialize service client, as a network error occurred: \n" + e.toString());
}
}
public static void filterLimitValueRegex(String projectId, String instanceId, String tableId) {
// A filter that matches cells whose value satisfies the given regex
Filter filter = new ValueFilter(CompareOp.EQUAL, new RegexStringComparator("PQ2A.*$"));
Scan scan = new Scan().setFilter(filter);
readWithFilter(projectId, instanceId, tableId, scan);
}
public static void filterComposingInterleave(
String projectId, String instanceId, String tableId) {
// A filter that matches cells with the value true OR with the column qualifier os_build
Filter qualifierFilter =
new QualifierFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("os_build")));
Filter valueFilter =
new ValueFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("true")));
FilterList filter = new FilterList(Operator.MUST_PASS_ONE);
filter.addFilter(qualifierFilter);
filter.addFilter(valueFilter);
Scan scan = new Scan().setFilter(filter).setMaxVersions();
readWithFilter(projectId, instanceId, tableId, scan);
}
void addFilterByMapping( FilterList fl, CompareFilter.CompareOp comp, Class<?> comparatorClass, Object comparator,
Mapping.TupleMapping tupleMapping )
throws NoSuchMethodException, InstantiationException, IllegalAccessException,
java.lang.reflect.InvocationTargetException {
switch ( tupleMapping ) {
case KEY: {
addFilter( RowFilter.class, fl, comp, comparatorClass, comparator );
return;
}
case FAMILY: {
addFilter( FamilyFilter.class, fl, comp, comparatorClass, comparator );
return;
}
case COLUMN: {
//TODO Check if ColumnPrefixFilter works faster and suit more
addFilter( QualifierFilter.class, fl, comp, comparatorClass, comparator );
return;
}
case VALUE: {
addFilter( ValueFilter.class, fl, comp, comparatorClass, comparator );
return;
}
case TIMESTAMP: {
addFilter( TimestampsFilter.class, fl, comp, comparatorClass, comparator );
// Constructor<TimestampsFilter> columnFilterConstructor =
// TimestampsFilter.class.getConstructor( CompareFilter.CompareOp.class, comparatorClass );
// TimestampsFilter scf = columnFilterConstructor.newInstance( comp, comparator );
// fl.addFilter( scf );
return;
}
}
}
@Test(timeOut = 60_000)
public void testGetWithValueFilter(ITestContext context) throws Exception {
testGet(context, new ValueFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(col1)));
}
@Test(timeOut = 60_000)
public void testScanWithValueFilter(ITestContext context) throws Exception {
testScan(context, new ValueFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(col1)));
}
/**
* Test for HBASE-17125
*/
@Test
public void testReadWithFilter() throws Exception {
final TableName tableName = name.getTableName();
try (Table table = TEST_UTIL.createTable(tableName, FAMILY, 3)) {
byte[] VALUEA = Bytes.toBytes("value-a");
byte[] VALUEB = Bytes.toBytes("value-b");
long[] ts = {1000, 2000, 3000, 4000};
Put put = new Put(ROW);
// Put version 1000,2000,3000,4000 of column FAMILY:QUALIFIER
for (int t = 0; t <= 3; t++) {
if (t <= 1) {
put.addColumn(FAMILY, QUALIFIER, ts[t], VALUEA);
} else {
put.addColumn(FAMILY, QUALIFIER, ts[t], VALUEB);
}
}
table.put(put);
Scan scan =
new Scan().setFilter(new ValueFilter(CompareOperator.EQUAL,
new SubstringComparator("value-a")))
.readVersions(3);
ResultScanner scanner = table.getScanner(scan);
Result result = scanner.next();
// ts[0] has gone from user view. Only read ts[2] which value is less or equal to 3
assertNResult(result, ROW, FAMILY, QUALIFIER, new long[]{ts[1]}, new byte[][]{VALUEA}, 0,
0);
Get get =
new Get(ROW)
.setFilter(new ValueFilter(CompareOperator.EQUAL,
new SubstringComparator("value-a")))
.readVersions(3);
result = table.get(get);
// ts[0] has gone from user view. Only read ts[2] which value is less or equal to 3
assertNResult(result, ROW, FAMILY, QUALIFIER, new long[]{ts[1]}, new byte[][]{VALUEA}, 0,
0);
// Test with max versions 1, it should still read ts[1]
scan =
new Scan().setFilter(new ValueFilter(CompareOperator.EQUAL,
new SubstringComparator("value-a")))
.readVersions(1);
scanner = table.getScanner(scan);
result = scanner.next();
// ts[0] has gone from user view. Only read ts[2] which value is less or equal to 3
assertNResult(result, ROW, FAMILY, QUALIFIER, new long[]{ts[1]}, new byte[][]{VALUEA}, 0,
0);
// Test with max versions 1, it should still read ts[1]
get =
new Get(ROW)
.setFilter(new ValueFilter(CompareOperator.EQUAL,
new SubstringComparator("value-a")))
.readVersions(1);
result = table.get(get);
// ts[0] has gone from user view. Only read ts[2] which value is less or equal to 3
assertNResult(result, ROW, FAMILY, QUALIFIER, new long[]{ts[1]}, new byte[][]{VALUEA}, 0,
0);
// Test with max versions 5, it should still read ts[1]
scan =
new Scan().setFilter(new ValueFilter(CompareOperator.EQUAL,
new SubstringComparator("value-a")))
.readVersions(5);
scanner = table.getScanner(scan);
result = scanner.next();
// ts[0] has gone from user view. Only read ts[2] which value is less or equal to 3
assertNResult(result, ROW, FAMILY, QUALIFIER, new long[]{ts[1]}, new byte[][]{VALUEA}, 0,
0);
// Test with max versions 5, it should still read ts[1]
get =
new Get(ROW)
.setFilter(new ValueFilter(CompareOperator.EQUAL,
new SubstringComparator("value-a")))
.readVersions(5);
result = table.get(get);
// ts[0] has gone from user view. Only read ts[2] which value is less or equal to 3
assertNResult(result, ROW, FAMILY, QUALIFIER, new long[]{ts[1]}, new byte[][]{VALUEA}, 0,
0);
}
}