use of org.apache.hadoop.hbase.filter.SingleColumnValueFilter in project hbase by apache.
the class TestFromClientSide method testFilterWithLongCompartor.
@Test
public void testFilterWithLongCompartor() throws Exception {
final TableName tableName = TableName.valueOf(name.getMethodName());
Table ht = TEST_UTIL.createTable(tableName, FAMILY);
byte[][] ROWS = makeN(ROW, 10);
byte[][] values = new byte[10][];
for (int i = 0; i < 10; i++) {
values[i] = Bytes.toBytes(100L * i);
}
for (int i = 0; i < 10; i++) {
Put put = new Put(ROWS[i]);
put.setDurability(Durability.SKIP_WAL);
put.addColumn(FAMILY, QUALIFIER, values[i]);
ht.put(put);
}
Scan scan = new Scan();
scan.addFamily(FAMILY);
Filter filter = new SingleColumnValueFilter(FAMILY, QUALIFIER, CompareOp.GREATER, new LongComparator(500));
scan.setFilter(filter);
ResultScanner scanner = ht.getScanner(scan);
int expectedIndex = 0;
for (Result result : ht.getScanner(scan)) {
assertEquals(result.size(), 1);
assertTrue(Bytes.toLong(result.getValue(FAMILY, QUALIFIER)) > 500);
expectedIndex++;
}
assertEquals(expectedIndex, 4);
scanner.close();
}
use of org.apache.hadoop.hbase.filter.SingleColumnValueFilter in project hbase by apache.
the class TestServerSideScanMetricsFromClientSide method testRowsSeenMetric.
public void testRowsSeenMetric(Scan baseScan) throws Exception {
Scan scan;
scan = new Scan(baseScan);
testMetric(scan, ServerSideScanMetrics.COUNT_OF_ROWS_SCANNED_KEY_METRIC_NAME, NUM_ROWS);
for (int i = 0; i < ROWS.length - 1; i++) {
scan = new Scan(baseScan);
scan.withStartRow(ROWS[0]);
scan.withStopRow(ROWS[i + 1]);
testMetric(scan, ServerSideScanMetrics.COUNT_OF_ROWS_SCANNED_KEY_METRIC_NAME, i + 1);
}
for (int i = ROWS.length - 1; i > 0; i--) {
scan = new Scan(baseScan);
scan.withStartRow(ROWS[i - 1]);
scan.withStopRow(ROWS[ROWS.length - 1]);
testMetric(scan, ServerSideScanMetrics.COUNT_OF_ROWS_SCANNED_KEY_METRIC_NAME, ROWS.length - i);
}
// The filter should filter out all rows, but we still expect to see every row.
Filter filter = new RowFilter(CompareOp.EQUAL, new BinaryComparator("xyz".getBytes()));
scan = new Scan(baseScan);
scan.setFilter(filter);
testMetric(scan, ServerSideScanMetrics.COUNT_OF_ROWS_SCANNED_KEY_METRIC_NAME, ROWS.length);
// Filter should pass on all rows
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(FAMILIES[0], QUALIFIERS[0], CompareOp.EQUAL, VALUE);
scan = new Scan(baseScan);
scan.setFilter(singleColumnValueFilter);
testMetric(scan, ServerSideScanMetrics.COUNT_OF_ROWS_SCANNED_KEY_METRIC_NAME, ROWS.length);
// Filter should filter out all rows
singleColumnValueFilter = new SingleColumnValueFilter(FAMILIES[0], QUALIFIERS[0], CompareOp.NOT_EQUAL, VALUE);
scan = new Scan(baseScan);
scan.setFilter(singleColumnValueFilter);
testMetric(scan, ServerSideScanMetrics.COUNT_OF_ROWS_SCANNED_KEY_METRIC_NAME, ROWS.length);
}
use of org.apache.hadoop.hbase.filter.SingleColumnValueFilter in project hbase by apache.
the class TestReversibleScanners method testReversibleRegionScanner.
@Test
public void testReversibleRegionScanner() throws IOException {
byte[] FAMILYNAME2 = Bytes.toBytes("testCf2");
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(name.getMethodName())).addFamily(new HColumnDescriptor(FAMILYNAME)).addFamily(new HColumnDescriptor(FAMILYNAME2));
Region region = TEST_UTIL.createLocalHRegion(htd, null, null);
loadDataToRegion(region, FAMILYNAME2);
// verify row count with forward scan
Scan scan = new Scan();
InternalScanner scanner = region.getScanner(scan);
verifyCountAndOrder(scanner, ROWSIZE * QUALSIZE * 2, ROWSIZE, true);
// Case1:Full reversed scan
scan.setReversed(true);
scanner = region.getScanner(scan);
verifyCountAndOrder(scanner, ROWSIZE * QUALSIZE * 2, ROWSIZE, false);
// Case2:Full reversed scan with one family
scan = new Scan();
scan.setReversed(true);
scan.addFamily(FAMILYNAME);
scanner = region.getScanner(scan);
verifyCountAndOrder(scanner, ROWSIZE * QUALSIZE, ROWSIZE, false);
// Case3:Specify qualifiers + One family
byte[][] specifiedQualifiers = { QUALS[1], QUALS[2] };
for (byte[] specifiedQualifier : specifiedQualifiers) scan.addColumn(FAMILYNAME, specifiedQualifier);
scanner = region.getScanner(scan);
verifyCountAndOrder(scanner, ROWSIZE * 2, ROWSIZE, false);
// Case4:Specify qualifiers + Two families
for (byte[] specifiedQualifier : specifiedQualifiers) scan.addColumn(FAMILYNAME2, specifiedQualifier);
scanner = region.getScanner(scan);
verifyCountAndOrder(scanner, ROWSIZE * 2 * 2, ROWSIZE, false);
// Case5: Case4 + specify start row
int startRowNum = ROWSIZE * 3 / 4;
scan.setStartRow(ROWS[startRowNum]);
scanner = region.getScanner(scan);
verifyCountAndOrder(scanner, (startRowNum + 1) * 2 * 2, (startRowNum + 1), false);
// Case6: Case4 + specify stop row
int stopRowNum = ROWSIZE / 4;
scan.setStartRow(HConstants.EMPTY_BYTE_ARRAY);
scan.setStopRow(ROWS[stopRowNum]);
scanner = region.getScanner(scan);
verifyCountAndOrder(scanner, (ROWSIZE - stopRowNum - 1) * 2 * 2, (ROWSIZE - stopRowNum - 1), false);
// Case7: Case4 + specify start row + specify stop row
scan.setStartRow(ROWS[startRowNum]);
scanner = region.getScanner(scan);
verifyCountAndOrder(scanner, (startRowNum - stopRowNum) * 2 * 2, (startRowNum - stopRowNum), false);
// Case8: Case7 + SingleColumnValueFilter
int valueNum = startRowNum % VALUESIZE;
Filter filter = new SingleColumnValueFilter(FAMILYNAME, specifiedQualifiers[0], CompareOp.EQUAL, VALUES[valueNum]);
scan.setFilter(filter);
scanner = region.getScanner(scan);
int unfilteredRowNum = (startRowNum - stopRowNum) / VALUESIZE + (stopRowNum / VALUESIZE == valueNum ? 0 : 1);
verifyCountAndOrder(scanner, unfilteredRowNum * 2 * 2, unfilteredRowNum, false);
// Case9: Case7 + PageFilter
int pageSize = 10;
filter = new PageFilter(pageSize);
scan.setFilter(filter);
scanner = region.getScanner(scan);
int expectedRowNum = pageSize;
verifyCountAndOrder(scanner, expectedRowNum * 2 * 2, expectedRowNum, false);
// Case10: Case7 + FilterList+MUST_PASS_ONE
SingleColumnValueFilter scvFilter1 = new SingleColumnValueFilter(FAMILYNAME, specifiedQualifiers[0], CompareOp.EQUAL, VALUES[0]);
SingleColumnValueFilter scvFilter2 = new SingleColumnValueFilter(FAMILYNAME, specifiedQualifiers[0], CompareOp.EQUAL, VALUES[1]);
expectedRowNum = 0;
for (int i = startRowNum; i > stopRowNum; i--) {
if (i % VALUESIZE == 0 || i % VALUESIZE == 1) {
expectedRowNum++;
}
}
filter = new FilterList(Operator.MUST_PASS_ONE, scvFilter1, scvFilter2);
scan.setFilter(filter);
scanner = region.getScanner(scan);
verifyCountAndOrder(scanner, expectedRowNum * 2 * 2, expectedRowNum, false);
// Case10: Case7 + FilterList+MUST_PASS_ALL
filter = new FilterList(Operator.MUST_PASS_ALL, scvFilter1, scvFilter2);
expectedRowNum = 0;
scan.setFilter(filter);
scanner = region.getScanner(scan);
verifyCountAndOrder(scanner, expectedRowNum * 2 * 2, expectedRowNum, false);
}
use of org.apache.hadoop.hbase.filter.SingleColumnValueFilter in project camel by apache.
the class ModelAwareColumnMatchingFilter method apply.
/**
* Applies the message to {@link org.apache.hadoop.hbase.filter.Filter} to context.
*/
@Override
public void apply(CamelContext context, HBaseRow rowModel) {
fl.getFilters().clear();
if (rowModel != null) {
for (HBaseCell cell : rowModel.getCells()) {
if (cell.getValue() != null) {
byte[] family = HBaseHelper.getHBaseFieldAsBytes(cell.getFamily());
byte[] qualifier = HBaseHelper.getHBaseFieldAsBytes(cell.getQualifier());
byte[] value = context.getTypeConverter().convertTo(byte[].class, cell.getValue());
SingleColumnValueFilter columnValueFilter = new SingleColumnValueFilter(family, qualifier, CompareFilter.CompareOp.EQUAL, value);
fl.addFilter(columnValueFilter);
}
}
}
}
use of org.apache.hadoop.hbase.filter.SingleColumnValueFilter in project drill by apache.
the class MapRDBFilterBuilder method createHBaseScanSpec.
private HBaseScanSpec createHBaseScanSpec(FunctionCall call, CompareFunctionsProcessor processor) {
String functionName = processor.getFunctionName();
SchemaPath field = processor.getPath();
byte[] fieldValue = processor.getValue();
boolean sortOrderAscending = processor.isSortOrderAscending();
boolean isRowKey = field.getAsUnescapedPath().equals(ROW_KEY);
if (!(isRowKey || (!field.getRootSegment().isLastPath() && field.getRootSegment().getChild().isLastPath() && field.getRootSegment().getChild().isNamed()))) {
/*
* if the field in this function is neither the row_key nor a qualified HBase column, return.
*/
return null;
}
if (processor.isRowKeyPrefixComparison()) {
return createRowKeyPrefixScanSpec(call, processor);
}
CompareOp compareOp = null;
boolean isNullTest = false;
ByteArrayComparable comparator = new BinaryComparator(fieldValue);
byte[] startRow = HConstants.EMPTY_START_ROW;
byte[] stopRow = HConstants.EMPTY_END_ROW;
switch(functionName) {
case "equal":
compareOp = CompareOp.EQUAL;
if (isRowKey) {
startRow = fieldValue;
/* stopRow should be just greater than 'value'*/
stopRow = Arrays.copyOf(fieldValue, fieldValue.length + 1);
compareOp = CompareOp.EQUAL;
}
break;
case "not_equal":
compareOp = CompareOp.NOT_EQUAL;
break;
case "greater_than_or_equal_to":
if (sortOrderAscending) {
compareOp = CompareOp.GREATER_OR_EQUAL;
if (isRowKey) {
startRow = fieldValue;
}
} else {
compareOp = CompareOp.LESS_OR_EQUAL;
if (isRowKey) {
// stopRow should be just greater than 'value'
stopRow = Arrays.copyOf(fieldValue, fieldValue.length + 1);
}
}
break;
case "greater_than":
if (sortOrderAscending) {
compareOp = CompareOp.GREATER;
if (isRowKey) {
// startRow should be just greater than 'value'
startRow = Arrays.copyOf(fieldValue, fieldValue.length + 1);
}
} else {
compareOp = CompareOp.LESS;
if (isRowKey) {
stopRow = fieldValue;
}
}
break;
case "less_than_or_equal_to":
if (sortOrderAscending) {
compareOp = CompareOp.LESS_OR_EQUAL;
if (isRowKey) {
// stopRow should be just greater than 'value'
stopRow = Arrays.copyOf(fieldValue, fieldValue.length + 1);
}
} else {
compareOp = CompareOp.GREATER_OR_EQUAL;
if (isRowKey) {
startRow = fieldValue;
}
}
break;
case "less_than":
if (sortOrderAscending) {
compareOp = CompareOp.LESS;
if (isRowKey) {
stopRow = fieldValue;
}
} else {
compareOp = CompareOp.GREATER;
if (isRowKey) {
// startRow should be just greater than 'value'
startRow = Arrays.copyOf(fieldValue, fieldValue.length + 1);
}
}
break;
case "isnull":
case "isNull":
case "is null":
if (isRowKey) {
return null;
}
isNullTest = true;
compareOp = CompareOp.EQUAL;
comparator = new NullComparator();
break;
case "isnotnull":
case "isNotNull":
case "is not null":
if (isRowKey) {
return null;
}
compareOp = CompareOp.NOT_EQUAL;
comparator = new NullComparator();
break;
case "like":
/*
* Convert the LIKE operand to Regular Expression pattern so that we can
* apply RegexStringComparator()
*/
HBaseRegexParser parser = new HBaseRegexParser(call).parse();
compareOp = CompareOp.EQUAL;
comparator = new RegexStringComparator(parser.getRegexString());
/*
* We can possibly do better if the LIKE operator is on the row_key
*/
if (isRowKey) {
String prefix = parser.getPrefixString();
if (prefix != null) {
/*
* If there is a literal prefix, it can help us prune the scan to a sub range
*/
if (prefix.equals(parser.getLikeString())) {
/* The operand value is literal. This turns the LIKE operator to EQUAL operator */
startRow = stopRow = fieldValue;
compareOp = null;
} else {
startRow = prefix.getBytes(Charsets.UTF_8);
stopRow = startRow.clone();
boolean isMaxVal = true;
for (int i = stopRow.length - 1; i >= 0; --i) {
int nextByteValue = (0xff & stopRow[i]) + 1;
if (nextByteValue < 0xff) {
stopRow[i] = (byte) nextByteValue;
isMaxVal = false;
break;
} else {
stopRow[i] = 0;
}
}
if (isMaxVal) {
stopRow = HConstants.EMPTY_END_ROW;
}
}
}
}
break;
}
if (compareOp != null || startRow != HConstants.EMPTY_START_ROW || stopRow != HConstants.EMPTY_END_ROW) {
Filter filter = null;
if (isRowKey) {
if (compareOp != null) {
filter = new RowFilter(compareOp, comparator);
}
} else {
byte[] family = HBaseUtils.getBytes(field.getRootSegment().getPath());
byte[] qualifier = HBaseUtils.getBytes(field.getRootSegment().getChild().getNameSegment().getPath());
filter = new SingleColumnValueFilter(family, qualifier, compareOp, comparator);
((SingleColumnValueFilter) filter).setLatestVersionOnly(true);
if (!isNullTest) {
((SingleColumnValueFilter) filter).setFilterIfMissing(true);
}
}
return new HBaseScanSpec(groupScan.getTableName(), startRow, stopRow, filter);
}
// else
return null;
}
Aggregations