Java Code Examples for org.apache.hadoop.hbase.client.Scan.setFilter()

The following are Jave code examples for showing how to use setFilter() of the org.apache.hadoop.hbase.client.Scan class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: ditb   File: TestTableInputFormat.java   Source Code and License Vote up 7 votes
@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(CompareOp.EQUAL, new RegexStringComparator("aa.*"));
    scan.setFilter(exampleFilter);
    setScan(scan);
  } catch (IOException exception) {
    throw new RuntimeException("Failed to initialize.", exception);
  }
}
 
Example 2
Project: SparkDemo   File: MyClass.java   Source Code and License Vote up 6 votes
public static void QueryByCondition2(String tableName) {

        try {
            HTablePool pool = new HTablePool(configuration, 1000);
            HTable table = (HTable) pool.getTable(tableName);
            Filter filter = new SingleColumnValueFilter(Bytes
                    .toBytes("column1"), null, CompareOp.EQUAL, Bytes
                    .toBytes("aaa")); // 当列column1的值为aaa时进行查询
            Scan s = new Scan();
            s.setFilter(filter);
            ResultScanner rs = table.getScanner(s);
            for (Result r : rs) {
                System.out.println("获得到rowkey:" + new String(r.getRow()));
                for (KeyValue keyValue : r.raw()) {
                    System.out.println("列:" + new String(keyValue.getFamily())
                            + "====值:" + new String(keyValue.getValue()));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
 
Example 3
Project: ditb   File: TestFilter.java   Source Code and License Vote up 6 votes
/**
 * Tests the the {@link WhileMatchFilter} works in combination with a
 * {@link Filter} that uses the
 * {@link Filter#filterKeyValue(org.apache.hadoop.hbase.KeyValue)} method.
 *
 * See HBASE-2258.
 *
 * @throws Exception
 */
@Test
public void testWhileMatchFilterWithFilterKeyValue() throws Exception {
  Scan s = new Scan();
  WhileMatchFilter filter = new WhileMatchFilter(
      new SingleColumnValueFilter(FAMILIES[0], QUALIFIERS_ONE[0], CompareOp.EQUAL, Bytes.toBytes("foo"))
  );
  s.setFilter(filter);

  InternalScanner scanner = this.region.getScanner(s);
  while (true) {
    ArrayList<Cell> values = new ArrayList<Cell>();
    boolean isMoreResults = scanner.next(values);
    assertTrue("The WhileMatchFilter should now filter all remaining", filter.filterAllRemaining());
    if (!isMoreResults) {
      break;
    }
  }
}
 
Example 4
Project: ditb   File: TestMultiRowRangeFilter.java   Source Code and License Vote up 6 votes
@Test
public void testMultiRowRangeFilterWithoutRangeOverlap() throws IOException {
  tableName = Bytes.toBytes("testMultiRowRangeFilterWithoutRangeOverlap");
  HTable ht = TEST_UTIL.createTable(tableName, family, Integer.MAX_VALUE);
  generateRows(numRows, ht, family, qf, value);

  Scan scan = new Scan();
  scan.setMaxVersions();

  List<RowRange> ranges = new ArrayList<RowRange>();
  ranges.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(40), false));
  ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(20), false));
  ranges.add(new RowRange(Bytes.toBytes(60), true, Bytes.toBytes(70), false));

  MultiRowRangeFilter filter = new MultiRowRangeFilter(ranges);
  scan.setFilter(filter);
  int resultsSize = getResultsSize(ht, scan);
  LOG.info("found " + resultsSize + " results");
  List<Cell> results1 = getScanResult(Bytes.toBytes(10), Bytes.toBytes(20), ht);
  List<Cell> results2 = getScanResult(Bytes.toBytes(30), Bytes.toBytes(40), ht);
  List<Cell> results3 = getScanResult(Bytes.toBytes(60), Bytes.toBytes(70), ht);

  assertEquals(results1.size() + results2.size() + results3.size(), resultsSize);

  ht.close();
}
 
Example 5
Project: mumu-hbase   File: HBaseFilterOperation.java   Source Code and License Vote up 6 votes
/**
 * 参考列过滤器(获取相同时间戳的列)
 *
 * @param tableName    表名
 * @param columnFamily 列族
 * @param qualifier    列限定符
 * @param columnValue  列值
 * @param count        数量
 */
