Java Code Examples for org.apache.hadoop.hbase.CompareOperator

The following examples show how to use org.apache.hadoop.hbase.CompareOperator. These examples are extracted from open source projects. 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 Project: hbase-operator-tools   Source File: RegionsMerger.java    License: Apache License 2.0 6 votes vote down vote up
private List<RegionInfo> getOpenRegions(Connection connection, TableName table) throws Exception {
  List<RegionInfo> regions = new ArrayList<>();
  Table metaTbl = connection.getTable(META_TABLE_NAME);
  String tblName = table.getNameAsString();
  RowFilter rowFilter = new RowFilter(CompareOperator.EQUAL,
    new SubstringComparator(tblName+","));
  SingleColumnValueFilter colFilter = new SingleColumnValueFilter(CATALOG_FAMILY,
    STATE_QUALIFIER, CompareOperator.EQUAL, Bytes.toBytes("OPEN"));
  Scan scan = new Scan();
  FilterList filter = new FilterList(FilterList.Operator.MUST_PASS_ALL);
  filter.addFilter(rowFilter);
  filter.addFilter(colFilter);
  scan.setFilter(filter);
  try(ResultScanner rs = metaTbl.getScanner(scan)){
    Result r;
    while ((r = rs.next()) != null) {
      RegionInfo region = RegionInfo.parseFrom(r.getValue(CATALOG_FAMILY, REGIONINFO_QUALIFIER));
      regions.add(region);
    }
  }
  return regions;
}
 
Example 2
Source Project: hbase   Source File: RequestConverter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Create a protocol buffer Condition
 *
 * @return a Condition
 * @throws IOException
 */
public static Condition buildCondition(final byte[] row, final byte[] family,
  final byte[] qualifier, final CompareOperator op, final byte[] value, final Filter filter,
  final TimeRange timeRange) throws IOException {

  Condition.Builder builder = Condition.newBuilder().setRow(UnsafeByteOperations.unsafeWrap(row));

  if (filter != null) {
    builder.setFilter(ProtobufUtil.toFilter(filter));
  } else {
    builder.setFamily(UnsafeByteOperations.unsafeWrap(family))
      .setQualifier(UnsafeByteOperations.unsafeWrap(
        qualifier == null ? HConstants.EMPTY_BYTE_ARRAY : qualifier))
      .setComparator(ProtobufUtil.toComparator(new BinaryComparator(value)))
      .setCompareType(CompareType.valueOf(op.name()));
  }

  return builder.setTimeRange(ProtobufUtil.toTimeRange(timeRange)).build();
}
 
Example 3
Source Project: hbase   Source File: ColumnValueFilter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creating this filter by reflection, it is used by {@link ParseFilter},
 * @param filterArguments arguments for creating a ColumnValueFilter
 * @return a ColumnValueFilter
 */
public static Filter createFilterFromArguments(ArrayList<byte[]> filterArguments) {
  Preconditions.checkArgument(filterArguments.size() == 4,
    "Expect 4 arguments: %s", filterArguments.size());
  byte[] family = ParseFilter.removeQuotesFromByteArray(filterArguments.get(0));
  byte[] qualifier = ParseFilter.removeQuotesFromByteArray(filterArguments.get(1));
  CompareOperator operator = ParseFilter.createCompareOperator(filterArguments.get(2));
  ByteArrayComparable comparator =
    ParseFilter.createComparator(ParseFilter.removeQuotesFromByteArray(filterArguments.get(3)));

  if (comparator instanceof RegexStringComparator ||
      comparator instanceof SubstringComparator) {
    if (operator != CompareOperator.EQUAL &&
        operator != CompareOperator.NOT_EQUAL) {
      throw new IllegalArgumentException("A regexstring comparator and substring comparator " +
          "can only be used with EQUAL and NOT_EQUAL");
    }
  }

  return new ColumnValueFilter(family, qualifier, operator, comparator);
}
 
