org.apache.hadoop.hbase.filter.CompareFilter Java Examples

The following examples show how to use org.apache.hadoop.hbase.filter.CompareFilter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Filters.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #2
Source File: DefaultHBaseSerde.java    From envelope with Apache License 2.0 6 votes vote down vote up
private FilterList getColumnValueFilters(Row row) {
  FilterList filterList = new FilterList(Operator.MUST_PASS_ALL);
  Set<String> filterColumnNames = Sets.newHashSet(row.schema().fieldNames());
  
  for (Map.Entry<String, ColumnDef> column : columns.entrySet()) {
    if (!column.getValue().cf.equals("rowkey")) {
      if (filterColumnNames.contains(column.getKey())) {
        byte[] value = getColumnValueAsBytes(column.getValue().name, column.getValue().type, row);
        if (value != null) {
          SingleColumnValueFilter columnValueFilter = new SingleColumnValueFilter(
              Bytes.toBytes(column.getValue().cf),
              Bytes.toBytes(column.getValue().name),
              CompareFilter.CompareOp.EQUAL,
              value
          );
          filterList.addFilter(columnValueFilter);
        }
      }
    }
  }
  
  return filterList;
}
 
Example #3
Source File: MockHTable.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean checkAndDelete(byte[] row, byte[] family, byte[] qualifier, CompareFilter.CompareOp compareOp, byte[] value, Delete delete) throws IOException {
    if (check(row, family, qualifier, compareOp, value)) {
        delete(delete);
        return true;
    }
    return false;
}
 
Example #4
Source File: TestBaillisAnomaliesWithTXs.java    From phoenix-omid with Apache License 2.0 5 votes vote down vote up
@Test(timeOut = 10_000)
public void testSIPreventsPredicateManyPrecedersForReadPredicates(ITestContext context) throws Exception {
    // TX History for PMP for Read Predicate:
    // begin; set transaction isolation level repeatable read; -- T1
    // begin; set transaction isolation level repeatable read; -- T2
    // select * from test where value = 30; -- T1. Returns nothing
    // insert into test (id, value) values(3, 30); -- T2
    // commit; -- T2
    // select * from test where value % 3 = 0; -- T1. Still returns nothing
    // commit; -- T1

    // 0) Start transactions
    TransactionManager tm = newTransactionManager(context);
    TTable txTable = new TTable(connection, TEST_TABLE);

    Transaction tx1 = tm.begin();
    Transaction tx2 = tm.begin();

    // 1) select * from test where value = 30; -- T1. Returns nothing
    Scan scan = new Scan();
    Filter f = new SingleColumnValueFilter(famName, colName, CompareFilter.CompareOp.EQUAL, Bytes.toBytes(30));
    scan.setFilter(f);
    ResultScanner tx1Scanner = txTable.getScanner(tx1, scan);
    assertNull(tx1Scanner.next());

    // 2) insert into test (id, value) values(3, 30); -- T2
    Put newRow = new Put(rowId3);
    newRow.addColumn(famName, colName, dataValue3);
    txTable.put(tx2, newRow);

    // 3) Commit TX 2
    tm.commit(tx2);

    // 4) select * from test where value % 3 = 0; -- T1. Still returns nothing
    tx1Scanner = txTable.getScanner(tx1, scan);
    assertNull(tx1Scanner.next());

    // 5) Commit TX 1
    tm.commit(tx1);
}
 
Example #5
Source File: TransactionAwareHTable.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkAndMutate(byte[] row, byte[] family, byte[] qualifier,
    CompareFilter.CompareOp compareOp, byte[] value, RowMutations rowMutations)
    throws IOException {
  if (allowNonTransactional) {
    return hTable.checkAndMutate(row, family, qualifier, compareOp, value, rowMutations);
  }

  throw new UnsupportedOperationException(
      "checkAndMutate operation is not supported transactionally");
}
 
Example #6
Source File: TransactionAwareHTable.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkAndDelete(byte[] bytes, byte[] bytes1, byte[] bytes2, CompareFilter.CompareOp compareOp,
                              byte[] bytes3, Delete delete) throws IOException {
  if (allowNonTransactional) {
    return hTable.checkAndDelete(bytes, bytes1, bytes2, compareOp, bytes3, delete);
  } else {
    throw new UnsupportedOperationException("Operation is not supported transactionally");
  }
}
 
