Search in sources :

Example 26 with RegexStringComparator

use of org.apache.hadoop.hbase.filter.RegexStringComparator in project drill by apache.

the class MapRDBFilterBuilder method createHBaseScanSpec.

private HBaseScanSpec createHBaseScanSpec(FunctionCall call, MaprDBCompareFunctionsProcessor processor) {
    String functionName = processor.getFunctionName();
    SchemaPath field = processor.getPath();
    byte[] fieldValue = processor.getValue();
    boolean sortOrderAscending = processor.isSortOrderAscending();
    boolean isRowKey = field.getRootSegmentPath().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 FunctionNames.EQ:
            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 FunctionNames.NE:
            compareOp = CompareOp.NOT_EQUAL;
            break;
        case FunctionNames.GE:
            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 FunctionNames.GT:
            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 FunctionNames.LE:
            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 FunctionNames.LT:
            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 FunctionNames.IS_NULL:
        case "isNull":
        case "is null":
            if (isRowKey) {
                return null;
            }
            isNullTest = true;
            compareOp = CompareOp.EQUAL;
            comparator = new NullComparator();
            break;
        case FunctionNames.IS_NOT_NULL:
        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;
}
Also used : HBaseScanSpec(org.apache.drill.exec.store.hbase.HBaseScanSpec) SingleColumnValueFilter(org.apache.hadoop.hbase.filter.SingleColumnValueFilter) HBaseRegexParser(org.apache.drill.exec.store.hbase.HBaseRegexParser) NullComparator(org.apache.hadoop.hbase.filter.NullComparator) BinaryComparator(org.apache.hadoop.hbase.filter.BinaryComparator) RegexStringComparator(org.apache.hadoop.hbase.filter.RegexStringComparator) ByteArrayComparable(org.apache.hadoop.hbase.filter.ByteArrayComparable) RowFilter(org.apache.hadoop.hbase.filter.RowFilter) SchemaPath(org.apache.drill.common.expression.SchemaPath) RowFilter(org.apache.hadoop.hbase.filter.RowFilter) Filter(org.apache.hadoop.hbase.filter.Filter) SingleColumnValueFilter(org.apache.hadoop.hbase.filter.SingleColumnValueFilter) CompareOp(org.apache.hadoop.hbase.filter.CompareFilter.CompareOp)

Example 27 with RegexStringComparator

use of org.apache.hadoop.hbase.filter.RegexStringComparator in project hbase by apache.

the class QuotaTableUtil method makeFilter.

/**
 * converts quotafilter to serializeable filterlists.
 */