public void dependentColumnFilter(String tableName, String columnFamily, String qualifier, String columnValue, int count) {
    HBaseConfiguration hBaseConfiguration = new HBaseConfiguration();
    Table table = hBaseConfiguration.table(tableName);
    Scan scan = new Scan();
    scan.setFilter(new PrefixFilter(Bytes.toBytes("")));
    scan.setCaching(10);
    scan.setBatch(10);
    try {
        ResultScanner scanner = table.getScanner(scan);
        Result[] results = scanner.next(count);
        HBaseResultUtil.print(results);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 6
Project: mumu-hbase   File: HBaseFilterOperation.java   Source Code and License Vote up 6 votes
/**
 * 单列排除过滤器(返回的列 不包含参考列)
 *
 * @param tableName    表名
 * @param columnFamily 列族
 * @param qualifier    列限定符
 * @param columnValue  列值
 * @param count        数量
 */
public void SingleColumnValueExcludeFilter(String tableName, String columnFamily, String qualifier, String columnValue, int count) {
    HBaseConfiguration hBaseConfiguration = new HBaseConfiguration();
    Table table = hBaseConfiguration.table(tableName);
    Scan scan = new Scan();
    SingleColumnValueExcludeFilter singleColumnValueFilter = new SingleColumnValueExcludeFilter(Bytes.toBytes(columnFamily), Bytes.toBytes(qualifier), CompareFilter.CompareOp.EQUAL, Bytes.toBytes(columnValue));
    //singleColumnValueFilter.setFilterIfMissing(true);//当不存在这列的行 默认不过滤
    singleColumnValueFilter.setLatestVersionOnly(true);//获取最新版本
    scan.setFilter(singleColumnValueFilter);
    scan.setCaching(10);
    //scan.setBatch(10);
    try {
        ResultScanner scanner = table.getScanner(scan);
        Result[] results = scanner.next(count);
        HBaseResultUtil.print(results);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 7
Project: ditb   File: TestMultiRowRangeFilter.java   Source Code and License Vote up 6 votes
@Test
public void testMultiRowRangeFilterWithEmptyStopRow() throws IOException {
  tableName = Bytes.toBytes("testMultiRowRangeFilterWithEmptyStopRow");
  HTable ht = TEST_UTIL.createTable(tableName, family, Integer.MAX_VALUE);
  generateRows(numRows, ht, family, qf, value);
  Scan scan = new Scan();
  scan.setMaxVersions();

  List<RowRange> ranges = new ArrayList<RowRange>();
  ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(""), false));
  ranges.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(40), false));

  MultiRowRangeFilter filter = new MultiRowRangeFilter(ranges);
  scan.setFilter(filter);
  int resultsSize = getResultsSize(ht, scan);
  List<Cell> results1 = getScanResult(Bytes.toBytes(10), Bytes.toBytes(""), ht);
  assertEquals(results1.size(), resultsSize);

  ht.close();
}
 
Example 8
Project: ditb   File: ScanRange.java   Source Code and License Vote up 5 votes
public static Scan getScan(String fileName) throws IOException {
  BufferedReader br = new BufferedReader(new FileReader(fileName));
  String line;
  Scan scan = new Scan();
  System.out.println("winter for scan ******");
  FilterList filterList = new FilterList();
  while ((line = br.readLine()) != null) {
    System.out.println("winter for scan : " + line);
    if (line.startsWith("#")) continue;
    // family, qualifier, type, >=, 10, <=, 1000
    // family, qualifier, type, >=, 10
    String[] splits = line.split("\t");
    byte[] family = Bytes.toBytes(splits[0]);
    byte[] qualifier = Bytes.toBytes(splits[1]);
    DataType type = DataType.valueOf(splits[2].toUpperCase());
    CompareFilter.CompareOp firstOp = parseOp(splits[3]);
    byte[] firstValue = DataType.stringToBytes(type, splits[4]);
    filterList.addFilter(new SingleColumnValueFilter(family, qualifier, firstOp, firstValue));
    if (splits.length >= 6) {
      CompareFilter.CompareOp secondOp = parseOp(splits[5].toUpperCase());
      byte[] secondValue = DataType.stringToBytes(type, splits[6]);
      filterList
          .addFilter(new SingleColumnValueFilter(family, qualifier, secondOp, secondValue));
    }
  }
  scan.setFilter(filterList);
  ScanRangeList scanRangeList = ScanRangeList.getScanRangeList(fileName);
  if (scanRangeList.getRanges().size() > 0) {
    scan.setAttribute(ScanRange.SCAN_RANGE_ATTRIBUTE_STR, scanRangeList.toBytesAttribute());
  }
  return scan;
}
 