Example #7
Source File: TransactionAwareHTable.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkAndPut(byte[] bytes, byte[] bytes1, byte[] bytes2, CompareFilter.CompareOp compareOp,
                           byte[] bytes3, Put put) throws IOException {
  if (allowNonTransactional) {
    return hTable.checkAndPut(bytes, bytes1, bytes2, compareOp, bytes3, put);
  } else {
    throw new UnsupportedOperationException("Operation is not supported transactionally");
  }
}
 
Example #8
Source File: TransactionAwareHTable.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkAndMutate(byte[] row, byte[] family, byte[] qualifier,
                              CompareFilter.CompareOp compareOp, byte[] value, RowMutations rowMutations)
    throws IOException {
  if (allowNonTransactional) {
    return hTable.checkAndMutate(row, family, qualifier, compareOp, value, rowMutations);
  }

  throw new UnsupportedOperationException("checkAndMutate operation is not supported transactionally");
}
 
Example #9
Source File: TransactionAwareHTable.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkAndDelete(byte[] bytes, byte[] bytes1, byte[] bytes2, CompareFilter.CompareOp compareOp,
                              byte[] bytes3, Delete delete) throws IOException {
  if (allowNonTransactional) {
    return hTable.checkAndDelete(bytes, bytes1, bytes2, compareOp, bytes3, delete);
  } else {
    throw new UnsupportedOperationException("Operation is not supported transactionally");
  }
}
 
Example #10
Source File: TransactionAwareHTable.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkAndPut(byte[] bytes, byte[] bytes1, byte[] bytes2, CompareFilter.CompareOp compareOp,
                           byte[] bytes3, Put put) throws IOException {
  if (allowNonTransactional) {
    return hTable.checkAndPut(bytes, bytes1, bytes2, compareOp, bytes3, put);
  } else {
    throw new UnsupportedOperationException("Operation is not supported transactionally");
  }
}
 
Example #11
Source File: TransactionAwareHTable.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkAndMutate(byte[] row, byte[] family, byte[] qualifier,
                              CompareFilter.CompareOp compareOp, byte[] value, RowMutations rowMutations)
    throws IOException {
  if (allowNonTransactional) {
    return hTable.checkAndMutate(row, family, qualifier, compareOp, value, rowMutations);
  }

  throw new UnsupportedOperationException("checkAndMutate operation is not supported transactionally");
}
 
Example #12
Source File: TransactionAwareHTable.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkAndDelete(byte[] bytes, byte[] bytes1, byte[] bytes2, CompareFilter.CompareOp compareOp,
                              byte[] bytes3, Delete delete) throws IOException {
  if (allowNonTransactional) {
    return hTable.checkAndDelete(bytes, bytes1, bytes2, compareOp, bytes3, delete);
  } else {
    throw new UnsupportedOperationException("Operation is not supported transactionally");
  }
}
 
Example #13
Source File: TransactionAwareHTable.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkAndPut(byte[] bytes, byte[] bytes1, byte[] bytes2, CompareFilter.CompareOp compareOp,
                           byte[] bytes3, Put put) throws IOException {
  if (allowNonTransactional) {
    return hTable.checkAndPut(bytes, bytes1, bytes2, compareOp, bytes3, put);
  } else {
    throw new UnsupportedOperationException("Operation is not supported transactionally");
  }
}
 
Example #14
Source File: TransactionAwareHTable.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkAndMutate(byte[] row, byte[] family, byte[] qualifier,
                              CompareFilter.CompareOp compareOp, byte[] value, RowMutations rowMutations)
    throws IOException {
  if (allowNonTransactional) {
    return hTable.checkAndMutate(row, family, qualifier, compareOp, value, rowMutations);
  }

  throw new UnsupportedOperationException("checkAndMutate operation is not supported transactionally");
}
 