Example 4
Source Project: hbase   Source File: TestThriftConnection.java    License: Apache License 2.0 6 votes vote down vote up
private void testScanWithFilters(Connection connection, String tableName) throws IOException {
  createTable(thriftAdmin, tableName);
  try (Table table = connection.getTable(TableName.valueOf(tableName))){
    FilterList filterList = new FilterList();
    PrefixFilter prefixFilter = new PrefixFilter(Bytes.toBytes("testrow"));
    ColumnValueFilter columnValueFilter = new ColumnValueFilter(FAMILYA, QUALIFIER_1,
        CompareOperator.EQUAL, VALUE_1);
    filterList.addFilter(prefixFilter);
    filterList.addFilter(columnValueFilter);
    Scan scan = new Scan();
    scan.readVersions(2);
    scan.setFilter(filterList);
    ResultScanner scanner = table.getScanner(scan);
    Iterator<Result> iterator = scanner.iterator();
    assertTrue(iterator.hasNext());
    int counter = 0;
    while (iterator.hasNext()) {
      Result result = iterator.next();
      counter += result.size();
    }
    assertEquals(2, counter);
  }
}
 
Example 5
Source Project: hbase   Source File: SingleColumnValueFilter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @param pbBytes A pb serialized {@link SingleColumnValueFilter} instance
 * @return An instance of {@link SingleColumnValueFilter} made from <code>bytes</code>
 * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
 * @see #toByteArray
 */
public static SingleColumnValueFilter parseFrom(final byte [] pbBytes)
throws DeserializationException {
  FilterProtos.SingleColumnValueFilter proto;
  try {
    proto = FilterProtos.SingleColumnValueFilter.parseFrom(pbBytes);
  } catch (InvalidProtocolBufferException e) {
    throw new DeserializationException(e);
  }

  final CompareOperator compareOp =
    CompareOperator.valueOf(proto.getCompareOp().name());
  final org.apache.hadoop.hbase.filter.ByteArrayComparable comparator;
  try {
    comparator = ProtobufUtil.toComparator(proto.getComparator());
  } catch (IOException ioe) {
    throw new DeserializationException(ioe);
  }

  return new SingleColumnValueFilter(proto.hasColumnFamily() ? proto.getColumnFamily()
      .toByteArray() : null, proto.hasColumnQualifier() ? proto.getColumnQualifier()
      .toByteArray() : null, compareOp, comparator, proto.getFilterIfMissing(), proto
      .getLatestVersionOnly());
}
 
Example 6
Source Project: hbase   Source File: CompareFilter.java    License: Apache License 2.0 6 votes vote down vote up
public static ArrayList<Object> extractArguments(ArrayList<byte []> filterArguments) {
  Preconditions.checkArgument(filterArguments.size() == 2,
                              "Expected 2 but got: %s", filterArguments.size());
  CompareOperator op = ParseFilter.createCompareOperator(filterArguments.get(0));
  ByteArrayComparable comparator = ParseFilter.createComparator(
    ParseFilter.removeQuotesFromByteArray(filterArguments.get(1)));

  if (comparator instanceof RegexStringComparator ||
      comparator instanceof SubstringComparator) {
    if (op != CompareOperator.EQUAL &&
        op != CompareOperator.NOT_EQUAL) {
      throw new IllegalArgumentException ("A regexstring comparator and substring comparator" +
                                          " can only be used with EQUAL and NOT_EQUAL");
    }
  }
  ArrayList<Object> arguments = new ArrayList<>(2);
  arguments.add(op);
  arguments.add(comparator);
  return arguments;
}
 
Example 7
Source Project: hbase   Source File: ColumnValueFilter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Parse protobuf bytes to a ColumnValueFilter
 * @param pbBytes pbBytes
 * @return a ColumnValueFilter
 * @throws DeserializationException deserialization exception
 */
public static ColumnValueFilter parseFrom(final byte[] pbBytes) throws DeserializationException {
  FilterProtos.ColumnValueFilter proto;
  try {
    proto = FilterProtos.ColumnValueFilter.parseFrom(pbBytes);
  } catch (InvalidProtocolBufferException e) {
    throw new DeserializationException(e);
  }

  final CompareOperator compareOp = CompareOperator.valueOf(proto.getCompareOp().name());
  final ByteArrayComparable comparator;
  try {
    comparator = ProtobufUtil.toComparator(proto.getComparator());
  } catch (IOException ioe) {
    throw new DeserializationException(ioe);
  }

  return new ColumnValueFilter(proto.getFamily().toByteArray(),
    proto.getQualifier().toByteArray(), compareOp, comparator);
}
 