Example 9
Project: ditb   File: PerformanceEvaluation.java   Source Code and License Vote up 5 votes
@Override
void testRow(final int i) throws IOException {
  Scan scan = new Scan(getRandomRow(this.rand, this.totalRows));
  scan.addColumn(FAMILY_NAME, QUALIFIER_NAME);
  scan.setFilter(new WhileMatchFilter(new PageFilter(120)));
  ResultScanner s = this.table.getScanner(scan);
  s.close();
}
 
Example 10
Project: ditb   File: TestSCVFWithMiniCluster.java   Source Code and License Vote up 5 votes
/**
 * Test the filter by adding 1 column of family A (the one used in the filter) and 1 column of
 * family B in the scan. (OK)
 */
@Test
public void scanWithSpecificQualifiers2() throws IOException {
  /* When */
  Scan scan = new Scan();
  scan.addColumn(FAMILY_A, QUALIFIER_FOO);
  scan.addColumn(FAMILY_B, QUALIFIER_BAR);
  scan.setFilter(scanFilter);

  verify(scan);
}
 
Example 11
Project: ditb   File: TestBigDecimalColumnInterpreter.java   Source Code and License Vote up 5 votes
@Test (timeout=300000)
public void testMinWithFilter() throws Throwable {
  AggregationClient aClient = new AggregationClient(conf);
  Scan scan = new Scan();
  scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
  Filter f = new PrefixFilter(Bytes.toBytes("foo:bar"));
  scan.setFilter(f);
  final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
    new BigDecimalColumnInterpreter();
  BigDecimal min = null;
  min = aClient.min(TEST_TABLE, ci, scan);
  assertEquals(null, min);
}
 
Example 12
Project: ditb   File: PerformanceEvaluation.java   Source Code and License Vote up 5 votes
protected Scan constructScan(byte[] valuePrefix) throws IOException {
  Filter filter = new SingleColumnValueFilter(
      FAMILY_NAME, QUALIFIER_NAME, CompareFilter.CompareOp.EQUAL,
      new BinaryComparator(valuePrefix)
  );
  Scan scan = new Scan();
  scan.addColumn(FAMILY_NAME, QUALIFIER_NAME);
  scan.setFilter(filter);
  return scan;
}
 
Example 13
Project: ditb   File: TestBigDecimalColumnInterpreter.java   Source Code and License Vote up 5 votes
@Test (timeout=300000)
public void testAvgWithFilter() throws Throwable {
  AggregationClient aClient = new AggregationClient(conf);
  Scan scan = new Scan();
  scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
  Filter f = new PrefixFilter(Bytes.toBytes("foo:bar"));
  scan.setFilter(f);
  final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
    new BigDecimalColumnInterpreter();
  Double avg = null;
  avg = aClient.avg(TEST_TABLE, ci, scan);
  assertEquals(Double.NaN, avg, 0);
}
 
Example 14
Project: ditb   File: TestDoubleColumnInterpreter.java   Source Code and License Vote up 5 votes
@Test(timeout = 300000)
public void testSumWithFilter() throws Throwable {
  AggregationClient aClient = new AggregationClient(conf);
  Filter f = new PrefixFilter(Bytes.toBytes("foo:bar"));
  Scan scan = new Scan();
  scan.addFamily(TEST_FAMILY);
  scan.setFilter(f);
  final ColumnInterpreter<Double, Double, EmptyMsg, DoubleMsg, DoubleMsg> ci = 
      new DoubleColumnInterpreter();
  Double sum = null;
  sum = aClient.sum(TEST_TABLE, ci, scan);
  assertEquals(null, sum);
}
 