Example #15
Source File: TransactionAwareHTable.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkAndMutate(byte[] row, byte[] family, byte[] qualifier,
                              CompareFilter.CompareOp compareOp, byte[] value, RowMutations rowMutations)
    throws IOException {
  if (allowNonTransactional) {
    return hTable.checkAndMutate(row, family, qualifier, compareOp, value, rowMutations);
  }

  throw new UnsupportedOperationException("checkAndMutate operation is not supported transactionally");
}
 
Example #16
Source File: MockHTable.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
private boolean check(byte[] row, byte[] family, byte[] qualifier, CompareFilter.CompareOp compareOp, byte[] value) {
    NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> rowData = data.get(row);
    if (value == null)
        return rowData == null ||
                !rowData.containsKey(family) ||
                !rowData.get(family).containsKey(qualifier);
    else if (rowData != null &&
            rowData.containsKey(family) &&
            rowData.get(family).containsKey(qualifier) &&
            !rowData.get(family).get(qualifier).isEmpty()) {

        byte[] oldValue = rowData.get(family).get(qualifier).lastEntry().getValue();
        int compareResult = Bytes.compareTo(value, oldValue);
        switch (compareOp) {
            case LESS:
                return compareResult < 0;
            case LESS_OR_EQUAL:
                return compareResult <= 0;
            case EQUAL:
                return compareResult == 0;
            case NOT_EQUAL:
                return compareResult != 0;
            case GREATER_OR_EQUAL:
                return compareResult >= 0;
            case GREATER:
                return compareResult > 0;
            default:
                throw new RuntimeException("Unknown Compare op " + compareOp.name());
        }
    } else {
        return false;
    }
}
 
Example #17
Source File: MockHTable.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean checkAndPut(byte[] row, byte[] family, byte[] qualifier, CompareFilter.CompareOp compareOp, byte[] value, Put put) throws IOException {
    if (check(row, family, qualifier, compareOp, value)) {
        put(put);
        return true;
    }
    return false;
}
 
Example #18
Source File: HBaseResourceStore.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
private FilterList generateTimeFilterList(VisitFilter visitFilter) {
    FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
    if (visitFilter.lastModStart >= 0) { // NOTE: Negative value does not work in its binary form
        SingleColumnValueFilter timeStartFilter = new SingleColumnValueFilter(B_FAMILY, B_COLUMN_TS,
                CompareFilter.CompareOp.GREATER_OR_EQUAL, Bytes.toBytes(visitFilter.lastModStart));
        filterList.addFilter(timeStartFilter);
    }
    if (visitFilter.lastModEndExclusive != Long.MAX_VALUE) {
        SingleColumnValueFilter timeEndFilter = new SingleColumnValueFilter(B_FAMILY, B_COLUMN_TS,
                CompareFilter.CompareOp.LESS, Bytes.toBytes(visitFilter.lastModEndExclusive));
        filterList.addFilter(timeEndFilter);
    }
    return filterList.getFilters().isEmpty() ? null : filterList;
}
 
Example #19
Source File: MockHTable.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean checkAndMutate(byte[] row, byte[] family, byte[] qualifier, CompareFilter.CompareOp compareOp, byte[] value, RowMutations rm) throws IOException {
    if (check(row, family, qualifier, compareOp, value)) {
        mutateRow(rm);
        return true;
    }
    return false;
}
 
Example #20
Source File: MockHTable.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public long incrementColumnValue(byte[] row, byte[] family, byte[] qualifier, long amount) throws IOException {
    if (check(row, family, qualifier, CompareFilter.CompareOp.EQUAL, null)) {
        Put put = new Put(row);
        put.addColumn(family, qualifier, Bytes.toBytes(amount));
        put(put);
        return amount;
    }
    NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> rowData = data.get(row);
    long newValue = Bytes.toLong(rowData.get(family).get(qualifier).lastEntry().getValue()) + amount;
    rowData.get(family).get(qualifier).put(System.currentTimeMillis(), Bytes.toBytes(newValue));
    return newValue;
}
 
Example #21
Source File: ElementModel.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
protected Scan getPropertyScan(String label) {
    Scan scan = new Scan();
    SingleColumnValueFilter valueFilter = new SingleColumnValueFilter(Constants.DEFAULT_FAMILY_BYTES,
            Constants.LABEL_BYTES, CompareFilter.CompareOp.EQUAL, new BinaryComparator(ValueUtils.serialize(label)));
    valueFilter.setFilterIfMissing(true);
    scan.setFilter(valueFilter);
    return scan;
}
 