Example 8
Source Project: hbase   Source File: RegionCoprocessorHost.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Supports Coprocessor 'bypass'.
 * @param row row to check
 * @param family column family
 * @param qualifier column qualifier
 * @param op the comparison operation
 * @param comparator the comparator
 * @param put data to put if check succeeds
 * @return true or false to return to client if default processing should be bypassed, or null
 * otherwise
 */
public Boolean preCheckAndPut(final byte [] row, final byte [] family,
    final byte [] qualifier, final CompareOperator op,
    final ByteArrayComparable comparator, final Put put)
    throws IOException {
  boolean bypassable = true;
  boolean defaultResult = false;
  if (coprocEnvironments.isEmpty()) {
    return null;
  }
  return execOperationWithResult(
      new ObserverOperationWithResult<RegionObserver, Boolean>(regionObserverGetter,
          defaultResult,  bypassable) {
        @Override
        public Boolean call(RegionObserver observer) throws IOException {
          return observer.preCheckAndPut(this, row, family, qualifier,
              op, comparator, put, getResult());
        }
      });
}
 
Example 9
Source Project: hbase   Source File: RegionCoprocessorHost.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Supports Coprocessor 'bypass'.
 * @param row row to check
 * @param family column family
 * @param qualifier column qualifier
 * @param op the comparison operation
 * @param comparator the comparator
 * @param put data to put if check succeeds
 * @return true or false to return to client if default processing should be bypassed, or null
 *   otherwise
 */
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value="NP_BOOLEAN_RETURN_NULL",
    justification="Null is legit")
public Boolean preCheckAndPutAfterRowLock(
    final byte[] row, final byte[] family, final byte[] qualifier, final CompareOperator op,
    final ByteArrayComparable comparator, final Put put) throws IOException {
  boolean bypassable = true;
  boolean defaultResult = false;
  if (coprocEnvironments.isEmpty()) {
    return null;
  }
  return execOperationWithResult(
      new ObserverOperationWithResult<RegionObserver, Boolean>(regionObserverGetter,
          defaultResult, bypassable) {
        @Override
        public Boolean call(RegionObserver observer) throws IOException {
          return observer.preCheckAndPutAfterRowLock(this, row, family, qualifier,
              op, comparator, put, getResult());
        }
      });
}
 
Example 10
Source Project: hbase   Source File: RegionCoprocessorHost.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @param row row to check
 * @param family column family
 * @param qualifier column qualifier
 * @param op the comparison operation
 * @param comparator the comparator
 * @param put data to put if check succeeds
 * @throws IOException e
 */
public boolean postCheckAndPut(final byte [] row, final byte [] family,
    final byte [] qualifier, final CompareOperator op,
    final ByteArrayComparable comparator, final Put put,
    boolean result) throws IOException {
  if (this.coprocEnvironments.isEmpty()) {
    return result;
  }
  return execOperationWithResult(
      new ObserverOperationWithResult<RegionObserver, Boolean>(regionObserverGetter, result) {
        @Override
        public Boolean call(RegionObserver observer) throws IOException {
          return observer.postCheckAndPut(this, row, family, qualifier,
              op, comparator, put, getResult());
        }
      });
}
 
Example 11
Source Project: hbase   Source File: RegionCoprocessorHost.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Supports Coprocessor 'bypass'.
 * @param row row to check
 * @param family column family
 * @param qualifier column qualifier
 * @param op the comparison operation
 * @param comparator the comparator
 * @param delete delete to commit if check succeeds
 * @return true or false to return to client if default processing should be bypassed, or null
 *   otherwise
 */
public Boolean preCheckAndDelete(final byte [] row, final byte [] family,
    final byte [] qualifier, final CompareOperator op,
    final ByteArrayComparable comparator, final Delete delete)
    throws IOException {
  boolean bypassable = true;
  boolean defaultResult = false;
  if (coprocEnvironments.isEmpty()) {
    return null;
  }
  return execOperationWithResult(
      new ObserverOperationWithResult<RegionObserver, Boolean>(regionObserverGetter,
          defaultResult, bypassable) {
        @Override
        public Boolean call(RegionObserver observer) throws IOException {
          return observer.preCheckAndDelete(this, row, family,
              qualifier, op, comparator, delete, getResult());
        }
      });
}
 
