Search code examples
javahadoophbasebigdata

Fetch fixed rows with filters in Hbase using Java


In my application, I need to fetch the data from Hbase and I need to apply filters on that data and I need to have a limit on the number of records to be fetched. Below is the code that I have written:

ResultScanner scanner = null;
HTable table = null;
Configuration config = HBaseConfiguration.create();
            config.set("hbase.zookeeper.quorum", hbaseServer);
            config.set("hbase.zookeeper.property.clientPort", hbasePort);
        FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ALL);
        table = new HTable(config, "TableName");
SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("ColumnName"),
                    Bytes.toBytes("ColumnName"), CompareFilter.CompareOp.EQUAL,
                    new BinaryComparator(Bytes.toBytes(String.valueOf("FilterValue"))));
            SingleColumnValueFilter filter1 = new SingleColumnValueFilter(Bytes.toBytes("ColumnName2"),
                    Bytes.toBytes("ColumnName2"), CompareFilter.CompareOp.EQUAL,
                    new BinaryComparator(Bytes.toBytes(String.valueOf("FilterValue2"))));
            list.addFilter(filter);
            list.addFilter(filter1);
 Scan scan = new Scan();
 scan.setFilter(list);
 scan.setFilter(new PageFilter(10));
 scanner = table.getScanner(scan);

The filters are working perfectly fine but I am getting the complete dataset. scan.setFilter(new PageFilter(10)) is not working. I need to pull only top 10 records that are matching the filters provided. I have also tried:

scan.setMaxResultSize(10);
scan.setMaxResultsPerColumnFamily(10);

But none of these are also working. What could be the issue with the above code?


Solution

  • If you copied the code correctly, you rewrite your first filter:

    Scan scan = new Scan();
    scan.setFilter(list);
    scan.setFilter(new PageFilter(10));  <-- changes FilterList to PageFilter
    

    Seems that you wanted to do

    list.addFilter(new PageFilter(10));