Example #22
Source File: ElementModel.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
protected Scan getPropertyScan(String label, byte[] key, byte[] val) {
    Scan scan = new Scan();
    SingleColumnValueFilter labelFilter = new SingleColumnValueFilter(Constants.DEFAULT_FAMILY_BYTES,
            Constants.LABEL_BYTES, CompareFilter.CompareOp.EQUAL, new BinaryComparator(ValueUtils.serialize(label)));
    labelFilter.setFilterIfMissing(true);
    SingleColumnValueFilter valueFilter = new SingleColumnValueFilter(Constants.DEFAULT_FAMILY_BYTES,
            key, CompareFilter.CompareOp.EQUAL, new BinaryComparator(val));
    valueFilter.setFilterIfMissing(true);
    FilterList filterList = new FilterList(labelFilter, valueFilter);
    scan.setFilter(filterList);
    return scan;
}
 
Example #23
Source File: HBaseIOTest.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Tests reading all rows using a filter. */
@Test
public void testReadingWithFilter() 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), false, 20);
}
 
Example #24
Source File: HBaseIOTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #25
Source File: HBaseResourceStore.java    From kylin with Apache License 2.0 5 votes vote down vote up
private FilterList generateTimeFilterList(VisitFilter visitFilter) {
    FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
    if (visitFilter.lastModStart >= 0) { // NOTE: Negative value does not work in its binary form
        SingleColumnValueFilter timeStartFilter = new SingleColumnValueFilter(B_FAMILY, B_COLUMN_TS,
                CompareFilter.CompareOp.GREATER_OR_EQUAL, Bytes.toBytes(visitFilter.lastModStart));
        filterList.addFilter(timeStartFilter);
    }
    if (visitFilter.lastModEndExclusive != Long.MAX_VALUE) {
        SingleColumnValueFilter timeEndFilter = new SingleColumnValueFilter(B_FAMILY, B_COLUMN_TS,
                CompareFilter.CompareOp.LESS, Bytes.toBytes(visitFilter.lastModEndExclusive));
        filterList.addFilter(timeEndFilter);
    }
    return filterList.getFilters().isEmpty() ? null : filterList;
}
 
Example #26
Source File: TestHBaseStorageFiltering.java    From spork with Apache License 2.0 5 votes vote down vote up
@Test
public void testPrefix() throws Exception {
    Filter filter = getHBaseStorageFilter("cf1:foo*");
    List<Filter> childFilters = assertFilterList(filter, FilterList.Operator.MUST_PASS_ALL, 1);
    childFilters = assertFilterList(childFilters.get(0), FilterList.Operator.MUST_PASS_ONE, 1);
    childFilters = assertFilterList(childFilters.get(0), FilterList.Operator.MUST_PASS_ALL, 2);
    assertFamilyFilter(childFilters.get(0), CompareFilter.CompareOp.EQUAL, "cf1");
    childFilters = assertFilterList(childFilters.get(1), FilterList.Operator.MUST_PASS_ONE, 1);
    assertPrefixFilter(childFilters.get(0), "foo");
}
 
