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

The following examples show how to use org.apache.hadoop.hbase.CompareOperator#EQUAL . 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: RegionsMerger.java    From hbase-operator-tools with 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 File: ColumnValueFilter.java    From hbase with 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 3
Source File: CompareFilter.java    From hbase with 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 4
Source File: ParseFilter.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Takes a compareOperator symbol as a byte array and returns the corresponding CompareOperator
 * @param compareOpAsByteArray the comparatorOperator symbol as a byte array
 * @return the Compare Operator
 */
public static CompareOperator createCompareOperator (byte [] compareOpAsByteArray) {
  ByteBuffer compareOp = ByteBuffer.wrap(compareOpAsByteArray);
  if (compareOp.equals(ParseConstants.LESS_THAN_BUFFER))
    return CompareOperator.LESS;
  else if (compareOp.equals(ParseConstants.LESS_THAN_OR_EQUAL_TO_BUFFER))
    return CompareOperator.LESS_OR_EQUAL;
  else if (compareOp.equals(ParseConstants.GREATER_THAN_BUFFER))
    return CompareOperator.GREATER;
  else if (compareOp.equals(ParseConstants.GREATER_THAN_OR_EQUAL_TO_BUFFER))
    return CompareOperator.GREATER_OR_EQUAL;
  else if (compareOp.equals(ParseConstants.NOT_EQUAL_TO_BUFFER))
    return CompareOperator.NOT_EQUAL;
  else if (compareOp.equals(ParseConstants.EQUAL_TO_BUFFER))
    return CompareOperator.EQUAL;
  else
    throw new IllegalArgumentException("Invalid compare operator");
}
 
Example 5
Source File: TestJoinedScanners.java    From hbase with Apache License 2.0 6 votes vote down vote up
private void runScanner(Table table, boolean slow) throws Exception {
  long time = System.nanoTime();
  Scan scan = new Scan();
  scan.addColumn(cf_essential, col_name);
  scan.addColumn(cf_joined, col_name);

  SingleColumnValueFilter filter = new SingleColumnValueFilter(
      cf_essential, col_name, CompareOperator.EQUAL, flag_yes);
  filter.setFilterIfMissing(true);
  scan.setFilter(filter);
  scan.setLoadColumnFamiliesOnDemand(!slow);

  ResultScanner result_scanner = table.getScanner(scan);
  Result res;
  long rows_count = 0;
  while ((res = result_scanner.next()) != null) {
    rows_count++;
  }

  double timeSec = (System.nanoTime() - time) / 1000000000.0;
  result_scanner.close();
  LOG.info((slow ? "Slow" : "Joined") + " scanner finished in " + Double.toString(timeSec)
    + " seconds, got " + Long.toString(rows_count/2) + " rows");
}
 
Example 6
Source File: TestFilterSerialization.java    From hbase with 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))));
}
 
Example 7
Source File: TestFilterWithScanLimits.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testScanWithLimit() {
  int kv_number = 0;
  try {
    Scan scan = new Scan();
    // set batch number as 2, which means each Result should contain 2 KVs at most
    scan.setBatch(2);
    SingleColumnValueFilter filter = new SingleColumnValueFilter(
        Bytes.toBytes(columnFamily), Bytes.toBytes("c5"),
    CompareOperator .EQUAL, new SubstringComparator("2_c5"));

    // add filter after batch defined
    scan.setFilter(filter);
    Table table = openTable(tableName);
    ResultScanner scanner = table.getScanner(scan);
    // Expect to get following row
    // row2 => <f1:c1, 2_c1>, <f1:c2, 2_c2>,
    // row2 => <f1:c3, 2_c3>, <f1:c4, 2_c4>,
    // row2 => <f1:c5, 2_c5>

    for (Result result : scanner) {
      for (Cell kv : result.listCells()) {
        kv_number++;
        LOG.debug(kv_number + ". kv: " + kv);
      }
    }

    scanner.close();
    table.close();
  } catch (Exception e) {
    // no correct result is expected
    assertNotNull("No IncompatibleFilterException catched", e);
  }
  LOG.debug("check the fetched kv number");
  assertEquals("We should not get result(s) returned.", 0, kv_number);
}
 
