Java Code Examples for org.apache.hadoop.hbase.filter.FamilyFilter

The following examples show how to use org.apache.hadoop.hbase.filter.FamilyFilter. 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: java-docs-samples   Source File: Filters.java    License: Apache License 2.0 5 votes vote down vote up
public static void filterLimitColFamilyRegex(
    String projectId, String instanceId, String tableId) {
  // A filter that matches cells whose column family satisfies the given regex
  Filter filter = new FamilyFilter(CompareOp.EQUAL, new RegexStringComparator("stats_.*$"));
  Scan scan = new Scan().setFilter(filter);
  readWithFilter(projectId, instanceId, tableId, scan);
}
 
Example 2
Source Project: java-docs-samples   Source File: Filters.java    License: Apache License 2.0 5 votes vote down vote up
public static void filterComposingChain(String projectId, String instanceId, String tableId) {
  // A filter that selects one cell per row AND within the column family cell_plan
  Filter familyFilter =
      new FamilyFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("cell_plan")));
  Filter columnCountGetFilter = new ColumnCountGetFilter(3);

  FilterList filter = new FilterList(FilterList.Operator.MUST_PASS_ALL);
  filter.addFilter(columnCountGetFilter);
  filter.addFilter(familyFilter);
  Scan scan = new Scan().setFilter(filter);
  readWithFilter(projectId, instanceId, tableId, scan);
}
 
Example 3
Source Project: hbase   Source File: TestAsyncTable.java    License: Apache License 2.0 5 votes vote down vote up
@Test
@Deprecated
public void testCheckAndMutateWithTimestampFilterForOldApi() throws Throwable {
  AsyncTable<?> table = getTable.get();

  // Put with specifying the timestamp
  table.put(new Put(row).addColumn(FAMILY, Bytes.toBytes("A"), 100, Bytes.toBytes("a"))).get();

  // Put with success
  boolean ok = table.checkAndMutate(row, new FilterList(
      new FamilyFilter(CompareOperator.EQUAL, new BinaryComparator(FAMILY)),
      new QualifierFilter(CompareOperator.EQUAL, new BinaryComparator(Bytes.toBytes("A"))),
      new TimestampsFilter(Collections.singletonList(100L))
    ))
    .thenPut(new Put(row).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b")))
    .get();
  assertTrue(ok);

  Result result = table.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("B"))).get();
  assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));

  // Put with failure
  ok = table.checkAndMutate(row, new FilterList(
      new FamilyFilter(CompareOperator.EQUAL, new BinaryComparator(FAMILY)),
      new QualifierFilter(CompareOperator.EQUAL, new BinaryComparator(Bytes.toBytes("A"))),
      new TimestampsFilter(Collections.singletonList(101L))
    ))
    .thenPut(new Put(row).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c")))
    .get();
  assertFalse(ok);

  assertFalse(table.exists(new Get(row).addColumn(FAMILY, Bytes.toBytes("C"))).get());
}
 