Example 12
Source Project: hbase   Source File: RegionCoprocessorHost.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @param row row to check
 * @param family column family
 * @param qualifier column qualifier
 * @param op the comparison operation
 * @param comparator the comparator
 * @param delete delete to commit if check succeeds
 * @throws IOException e
 */
public boolean postCheckAndDelete(final byte [] row, final byte [] family,
    final byte [] qualifier, final CompareOperator op,
    final ByteArrayComparable comparator, final Delete delete,
    boolean result) throws IOException {
  if (this.coprocEnvironments.isEmpty()) {
    return result;
  }
  return execOperationWithResult(
      new ObserverOperationWithResult<RegionObserver, Boolean>(regionObserverGetter, result) {
        @Override
        public Boolean call(RegionObserver observer) throws IOException {
          return observer.postCheckAndDelete(this, row, family,
              qualifier, op, comparator, delete, getResult());
        }
      });
}
 
Example 13
Source Project: hbase   Source File: QualifierFilter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @param pbBytes A pb serialized {@link QualifierFilter} instance
 * @return An instance of {@link QualifierFilter} made from <code>bytes</code>
 * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
 * @see #toByteArray
 */
public static QualifierFilter parseFrom(final byte [] pbBytes)
throws DeserializationException {
  FilterProtos.QualifierFilter proto;
  try {
    proto = FilterProtos.QualifierFilter.parseFrom(pbBytes);
  } catch (InvalidProtocolBufferException e) {
    throw new DeserializationException(e);
  }
  final CompareOperator valueCompareOp =
    CompareOperator.valueOf(proto.getCompareFilter().getCompareOp().name());
  ByteArrayComparable valueComparator = null;
  try {
    if (proto.getCompareFilter().hasComparator()) {
      valueComparator = ProtobufUtil.toComparator(proto.getCompareFilter().getComparator());
    }
  } catch (IOException ioe) {
    throw new DeserializationException(ioe);
  }
  return new QualifierFilter(valueCompareOp,valueComparator);
}
 
Example 14
Source Project: hbase   Source File: TestTableInputFormat.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void configure(JobConf job) {
  try {
    Connection connection = ConnectionFactory.createConnection(HBaseConfiguration.create(job));
    TableName tableName = TableName.valueOf("exampleJobConfigurableTable");
    // mandatory
    initializeTable(connection, tableName);
    byte[][] inputColumns = new byte [][] { Bytes.toBytes("columnA"),
      Bytes.toBytes("columnB") };
    //optional
    Scan scan = new Scan();
    for (byte[] family : inputColumns) {
      scan.addFamily(family);
    }
    Filter exampleFilter =
      new RowFilter(CompareOperator.EQUAL, new RegexStringComparator("aa.*"));
    scan.setFilter(exampleFilter);
    setScan(scan);
  } catch (IOException exception) {
    throw new RuntimeException("Failed to initialize.", exception);
  }
}
 
Example 15
Source Project: hbase   Source File: TestFilterSerialization.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSingleColumnValueFilter() throws Exception {
  // null family/column SingleColumnValueFilter
  SingleColumnValueFilter singleColumnValueFilter =
    new SingleColumnValueFilter(null, null,
    CompareOperator.LESS, Bytes.toBytes("value"));
  assertTrue(singleColumnValueFilter.areSerializedFieldsEqual(
    ProtobufUtil.toFilter(ProtobufUtil.toFilter(singleColumnValueFilter))));

  // non-null family/column SingleColumnValueFilter
  singleColumnValueFilter =
    new SingleColumnValueFilter(Bytes.toBytes("family"), Bytes.toBytes("qualifier"),
     CompareOperator.NOT_EQUAL, new NullComparator(), true, true);
  assertTrue(singleColumnValueFilter.areSerializedFieldsEqual(
    ProtobufUtil.toFilter(ProtobufUtil.toFilter(singleColumnValueFilter))));
}
 
Example 16
Source Project: hbase   Source File: SingleColumnValueExcludeFilter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @param pbBytes A pb serialized {@link SingleColumnValueExcludeFilter} instance
 * @return An instance of {@link SingleColumnValueExcludeFilter} made from <code>bytes</code>
 * @throws DeserializationException
 * @see #toByteArray
 */