Example 8
Source File: TestFilterSerialization.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testQualifierFilter() throws Exception {
  QualifierFilter qualifierFilter = new QualifierFilter(CompareOperator.EQUAL,
    new NullComparator());
  assertTrue(qualifierFilter.areSerializedFieldsEqual(
    ProtobufUtil.toFilter(ProtobufUtil.toFilter(qualifierFilter))));
}
 
Example 9
Source File: TestDependentColumnFilter.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Test for HBASE-8794. Avoid NullPointerException in DependentColumnFilter.toString().
 */
@Test
public void testToStringWithNullComparator() {
  // Test constructor that implicitly sets a null comparator
  Filter filter = new DependentColumnFilter(FAMILIES[0], QUALIFIER);
  assertNotNull(filter.toString());
  assertTrue("check string contains 'null' as compatator is null",
    filter.toString().contains("null"));

  // Test constructor with explicit null comparator
  filter = new DependentColumnFilter(FAMILIES[0], QUALIFIER, true, CompareOperator.EQUAL, null);
  assertNotNull(filter.toString());
  assertTrue("check string contains 'null' as compatator is null",
    filter.toString().contains("null"));
}
 
Example 10
Source File: TestFilterList.java    From hbase with 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 11
Source File: TestJoinedScanners.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test(expected = DoNotRetryIOException.class)
public void testWithReverseScan() throws Exception {
  try (Connection con = TEST_UTIL.getConnection(); Admin admin = con.getAdmin()) {
    TableName tableName = TableName.valueOf(name.getMethodName());

    TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName)
        .setColumnFamily(ColumnFamilyDescriptorBuilder.of("cf1"))
        .setColumnFamily(ColumnFamilyDescriptorBuilder.of("cf2"))
        .build();
    admin.createTable(tableDescriptor);

    try (Table table = con.getTable(tableName)) {
      SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("cf1"),
        Bytes.toBytes("col"), CompareOperator.EQUAL, Bytes.toBytes("val"));
      filter.setFilterIfMissing(true);

      // Reverse scan with loading CFs on demand
      Scan scan = new Scan();
      scan.setFilter(filter);
      scan.setReversed(true);
      scan.setLoadColumnFamiliesOnDemand(true);

      try (ResultScanner scanner = table.getScanner(scan)) {
        // DoNotRetryIOException should occur
        scanner.next();
      }
    }
  }
}
 
Example 12
Source File: TestFromClientSide.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test public void testFilters() throws Exception {
  final TableName tableName = name.getTableName();
  try (Table ht = TEST_UTIL.createTable(tableName, FAMILY)) {
    byte[][] ROWS = makeN(ROW, 10);
    byte[][] QUALIFIERS =
      { Bytes.toBytes("col0-<d2v1>-<d3v2>"), Bytes.toBytes("col1-<d2v1>-<d3v2>"),
        Bytes.toBytes("col2-<d2v1>-<d3v2>"), Bytes.toBytes("col3-<d2v1>-<d3v2>"),
        Bytes.toBytes("col4-<d2v1>-<d3v2>"), Bytes.toBytes("col5-<d2v1>-<d3v2>"),
        Bytes.toBytes("col6-<d2v1>-<d3v2>"), Bytes.toBytes("col7-<d2v1>-<d3v2>"),
        Bytes.toBytes("col8-<d2v1>-<d3v2>"), Bytes.toBytes("col9-<d2v1>-<d3v2>") };
    for (int i = 0; i < 10; i++) {
      Put put = new Put(ROWS[i]);
      put.setDurability(Durability.SKIP_WAL);
      put.addColumn(FAMILY, QUALIFIERS[i], VALUE);
      ht.put(put);
    }
    Scan scan = new Scan();
    scan.addFamily(FAMILY);
    Filter filter = new QualifierFilter(CompareOperator.EQUAL,
      new RegexStringComparator("col[1-5]"));
    scan.setFilter(filter);
    try (ResultScanner scanner = ht.getScanner(scan)) {
      int expectedIndex = 1;
      for (Result result : scanner) {
        assertEquals(1, result.size());
        assertTrue(Bytes.equals(CellUtil.cloneRow(result.rawCells()[0]), ROWS[expectedIndex]));
        assertTrue(Bytes.equals(CellUtil.cloneQualifier(result.rawCells()[0]),
          QUALIFIERS[expectedIndex]));
        expectedIndex++;
      }
      assertEquals(6, expectedIndex);
    }
  }
}
 