Example 4
Source Project: hbase   Source File: TestAsyncTable.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCheckAndMutateWithTimestampFilter() throws Throwable {
  AsyncTable<?> table = getTable.get();

  // Put with specifying the timestamp
  table.put(new Put(row).addColumn(FAMILY, Bytes.toBytes("A"), 100, Bytes.toBytes("a"))).get();

  // Put with success
  boolean ok = table.checkAndMutate(CheckAndMutate.newBuilder(row)
    .ifMatches(new FilterList(
      new FamilyFilter(CompareOperator.EQUAL, new BinaryComparator(FAMILY)),
      new QualifierFilter(CompareOperator.EQUAL, new BinaryComparator(Bytes.toBytes("A"))),
      new TimestampsFilter(Collections.singletonList(100L))))
    .build(new Put(row).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b")))).get();
  assertTrue(ok);

  Result result = table.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("B"))).get();
  assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));

  // Put with failure
  ok = table.checkAndMutate(CheckAndMutate.newBuilder(row)
    .ifMatches(new FilterList(
      new FamilyFilter(CompareOperator.EQUAL, new BinaryComparator(FAMILY)),
      new QualifierFilter(CompareOperator.EQUAL, new BinaryComparator(Bytes.toBytes("A"))),
      new TimestampsFilter(Collections.singletonList(101L))))
    .build(new Put(row).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c")))).get();
  assertFalse(ok);

  assertFalse(table.exists(new Get(row).addColumn(FAMILY, Bytes.toBytes("C"))).get());
}
 
Example 5
Source Project: hbase   Source File: TestCheckAndMutate.java    License: Apache License 2.0 5 votes vote down vote up
@Test
@Deprecated
public void testCheckAndMutateWithTimestampFilterForOldApi() throws Throwable {
  try (Table table = createTable()) {
    // Put with specifying the timestamp
    table.put(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"), 100, Bytes.toBytes("a")));

    // Put with success
    boolean ok = table.checkAndMutate(ROWKEY, new FilterList(
        new FamilyFilter(CompareOperator.EQUAL, new BinaryComparator(FAMILY)),
        new QualifierFilter(CompareOperator.EQUAL, new BinaryComparator(Bytes.toBytes("A"))),
        new TimestampsFilter(Collections.singletonList(100L))
      ))
      .thenPut(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b")));
    assertTrue(ok);

    Result result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B")));
    assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));

    // Put with failure
    ok = table.checkAndMutate(ROWKEY, new FilterList(
        new FamilyFilter(CompareOperator.EQUAL, new BinaryComparator(FAMILY)),
        new QualifierFilter(CompareOperator.EQUAL, new BinaryComparator(Bytes.toBytes("A"))),
        new TimestampsFilter(Collections.singletonList(101L))
      ))
      .thenPut(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c")));
    assertFalse(ok);

    assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"))));
  }
}
 
Example 6
Source Project: hbase   Source File: TestCheckAndMutate.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCheckAndMutateWithTimestampFilter() throws Throwable {
  try (Table table = createTable()) {
    // Put with specifying the timestamp
    table.put(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"), 100, Bytes.toBytes("a")));

    // Put with success
    boolean ok = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
      .ifMatches(new FilterList(
        new FamilyFilter(CompareOperator.EQUAL, new BinaryComparator(FAMILY)),
        new QualifierFilter(CompareOperator.EQUAL, new BinaryComparator(Bytes.toBytes("A"))),
        new TimestampsFilter(Collections.singletonList(100L))))
      .build(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b"))));
    assertTrue(ok);

    Result result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B")));
    assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));

    // Put with failure
    ok = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
      .ifMatches(new FilterList(
        new FamilyFilter(CompareOperator.EQUAL, new BinaryComparator(FAMILY)),
        new QualifierFilter(CompareOperator.EQUAL, new BinaryComparator(Bytes.toBytes("A"))),
        new TimestampsFilter(Collections.singletonList(101L))))
      .build(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c"))));
    assertFalse(ok);

    assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"))));
  }
}
 
Example 7
Source Project: spork   Source File: TestHBaseStorageFiltering.java    License: 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 8
Source Project: spork   Source File: TestHBaseStorageFiltering.java    License: 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 9
void addFilterByMapping( FilterList fl, CompareFilter.CompareOp comp, Class<?> comparatorClass, Object comparator,
                         Mapping.TupleMapping tupleMapping )
  throws NoSuchMethodException, InstantiationException, IllegalAccessException,
  java.lang.reflect.InvocationTargetException {
  switch ( tupleMapping ) {
    case KEY: {
      addFilter( RowFilter.class, fl, comp, comparatorClass, comparator );
      return;
    }
    case FAMILY: {
      addFilter( FamilyFilter.class, fl, comp, comparatorClass, comparator );
      return;
    }
    case COLUMN: {
      //TODO Check if ColumnPrefixFilter works faster and suit more

      addFilter( QualifierFilter.class, fl, comp, comparatorClass, comparator );
      return;
    }
    case VALUE: {
      addFilter( ValueFilter.class, fl, comp, comparatorClass, comparator );
      return;
    }
    case TIMESTAMP: {
      addFilter( TimestampsFilter.class, fl, comp, comparatorClass, comparator );
      //        Constructor<TimestampsFilter> columnFilterConstructor =
      //          TimestampsFilter.class.getConstructor( CompareFilter.CompareOp.class, comparatorClass );
      //        TimestampsFilter scf = columnFilterConstructor.newInstance( comp, comparator );
      //        fl.addFilter( scf );
      return;
    }
  }
}
 
Example 10
Source Project: phoenix-omid   Source File: TestSnapshotFilter.java    License: Apache License 2.0 4 votes vote down vote up
@Test(timeOut = 60_000)
public void testGetWithFamilyDelete() throws Throwable {
    byte[] rowName1 = Bytes.toBytes("row1");
    byte[] famName1 = Bytes.toBytes(TEST_FAMILY);
    byte[] famName2 = Bytes.toBytes("test-fam2");
    byte[] colName1 = Bytes.toBytes("col1");
    byte[] colName2 = Bytes.toBytes("col2");
    byte[] dataValue1 = Bytes.toBytes("testWrite-1");

    String TEST_TABLE = "testGetWithFamilyDelete";
    createTableIfNotExists(TEST_TABLE, Bytes.toBytes(TEST_FAMILY), famName2);

    TTable tt = new TTable(connection, TEST_TABLE);

    Transaction tx1 = tm.begin();

    Put put1 = new Put(rowName1);
    put1.addColumn(famName1, colName1, dataValue1);
    tt.put(tx1, put1);

    tm.commit(tx1);

    Transaction tx2 = tm.begin();
    Put put2 = new Put(rowName1);
    put2.addColumn(famName2, colName2, dataValue1);
    tt.put(tx2, put2);
    tm.commit(tx2);

    Transaction tx3 = tm.begin();

    Delete d = new Delete(rowName1);
    d.addFamily(famName2);
    tt.delete(tx3, d);


    Transaction tx4 = tm.begin();

    Get get = new Get(rowName1);

    Filter filter1 = new FilterList(FilterList.Operator.MUST_PASS_ONE,
            new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(TEST_FAMILY))),
            new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(famName2)));

    get.setFilter(filter1);
    Result result = tt.get(tx4, get);
    assertTrue(result.size() == 2, "Result should be 2");

    try {
        tm.commit(tx3);
    } catch (RollbackException e) {
        if (!tm.isLowLatency())
            fail();
    }
    Transaction tx5 = tm.begin();
    result = tt.get(tx5, get);
    if (!tm.isLowLatency())
        assertTrue(result.size() == 1, "Result should be 1");

    tt.close();
}
 