public static SingleColumnValueExcludeFilter parseFrom(final byte [] pbBytes)
throws DeserializationException {
  FilterProtos.SingleColumnValueExcludeFilter proto;
  try {
    proto = FilterProtos.SingleColumnValueExcludeFilter.parseFrom(pbBytes);
  } catch (InvalidProtocolBufferException e) {
    throw new DeserializationException(e);
  }

  FilterProtos.SingleColumnValueFilter parentProto = proto.getSingleColumnValueFilter();
  final CompareOperator compareOp =
    CompareOperator.valueOf(parentProto.getCompareOp().name());
  final ByteArrayComparable comparator;
  try {
    comparator = ProtobufUtil.toComparator(parentProto.getComparator());
  } catch (IOException ioe) {
    throw new DeserializationException(ioe);
  }

  return new SingleColumnValueExcludeFilter(parentProto.hasColumnFamily() ? parentProto
      .getColumnFamily().toByteArray() : null, parentProto.hasColumnQualifier() ? parentProto
      .getColumnQualifier().toByteArray() : null, compareOp, comparator, parentProto
      .getFilterIfMissing(), parentProto.getLatestVersionOnly());
}
 
Example 17
Source Project: hbase   Source File: RequestConverter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Create a protocol buffer MutateRequest for a conditioned put/delete
 *
 * @return a mutate request
 * @throws IOException
 */
public static MutateRequest buildMutateRequest(final byte[] regionName, final byte[] row,
  final byte[] family, final byte[] qualifier, final CompareOperator op, final byte[] value,
  final Filter filter, final TimeRange timeRange, final Mutation mutation) throws IOException {
  MutationType type;
  if (mutation instanceof Put) {
    type = MutationType.PUT;
  } else {
    type = MutationType.DELETE;
  }
  return MutateRequest.newBuilder()
    .setRegion(buildRegionSpecifier(RegionSpecifierType.REGION_NAME, regionName))
    .setMutation(ProtobufUtil.toMutation(type, mutation))
    .setCondition(buildCondition(row, family, qualifier, op, value, filter, timeRange))
    .build();
}
 
Example 18
Source Project: hbase   Source File: TestScannersFromClientSide.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testScanWithColumnsAndFilterAndVersion() throws IOException {
  TableName tableName = name.getTableName();
  long now = System.currentTimeMillis();
  try (Table table = TEST_UTIL.createTable(tableName, FAMILY, 4)) {
    for (int i = 0; i < 4; i++) {
      Put put = new Put(ROW);
      put.addColumn(FAMILY, QUALIFIER, now + i, VALUE);
      table.put(put);
    }

    Scan scan = new Scan();
    scan.addColumn(FAMILY, QUALIFIER);
    scan.setFilter(new QualifierFilter(CompareOperator.EQUAL, new BinaryComparator(QUALIFIER)));
    scan.readVersions(3);

    try (ResultScanner scanner = table.getScanner(scan)) {
      Result result = scanner.next();
      assertEquals(3, result.size());
    }
  }
}
 
Example 19
Source Project: hbase   Source File: FamilyFilter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @param pbBytes A pb serialized {@link FamilyFilter} instance
 * @return An instance of {@link FamilyFilter} made from <code>bytes</code>
 * @throws DeserializationException
 * @see #toByteArray
 */
public static FamilyFilter parseFrom(final byte [] pbBytes)
throws DeserializationException {
  FilterProtos.FamilyFilter proto;
  try {
    proto = FilterProtos.FamilyFilter.parseFrom(pbBytes);
  } catch (InvalidProtocolBufferException e) {
    throw new DeserializationException(e);
  }
  final CompareOperator valueCompareOp =
    CompareOperator.valueOf(proto.getCompareFilter().getCompareOp().name());
  ByteArrayComparable valueComparator = null;
  try {
    if (proto.getCompareFilter().hasComparator()) {
      valueComparator = ProtobufUtil.toComparator(proto.getCompareFilter().getComparator());
    }
  } catch (IOException ioe) {
    throw new DeserializationException(ioe);
  }
  return new FamilyFilter(valueCompareOp,valueComparator);
}
 
Example 20
Source Project: hbase   Source File: ValueFilter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @param pbBytes A pb serialized {@link ValueFilter} instance
 * @return An instance of {@link ValueFilter} made from <code>bytes</code>
 * @throws DeserializationException
 * @see #toByteArray
 */