Example 13
Source File: TestFilterSerialization.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testRowFilter() throws Exception {
  RowFilter rowFilter = new RowFilter(CompareOperator.EQUAL,
    new SubstringComparator("testRowFilter"));
  assertTrue(rowFilter.areSerializedFieldsEqual(
    ProtobufUtil.toFilter(ProtobufUtil.toFilter(rowFilter))));
}
 
Example 14
Source File: TestFiltersWithBinaryComponentComparator.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void setRowFilters(FilterList filterList) {
  //offset for b as it is second component of "a+b+c+d"
  //'a' is at offset 0
  int bOffset = 4;
  byte[] b10 = Bytes.toBytes(10); //tests b > 10
  Filter b10Filter = new RowFilter(CompareOperator.GREATER,
          new BinaryComponentComparator(b10,bOffset));
  filterList.addFilter(b10Filter);

  byte[] b20  = Bytes.toBytes(20); //tests b < 20
  Filter b20Filter = new RowFilter(CompareOperator.LESS,
          new BinaryComponentComparator(b20,bOffset));
  filterList.addFilter(b20Filter);

  //offset for c as it is third component of "a+b+c+d"
  int cOffset = 8;
  byte[] c90  = Bytes.toBytes(90); //tests c > 90
  Filter c90Filter = new RowFilter(CompareOperator.GREATER,
          new BinaryComponentComparator(c90,cOffset));
  filterList.addFilter(c90Filter);

  byte[] c100  = Bytes.toBytes(100); //tests c < 100
  Filter c100Filter = new RowFilter(CompareOperator.LESS,
          new BinaryComponentComparator(c100,cOffset));
  filterList.addFilter(c100Filter);

  //offset for d as it is fourth component of "a+b+c+d"
  int dOffset = 12;
  byte[] d1   = Bytes.toBytes(1); //tests d == 1
  Filter dFilter  = new RowFilter(CompareOperator.EQUAL,
          new BinaryComponentComparator(d1,dOffset));

  filterList.addFilter(dFilter);

}
 
Example 15
Source File: ThriftUtilities.java    From hbase with 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 16
Source File: HBCK2.java    From hbase-operator-tools with Apache License 2.0 5 votes vote down vote up
int setRegionState(ClusterConnection connection, String region,
      RegionState.State newState)
    throws IOException {
  if (newState == null) {
    throw new IllegalArgumentException("State can't be null.");
  }
  RegionState.State currentState = null;
  Table table = connection.getTable(TableName.valueOf("hbase:meta"));
  RowFilter filter = new RowFilter(CompareOperator.EQUAL, new SubstringComparator(region));
  Scan scan = new Scan();
  scan.setFilter(filter);
  Result result = table.getScanner(scan).next();
  if (result != null) {
    byte[] currentStateValue = result.getValue(HConstants.CATALOG_FAMILY,
      HConstants.STATE_QUALIFIER);
    if (currentStateValue == null) {
      System.out.println("WARN: Region state info on meta was NULL");
    } else {
      currentState = RegionState.State.valueOf(
          org.apache.hadoop.hbase.util.Bytes.toString(currentStateValue));
    }
    Put put = new Put(result.getRow());
    put.addColumn(HConstants.CATALOG_FAMILY, HConstants.STATE_QUALIFIER,
      org.apache.hadoop.hbase.util.Bytes.toBytes(newState.name()));
    table.put(put);
    System.out.println("Changed region " + region + " STATE from "
      + currentState + " to " + newState);
    return EXIT_SUCCESS;
  } else {
    System.out.println("ERROR: Could not find region " + region + " in meta.");
  }
  return EXIT_FAILURE;
}
 