Example 15
Project: ditb   File: DefaultVisibilityLabelServiceImpl.java   Source Code and License Vote up 5 votes
@Override
public List<String> getUserAuths(byte[] user, boolean systemCall)
    throws IOException {
  assert (labelsRegion != null || systemCall);
  if (systemCall || labelsRegion == null) {
    return this.labelsCache.getUserAuths(Bytes.toString(user));
  }
  Scan s = new Scan();
  if (user != null && user.length > 0) {
    s.addColumn(LABELS_TABLE_FAMILY, user);
  }
  Filter filter = VisibilityUtils.createVisibilityLabelFilter(this.labelsRegion,
    new Authorizations(SYSTEM_LABEL));
  s.setFilter(filter);
  ArrayList<String> auths = new ArrayList<String>();
  RegionScanner scanner = this.labelsRegion.getScanner(s);
  try {
    List<Cell> results = new ArrayList<Cell>(1);
    while (true) {
      scanner.next(results);
      if (results.isEmpty()) break;
      Cell cell = results.get(0);
      int ordinal = Bytes.toInt(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
      String label = this.labelsCache.getLabel(ordinal);
      if (label != null) {
        auths.add(label);
      }
      results.clear();
    }
  } finally {
    scanner.close();
  }
  return auths;
}
 
Example 16
Project: ditb   File: HRegion.java   Source Code and License Vote up 5 votes
/**
 * create scan on memstore
 */
private Scan createScanOnMemstore(Scan rawScan, ScanRange.ScanRangeList allRangeList)
    throws IOException {
  Scan scan = new Scan(rawScan);
  scan.setCacheBlocks(false);
  scan.setFilter(allRangeList.toFilterList());
  return scan;
}
 
Example 17
Project: ditb   File: TestScannersWithFilters.java   Source Code and License Vote up 5 votes
@Test
public void testPrefixFilter() throws Exception {
  // Grab rows from group one (half of total)
  long expectedRows = numRows / 2;
  long expectedKeys = colsPerRow;
  Scan s = new Scan();
  s.setFilter(new PrefixFilter(Bytes.toBytes("testRowOne")));
  verifyScan(s, expectedRows, expectedKeys);
}
 
Example 18
Project: ditb   File: TestBigDecimalColumnInterpreter.java   Source Code and License Vote up 5 votes
@Test (timeout=300000)
public void testStdWithFilter() throws Throwable {
  AggregationClient aClient = new AggregationClient(conf);
  Filter f = new PrefixFilter(Bytes.toBytes("foo:bar"));
  Scan scan = new Scan();
  scan.addFamily(TEST_FAMILY);
  scan.setFilter(f);
  final ColumnInterpreter<BigDecimal, BigDecimal, EmptyMsg, BigDecimalMsg, BigDecimalMsg> ci =
      new BigDecimalColumnInterpreter();
  Double std = null;
  std = aClient.std(TEST_TABLE, ci, scan);
  assertEquals(Double.NaN, std, 0);
}
 
Example 19
Project: ditb   File: TestFilter.java   Source Code and License Vote up 5 votes
/**
 * The following test is to ensure old(such as hbase0.94) filterRow() can be correctly fired in 
 * 0.96+ code base.  
 * 
 * See HBASE-10366
 * 
 * @throws Exception
 */
@Test
public void tes94FilterRowCompatibility() throws Exception {
  Scan s = new Scan();
  OldTestFilter filter = new OldTestFilter();
  s.setFilter(filter);

  InternalScanner scanner = this.region.getScanner(s);
  ArrayList<Cell> values = new ArrayList<Cell>();
  scanner.next(values);
  assertTrue("All rows should be filtered out", values.isEmpty());
}
 
Example 20
Project: ditb   File: AggregateImplementation.java   Source Code and License Vote up 4 votes
/**
 * Gives the row count for the given column family and column qualifier, in
 * the given row range as defined in the Scan object.
 */
@Override
public void getRowNum(RpcController controller, AggregateRequest request,
    RpcCallback<AggregateResponse> done) {
  AggregateResponse response = null;
  long counter = 0l;
  List<Cell> results = new ArrayList<Cell>();
  InternalScanner scanner = null;
  try {
    Scan scan = ProtobufUtil.toScan(request.getScan());
    byte[][] colFamilies = scan.getFamilies();
    byte[] colFamily = colFamilies != null ? colFamilies[0] : null;
    NavigableSet<byte[]> qualifiers = colFamilies != null ?
        scan.getFamilyMap().get(colFamily) : null;
    byte[] qualifier = null;
    if (qualifiers != null && !qualifiers.isEmpty()) {
      qualifier = qualifiers.pollFirst();
    }
    if (scan.getFilter() == null && qualifier == null)
      scan.setFilter(new FirstKeyOnlyFilter());
    scanner = env.getRegion().getScanner(scan);
    boolean hasMoreRows = false;
    do {
      hasMoreRows = scanner.next(results);
      if (results.size() > 0) {
        counter++;
      }
      results.clear();
    } while (hasMoreRows);
    ByteBuffer bb = ByteBuffer.allocate(8).putLong(counter);
    bb.rewind();
    response = AggregateResponse.newBuilder().addFirstPart( 
        ByteString.copyFrom(bb)).build();
  } catch (IOException e) {
    ResponseConverter.setControllerException(controller, e);
  } finally {
    if (scanner != null) {
      try {
        scanner.close();
      } catch (IOException ignored) {}
    }
  }
  log.info("Row counter from this region is "
      + env.getRegion().getRegionInfo().getRegionNameAsString() + ": " + counter);
  done.run(response);
}