Example #27
Source File: TestHBaseStorageFiltering.java    From spork with Apache License 2.0 5 votes vote down vote up
@Test
public void testDescriptorsAndPrefix() throws Exception {
    Filter filter = getHBaseStorageFilter("cf1:a cf1:b cf2:foo*");
    List<Filter> childFilters = assertFilterList(filter, FilterList.Operator.MUST_PASS_ALL, 1);
    List<Filter> groupFilters = assertFilterList(childFilters.get(0), FilterList.Operator.MUST_PASS_ONE, 2);

    List<Filter> firstFilters = assertFilterList(groupFilters.get(0), FilterList.Operator.MUST_PASS_ALL, 2);
    FamilyFilter firstFamilyFilter = assertFamilyFilter(firstFilters.get(0), CompareFilter.CompareOp.EQUAL);

    List<Filter> secondFilters = assertFilterList(groupFilters.get(1), FilterList.Operator.MUST_PASS_ALL, 2);
    FamilyFilter secondFamilyFilter = assertFamilyFilter(secondFilters.get(0), CompareFilter.CompareOp.EQUAL);

    // one of the above will be the cf1 filters, one will be the cf2. Order is unknown
    Filter cf1ColumnList;
    Filter cf2ColumnList;
    if (Bytes.toString(firstFamilyFilter.getComparator().getValue()).equals("cf1")) {
        assertEquals("cf2", Bytes.toString(secondFamilyFilter.getComparator().getValue()));
        cf1ColumnList = firstFilters.get(1);
        cf2ColumnList = secondFilters.get(1);
    }
    else {
        assertEquals("cf1", Bytes.toString(secondFamilyFilter.getComparator().getValue()));
        assertEquals("cf2", Bytes.toString(firstFamilyFilter.getComparator().getValue()));
        cf1ColumnList = secondFilters.get(1);
        cf2ColumnList = firstFilters.get(1);
    }

    List<Filter> c1ColumnFilters = assertFilterList(cf1ColumnList, FilterList.Operator.MUST_PASS_ONE, 2);
    assertQualifierFilter(c1ColumnFilters.get(0), CompareFilter.CompareOp.EQUAL, "a");
    assertQualifierFilter(c1ColumnFilters.get(1), CompareFilter.CompareOp.EQUAL, "b");

    List<Filter> c2ColumnFilters = assertFilterList(cf2ColumnList, FilterList.Operator.MUST_PASS_ONE, 1);
    assertPrefixFilter(c2ColumnFilters.get(0), "foo");
}
 
Example #28
Source File: TestHBaseStorageFiltering.java    From spork with Apache License 2.0 5 votes vote down vote up
private FamilyFilter assertFamilyFilter(Filter filter, CompareFilter.CompareOp compareOp) {
    assertTrue("Filter is not a FamilyFilter: " + filter.getClass().getSimpleName(),
            filter instanceof FamilyFilter);
    FamilyFilter familyFilter = (FamilyFilter)filter;
    assertEquals("Unexpected compareOp", compareOp, familyFilter.getOperator());
    return familyFilter;
}
 
Example #29
Source File: TestHBaseStorageFiltering.java    From spork with Apache License 2.0 5 votes vote down vote up
private void assertQualifierFilter(Filter filter, CompareFilter.CompareOp compareOp, String value) {
    assertTrue("Filter is not a QualifierFilter: " + filter.getClass().getSimpleName(),
            filter instanceof QualifierFilter);
    QualifierFilter qualifierFilter = (QualifierFilter)filter;
    assertEquals("Unexpected compareOp", compareOp, qualifierFilter.getOperator());
    assertEquals("Unexpected value", value, Bytes.toString(qualifierFilter.getComparator().getValue()));
}
 
Example #30
Source File: HbaseAgentEventDao.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Override
public List<AgentEventBo> getAgentEvents(String agentId, Range range, Set<AgentEventType> excludeEventTypes) {
    Objects.requireNonNull(agentId, "agentId");
    Objects.requireNonNull(range, "range");

    Scan scan = new Scan();
    scan.setMaxVersions(1);
    scan.setCaching(SCANNER_CACHE_SIZE);

    scan.setStartRow(createRowKey(agentId, range.getTo()));
    scan.setStopRow(createRowKey(agentId, range.getFrom()));
    scan.addFamily(descriptor.getColumnFamilyName());

    if (!CollectionUtils.isEmpty(excludeEventTypes)) {
        FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
        for (AgentEventType excludeEventType : excludeEventTypes) {
            byte[] excludeQualifier = Bytes.toBytes(excludeEventType.getCode());
            filterList.addFilter(new QualifierFilter(CompareFilter.CompareOp.NOT_EQUAL, new BinaryComparator(excludeQualifier)));
        }
        scan.setFilter(filterList);
    }

    TableName agentEventTableName = descriptor.getTableName();
    List<AgentEventBo> agentEvents = this.hbaseOperations2.find(agentEventTableName, scan, agentEventResultsExtractor);
    logger.debug("agentEvents found. {}", agentEvents);
    return agentEvents;
}