Example 17
Source File: TestSingleColumnValueFilter.java    From hbase with Apache License 2.0 4 votes vote down vote up
private Filter regexFilterNew() {
  return new SingleColumnValueFilter(COLUMN_FAMILY, COLUMN_QUALIFIER,
  CompareOperator.EQUAL,
    new RegexStringComparator(QUICK_REGEX));
}
 
Example 18
Source File: TestSingleColumnValueFilter.java    From hbase with Apache License 2.0 4 votes vote down vote up
private Filter regexFilterNew(Pattern pattern) {
  return new SingleColumnValueFilter(COLUMN_FAMILY, COLUMN_QUALIFIER,
  CompareOperator.EQUAL,
      new RegexStringComparator(pattern.pattern(), pattern.flags()));
}
 
Example 19
Source File: ThriftTable.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public CheckAndMutateBuilder ifNotExists() {
  this.op = CompareOperator.EQUAL;
  this.value = null;
  return this;
}
 
Example 20
Source File: TestFilter.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Test
public void testFilterListWithPrefixFilter() throws IOException {
  byte[] family = Bytes.toBytes("f1");
  byte[] qualifier = Bytes.toBytes("q1");
  TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
    new TableDescriptorBuilder.ModifyableTableDescriptor(
      TableName.valueOf(name.getMethodName()));

  tableDescriptor.setColumnFamily(
    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(family));
  RegionInfo info = RegionInfoBuilder.newBuilder(tableDescriptor.getTableName()).build();
  HRegion testRegion = HBaseTestingUtility.createRegionAndWAL(info, TEST_UTIL.getDataTestDir(),
    TEST_UTIL.getConfiguration(), tableDescriptor);

  for(int i=0; i<5; i++) {
    Put p = new Put(Bytes.toBytes((char)('a'+i) + "row"));
    p.setDurability(Durability.SKIP_WAL);
    p.addColumn(family, qualifier, Bytes.toBytes(String.valueOf(111 + i)));
    testRegion.put(p);
  }
  testRegion.flush(true);

  // rows starting with "b"
  PrefixFilter pf = new PrefixFilter(new byte[] {'b'}) ;
  // rows with value of column 'q1' set to '113'
  SingleColumnValueFilter scvf = new SingleColumnValueFilter(
      family, qualifier, CompareOperator.EQUAL, Bytes.toBytes("113"));
  // combine these two with OR in a FilterList
  FilterList filterList = new FilterList(Operator.MUST_PASS_ONE, pf, scvf);

  Scan s1 = new Scan();
  s1.setFilter(filterList);
  InternalScanner scanner = testRegion.getScanner(s1);
  List<Cell> results = new ArrayList<>();
  int resultCount = 0;
  while (scanner.next(results)) {
    resultCount++;
    byte[] row =  CellUtil.cloneRow(results.get(0));
    LOG.debug("Found row: " + Bytes.toStringBinary(row));
    assertTrue(Bytes.equals(row, Bytes.toBytes("brow"))
        || Bytes.equals(row, Bytes.toBytes("crow")));
    results.clear();
  }
  assertEquals(2, resultCount);
  scanner.close();

  WAL wal = ((HRegion)testRegion).getWAL();
  ((HRegion)testRegion).close();
  wal.close();
}