public static ValueFilter parseFrom(final byte [] pbBytes)
throws DeserializationException {
  FilterProtos.ValueFilter proto;
  try {
    proto = FilterProtos.ValueFilter.parseFrom(pbBytes);
  } catch (InvalidProtocolBufferException e) {
    throw new DeserializationException(e);
  }
  final CompareOperator valueCompareOp =
    CompareOperator.valueOf(proto.getCompareFilter().getCompareOp().name());
  ByteArrayComparable valueComparator = null;
  try {
    if (proto.getCompareFilter().hasComparator()) {
      valueComparator = ProtobufUtil.toComparator(proto.getCompareFilter().getComparator());
    }
  } catch (IOException ioe) {
    throw new DeserializationException(ioe);
  }
  return new ValueFilter(valueCompareOp,valueComparator);
}
 
Example 21
Source Project: hbase   Source File: CompareFilter.java    License: Apache License 2.0 5 votes vote down vote up
protected boolean compareQualifier(final CompareOperator op,
                                   final ByteArrayComparable comparator, final Cell cell) {
  // We do not call through to the non-deprecated method for perf reasons.
  if (op == CompareOperator.NO_OP) {
    return true;
  }
  int compareResult = PrivateCellUtil.compareQualifier(cell, comparator);
  return compare(op, compareResult);
}
 
Example 22
Source Project: hbase   Source File: TestFilterList.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testKeyOnlyFilterTransformCell() throws IOException {
  Cell c;
  KeyValue kv1 = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("cf"), Bytes.toBytes("column1"),
      1, Bytes.toBytes("value1"));
  KeyValue kv2 = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("cf"), Bytes.toBytes("column1"),
      2, Bytes.toBytes("value2"));

  Filter filter1 = new SingleColumnValueFilter(Bytes.toBytes("cf"), Bytes.toBytes("column1"),
      CompareOperator.EQUAL, Bytes.toBytes("value1"));
  Filter filter2 = new SingleColumnValueFilter(Bytes.toBytes("cf"), Bytes.toBytes("column1"),
      CompareOperator.EQUAL, Bytes.toBytes("value2"));
  FilterList internalFilterList = new FilterList(Operator.MUST_PASS_ONE, filter1, filter2);

  FilterList keyOnlyFilterFirst =
      new FilterList(Operator.MUST_PASS_ALL, new KeyOnlyFilter(), internalFilterList);

  assertEquals(ReturnCode.INCLUDE, keyOnlyFilterFirst.filterCell(kv1));
  c = keyOnlyFilterFirst.transformCell(kv1);
  assertEquals(0, c.getValueLength());
  assertEquals(ReturnCode.INCLUDE, keyOnlyFilterFirst.filterCell(kv2));
  c = keyOnlyFilterFirst.transformCell(kv2);
  assertEquals(0, c.getValueLength());

  internalFilterList.reset();
  FilterList keyOnlyFilterLast =
      new FilterList(Operator.MUST_PASS_ALL, new KeyOnlyFilter(), internalFilterList);
  assertEquals(ReturnCode.INCLUDE, keyOnlyFilterLast.filterCell(kv1));
  c = keyOnlyFilterLast.transformCell(kv1);
  assertEquals(0, c.getValueLength());
  assertEquals(ReturnCode.INCLUDE, keyOnlyFilterLast.filterCell(kv2));
  c = keyOnlyFilterLast.transformCell(kv2);
  assertEquals(0, c.getValueLength());
}
 
Example 23
Source Project: phoenix-tephra   Source File: TransactionAwareHTable.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkAndMutate(byte[] row, byte[] family, byte[] qualifier, CompareOperator op,
    byte[] value, RowMutations mutation) throws IOException {
  if (allowNonTransactional) {
    return hTable.checkAndMutate(row, family, qualifier, op, value, mutation);
  } else {
    throw new UnsupportedOperationException("Operation is not supported transactionally");
  }
}
 