Example 11
Source Project: phoenix-omid   Source File: TestSnapshotFilter.java    License: Apache License 2.0 4 votes vote down vote up
@Test(timeOut = 60_000)
public void testGetWithFilter() throws Throwable {
    byte[] rowName1 = Bytes.toBytes("row1");
    byte[] famName1 = Bytes.toBytes(TEST_FAMILY);
    byte[] famName2 = Bytes.toBytes("test-fam2");
    byte[] colName1 = Bytes.toBytes("col1");
    byte[] colName2 = Bytes.toBytes("col2");
    byte[] dataValue1 = Bytes.toBytes("testWrite-1");

    String TEST_TABLE = "testGetWithFilter";
    createTableIfNotExists(TEST_TABLE, Bytes.toBytes(TEST_FAMILY), famName2);
    TTable tt = new TTable(connection, TEST_TABLE);

    Transaction tx1 = tm.begin();

    Put put1 = new Put(rowName1);
    put1.addColumn(famName1, colName1, dataValue1);
    tt.put(tx1, put1);

    tm.commit(tx1);

    Transaction tx2 = tm.begin();
    Put put2 = new Put(rowName1);
    put2.addColumn(famName2, colName2, dataValue1);
    tt.put(tx2, put2);
    tm.commit(tx2);

    Transaction tx3 = tm.begin();

    Get get = new Get(rowName1);

    Filter filter1 = new FilterList(FilterList.Operator.MUST_PASS_ONE,
            new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(TEST_FAMILY))),
            new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(famName2)));

    get.setFilter(filter1);
    Result result = tt.get(tx3, get);
    assertTrue(result.size() == 2, "Result should be 2");


    Filter filter2 = new FilterList(FilterList.Operator.MUST_PASS_ONE,
            new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(TEST_FAMILY))));

    get.setFilter(filter2);
    result = tt.get(tx3, get);
    assertTrue(result.size() == 1, "Result should be 2");

    tm.commit(tx3);

    tt.close();
}
 
Example 12
Source Project: phoenix-omid   Source File: TestSnapshotFilter.java    License: Apache License 2.0 4 votes vote down vote up
@Test(timeOut = 60_000)
public void testScanWithFilter() throws Throwable {

    byte[] rowName1 = Bytes.toBytes("row1");
    byte[] famName1 = Bytes.toBytes(TEST_FAMILY);
    byte[] famName2 = Bytes.toBytes("test-fam2");
    byte[] colName1 = Bytes.toBytes("col1");
    byte[] colName2 = Bytes.toBytes("col2");
    byte[] dataValue1 = Bytes.toBytes("testWrite-1");

    String TEST_TABLE = "testScanWithFilter";
    createTableIfNotExists(TEST_TABLE, famName1, famName2);
    TTable tt = new TTable(connection, TEST_TABLE);

    Transaction tx1 = tm.begin();
    Put put1 = new Put(rowName1);
    put1.addColumn(famName1, colName1, dataValue1);
    tt.put(tx1, put1);
    tm.commit(tx1);

    Transaction tx2 = tm.begin();
    Put put2 = new Put(rowName1);
    put2.addColumn(famName2, colName2, dataValue1);
    tt.put(tx2, put2);

    tm.commit(tx2);
    Transaction tx3 = tm.begin();

    Scan scan = new Scan();
    scan.setFilter(new FilterList(FilterList.Operator.MUST_PASS_ONE,
            new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(TEST_FAMILY)))));
    scan.setStartRow(rowName1).setStopRow(rowName1);

    ResultScanner iterableRS = tt.getScanner(tx3, scan);
    Result result = iterableRS.next();
    assertTrue(result.containsColumn(famName1, colName1));
    assertFalse(result.containsColumn(famName2, colName2));

    scan.setFilter(new FilterList(FilterList.Operator.MUST_PASS_ONE,
            new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(TEST_FAMILY))),
            new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(famName2))));

    iterableRS = tt.getScanner(tx3, scan);
    result = iterableRS.next();
    assertTrue(result.containsColumn(famName1, colName1));
    assertTrue(result.containsColumn(famName2, colName2));

    tm.commit(tx3);
    tt.close();
}
 
Example 13
Source Project: spork   Source File: TestHBaseStorageFiltering.java    License: Apache License 2.0 4 votes vote down vote up
private FamilyFilter assertFamilyFilter(Filter filter, CompareFilter.CompareOp compareOp, String value) {
    FamilyFilter familyFilter = assertFamilyFilter(filter, compareOp);
    assertEquals("Unexpected value", value, Bytes.toString(familyFilter.getComparator().getValue()));
    return familyFilter;
}