public static Filter makeFilter(final QuotaFilter filter) {
    FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
    if (StringUtils.isNotEmpty(filter.getUserFilter())) {
        FilterList userFilters = new FilterList(FilterList.Operator.MUST_PASS_ONE);
        boolean hasFilter = false;
        if (StringUtils.isNotEmpty(filter.getNamespaceFilter())) {
            FilterList nsFilters = new FilterList(FilterList.Operator.MUST_PASS_ALL);
            nsFilters.addFilter(new RowFilter(CompareOperator.EQUAL, new RegexStringComparator(getUserRowKeyRegex(filter.getUserFilter()), 0)));
            nsFilters.addFilter(new QualifierFilter(CompareOperator.EQUAL, new RegexStringComparator(getSettingsQualifierRegexForUserNamespace(filter.getNamespaceFilter()), 0)));
            userFilters.addFilter(nsFilters);
            hasFilter = true;
        }
        if (StringUtils.isNotEmpty(filter.getTableFilter())) {
            FilterList tableFilters = new FilterList(FilterList.Operator.MUST_PASS_ALL);
            tableFilters.addFilter(new RowFilter(CompareOperator.EQUAL, new RegexStringComparator(getUserRowKeyRegex(filter.getUserFilter()), 0)));
            tableFilters.addFilter(new QualifierFilter(CompareOperator.EQUAL, new RegexStringComparator(getSettingsQualifierRegexForUserTable(filter.getTableFilter()), 0)));
            userFilters.addFilter(tableFilters);
            hasFilter = true;
        }
        if (!hasFilter) {
            userFilters.addFilter(new RowFilter(CompareOperator.EQUAL, new RegexStringComparator(getUserRowKeyRegex(filter.getUserFilter()), 0)));
        }
        filterList.addFilter(userFilters);
    } else if (StringUtils.isNotEmpty(filter.getTableFilter())) {
        filterList.addFilter(new RowFilter(CompareOperator.EQUAL, new RegexStringComparator(getTableRowKeyRegex(filter.getTableFilter()), 0)));
    } else if (StringUtils.isNotEmpty(filter.getNamespaceFilter())) {
        filterList.addFilter(new RowFilter(CompareOperator.EQUAL, new RegexStringComparator(getNamespaceRowKeyRegex(filter.getNamespaceFilter()), 0)));
    } else if (StringUtils.isNotEmpty(filter.getRegionServerFilter())) {
        filterList.addFilter(new RowFilter(CompareOperator.EQUAL, new RegexStringComparator(getRegionServerRowKeyRegex(filter.getRegionServerFilter()), 0)));
    }
    return filterList;
}
Also used : RegexStringComparator(org.apache.hadoop.hbase.filter.RegexStringComparator) RowFilter(org.apache.hadoop.hbase.filter.RowFilter) FilterList(org.apache.hadoop.hbase.filter.FilterList) QualifierFilter(org.apache.hadoop.hbase.filter.QualifierFilter)

Example 28 with RegexStringComparator

use of org.apache.hadoop.hbase.filter.RegexStringComparator in project beam by apache.

the class HBaseIOTest method testReadingWithFilterSDF.

@Test
public void testReadingWithFilterSDF() throws Exception {
    final String table = tmpTable.getName();
    final int numRows = 1001;
    createAndWriteData(table, numRows);
    String regex = ".*17.*";
    Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator(regex));
    runReadTestLength(HBaseIO.read().withConfiguration(conf).withTableId(table).withFilter(filter), true, 20);
}
Also used : RegexStringComparator(org.apache.hadoop.hbase.filter.RegexStringComparator) RowFilter(org.apache.hadoop.hbase.filter.RowFilter) RowFilter(org.apache.hadoop.hbase.filter.RowFilter) Filter(org.apache.hadoop.hbase.filter.Filter) CompareFilter(org.apache.hadoop.hbase.filter.CompareFilter) Test(org.junit.Test)

Aggregations

RegexStringComparator (org.apache.hadoop.hbase.filter.RegexStringComparator)28 Filter (org.apache.hadoop.hbase.filter.Filter)24 RowFilter (org.apache.hadoop.hbase.filter.RowFilter)24 FirstKeyOnlyFilter (org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter)13 CompareFilter (org.apache.hadoop.hbase.filter.CompareFilter)10 QualifierFilter (org.apache.hadoop.hbase.filter.QualifierFilter)10 Result (org.apache.hadoop.hbase.client.Result)9 BinaryComparator (org.apache.hadoop.hbase.filter.BinaryComparator)9 SingleColumnValueFilter (org.apache.hadoop.hbase.filter.SingleColumnValueFilter)9 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)8 PrefixFilter (org.apache.hadoop.hbase.filter.PrefixFilter)8 Scan (org.apache.hadoop.hbase.client.Scan)7 BloomFilter (org.apache.hive.common.util.BloomFilter)7 ByteArrayComparable (org.apache.hadoop.hbase.filter.ByteArrayComparable)6 CompareOp (org.apache.hadoop.hbase.filter.CompareFilter.CompareOp)6 InclusiveStopFilter (org.apache.hadoop.hbase.filter.InclusiveStopFilter)6 SchemaPath (org.apache.drill.common.expression.SchemaPath)5 NullComparator (org.apache.hadoop.hbase.filter.NullComparator)5 ValueFilter (org.apache.hadoop.hbase.filter.ValueFilter)5