Example 24
Source Project: hbase   Source File: ThriftUtilities.java    License: Apache License 2.0 5 votes vote down vote up
public static CompareOperator compareOpFromThrift(TCompareOperator tCompareOp) {
  switch (tCompareOp.getValue()) {
    case 0: return CompareOperator.LESS;
    case 1: return CompareOperator.LESS_OR_EQUAL;
    case 2: return CompareOperator.EQUAL;
    case 3: return CompareOperator.NOT_EQUAL;
    case 4: return CompareOperator.GREATER_OR_EQUAL;
    case 5: return CompareOperator.GREATER;
    case 6: return CompareOperator.NO_OP;
    default: return null;
  }
}
 
Example 25
Source Project: hbase   Source File: TestParseFilter.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testFamilyFilter() throws IOException {
  String filterString = "FamilyFilter(>=, 'binaryprefix:pre')";
  FamilyFilter familyFilter =
    doTestFilter(filterString, FamilyFilter.class);
  assertEquals(CompareOperator.GREATER_OR_EQUAL, familyFilter.getCompareOperator());
  assertTrue(familyFilter.getComparator() instanceof BinaryPrefixComparator);
  BinaryPrefixComparator binaryPrefixComparator =
    (BinaryPrefixComparator) familyFilter.getComparator();
  assertEquals("pre", new String(binaryPrefixComparator.getValue(), StandardCharsets.UTF_8));
}
 
Example 26
Source Project: hbase   Source File: TestParseFilter.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCompoundFilter3() throws IOException {
  String filterString = " ColumnPrefixFilter ('realtime')AND  " +
    "FirstKeyOnlyFilter() OR SKIP FamilyFilter(=, 'substring:hihi')";
  FilterList filterList =
    doTestFilter(filterString, FilterList.class);
  ArrayList<Filter> filters = (ArrayList<Filter>) filterList.getFilters();

  assertTrue(filters.get(0) instanceof FilterList);
  assertTrue(filters.get(1) instanceof SkipFilter);

  filterList = (FilterList) filters.get(0);
  SkipFilter skipFilter = (SkipFilter) filters.get(1);

  filters = (ArrayList<Filter>) filterList.getFilters();
  assertTrue(filters.get(0) instanceof ColumnPrefixFilter);
  assertTrue(filters.get(1) instanceof FirstKeyOnlyFilter);

  ColumnPrefixFilter columnPrefixFilter = (ColumnPrefixFilter) filters.get(0);
  byte [] columnPrefix = columnPrefixFilter.getPrefix();
  assertEquals("realtime", new String(columnPrefix, StandardCharsets.UTF_8));

  FirstKeyOnlyFilter firstKeyOnlyFilter = (FirstKeyOnlyFilter) filters.get(1);

  assertTrue(skipFilter.getFilter() instanceof FamilyFilter);
  FamilyFilter familyFilter = (FamilyFilter) skipFilter.getFilter();

  assertEquals(CompareOperator.EQUAL, familyFilter.getCompareOperator());
  assertTrue(familyFilter.getComparator() instanceof SubstringComparator);
  SubstringComparator substringComparator =
    (SubstringComparator) familyFilter.getComparator();
  assertEquals("hihi", new String(substringComparator.getValue(), StandardCharsets.UTF_8));
}
 
Example 27
Source Project: hbase   Source File: AccessController.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean preCheckAndDeleteAfterRowLock(
    final ObserverContext<RegionCoprocessorEnvironment> c, final byte[] row,
    final byte[] family, final byte[] qualifier, final CompareOperator op,
    final ByteArrayComparable comparator, final Delete delete, final boolean result)
    throws IOException {
  if (delete.getAttribute(CHECK_COVERING_PERM) != null) {
    // We had failure with table, cf and q perm checks and now giving a chance for cell
    // perm check
    TableName table = c.getEnvironment().getRegion().getRegionInfo().getTable();
    Map<byte[], ? extends Collection<byte[]>> families = makeFamilyMap(family, qualifier);
    AuthResult authResult = null;
    User user = getActiveUser(c);
    if (checkCoveringPermission(user, OpType.CHECK_AND_DELETE, c.getEnvironment(),
        row, families, HConstants.LATEST_TIMESTAMP, Action.READ)) {
      authResult = AuthResult.allow(OpType.CHECK_AND_DELETE.toString(),
          "Covering cell set", user, Action.READ, table, families);
    } else {
      authResult = AuthResult.deny(OpType.CHECK_AND_DELETE.toString(),
          "Covering cell set", user, Action.READ, table, families);
    }
    AccessChecker.logResult(authResult);
    if (authorizationEnabled && !authResult.isAllowed()) {
      throw new AccessDeniedException("Insufficient permissions " + authResult.toContextString());
    }
  }
  return result;
}
 
Example 28
Source Project: hbase   Source File: MetaBrowser.java    License: Apache License 2.0 5 votes vote down vote up
private static Filter buildScanRegionStateFilter(final RegionState.State state) {
  return new SingleColumnValueFilter(
    HConstants.CATALOG_FAMILY,
    HConstants.STATE_QUALIFIER,
    CompareOperator.EQUAL,
    // use the same serialization strategy as found in MetaTableAccessor#addRegionStateToPut
    Bytes.toBytes(state.name()));
}
 
Example 29
Source Project: hbase   Source File: TestFilter.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSkipFilter() throws IOException {

  // Test for qualifier regex: "testQualifierOne-2"
  // Should only get rows from second group, and all keys
  Filter f = new SkipFilter(new QualifierFilter(CompareOperator.NOT_EQUAL,
      new BinaryComparator(Bytes.toBytes("testQualifierOne-2"))));
  Scan s = new Scan();
  s.setFilter(f);

  KeyValue [] kvs = {
      // testRowTwo-0
      new KeyValue(ROWS_TWO[0], FAMILIES[0], QUALIFIERS_TWO[0], VALUES[1]),
      new KeyValue(ROWS_TWO[0], FAMILIES[0], QUALIFIERS_TWO[2], VALUES[1]),
      new KeyValue(ROWS_TWO[0], FAMILIES[0], QUALIFIERS_TWO[3], VALUES[1]),
      new KeyValue(ROWS_TWO[0], FAMILIES[1], QUALIFIERS_TWO[0], VALUES[1]),
      new KeyValue(ROWS_TWO[0], FAMILIES[1], QUALIFIERS_TWO[2], VALUES[1]),
      new KeyValue(ROWS_TWO[0], FAMILIES[1], QUALIFIERS_TWO[3], VALUES[1]),
      // testRowTwo-2
      new KeyValue(ROWS_TWO[2], FAMILIES[0], QUALIFIERS_TWO[0], VALUES[1]),
      new KeyValue(ROWS_TWO[2], FAMILIES[0], QUALIFIERS_TWO[2], VALUES[1]),
      new KeyValue(ROWS_TWO[2], FAMILIES[0], QUALIFIERS_TWO[3], VALUES[1]),
      new KeyValue(ROWS_TWO[2], FAMILIES[1], QUALIFIERS_TWO[0], VALUES[1]),
      new KeyValue(ROWS_TWO[2], FAMILIES[1], QUALIFIERS_TWO[2], VALUES[1]),
      new KeyValue(ROWS_TWO[2], FAMILIES[1], QUALIFIERS_TWO[3], VALUES[1]),
      // testRowTwo-3
      new KeyValue(ROWS_TWO[3], FAMILIES[0], QUALIFIERS_TWO[0], VALUES[1]),
      new KeyValue(ROWS_TWO[3], FAMILIES[0], QUALIFIERS_TWO[2], VALUES[1]),
      new KeyValue(ROWS_TWO[3], FAMILIES[0], QUALIFIERS_TWO[3], VALUES[1]),
      new KeyValue(ROWS_TWO[3], FAMILIES[1], QUALIFIERS_TWO[0], VALUES[1]),
      new KeyValue(ROWS_TWO[3], FAMILIES[1], QUALIFIERS_TWO[2], VALUES[1]),
      new KeyValue(ROWS_TWO[3], FAMILIES[1], QUALIFIERS_TWO[3], VALUES[1]),
  };
  verifyScanFull(s, kvs);
}
 
Example 30
Source Project: hbase   Source File: TestFilterSerialization.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testColumnValueFilter() throws Exception {
  ColumnValueFilter columnValueFilter =
      new ColumnValueFilter(Bytes.toBytes("family"), Bytes.toBytes("qualifier"),
          CompareOperator.EQUAL, Bytes.toBytes("value"));
  assertTrue(columnValueFilter.areSerializedFieldsEqual(
      ProtobufUtil.toFilter(ProtobufUtil.toFilter(columnValueFilter))));
}