Client source code modified based on version 0.94 supports HBase Paging

Source: Internet
Author: User

One scenario on the platform is to query historical order data of users and support paging.

Currently, hbase only supports rowkey primary indexes and does not support secondary indexes. In my previous blog, I proposed a solution based on SOLR + hbase,

The following is another scheme adopted by the rowkey Design Based on hbase. This scheme only depends on hbase, and the coupling is smaller.

Hbase is based on version 0.94. You need to modify the source code based on this version, mainly Org. apache. hadoop. hbase. in the client package, add the following paging methods:

// Start, the number of records starting from, for example, starting from 10th

// Nbrows indicates the page size, that is, the number of records to be retrieved.

Public result [] limit (INT start, int nbrows ){
Arraylist <result> resultlist = new arraylist <result> (nbrows );
For (INT I = 1; I <start & next ()! = NULL; I ++ );
Result next = NULL;
For (INT I = 0; I <nbrows & (next = next ())! = NULL; I ++ ){
Resultlist. Add (next );
}
Return resultList. toArray (new Result [resultList. size ()]);
}

The requirement scenario is that you can query order history data by Page Based on the date range and Other filtering conditions. The related pseudocode is:

HBase provides scan Based on the Rowkey range. For historical betting records, rowkey is designed to: rowkey = userNum $ orderDate $ field1 $ field2
Use the startrow and stoprow conditions of scan to determine the range and filter to define the filter interface: List scanerWithPage (int start, int pageSize, String userNum, String startDate, String endDate, String field1, String field2) specifically, start and pageSize are the starting record and the size of each page. Related pseudocode: FilterList lf = new FilterList ();
// This filter filters all query fields except userNum and orderDate, such as field1.
    RowFilter rowf = new RowFilter(CompareOp.EQUAL,new RegexStringComparator("([^$]+\\$){2}[2]"));     lf.addFilter(rowf);    scan.setFilter(lf);    scan.setCaching(100);    scan.setCacheBlocks(true);
// The Order List is displayed in descending order of the date in demand, while the order list is stored in ascending order of rowkey in HBase. To reduce unnecessary sorting, the date field is stored in (maxNum-orderDate), where maxNum takes a large long constant value of 1000000000, and orderDate is converted to long, in this way, HBase stores data in descending order of dates.
   scan.setStartRow((userNum+"$"+endDate).getBytes());    scan.setStopRow((userNum+"$"+startDate).getBytes());    scan.setBatch(100);    ClientScanner clscaner = null;    HTableInterface tbl = null;    tbl = table.getTable("order_his");    clscaner = (ClientScanner) tbl.getScanner(scan);    Result[] rets = clscaner.limit(start, pageSize);

This method is relatively simple. It fully utilizes the rowkey design principle of HBase, and the efficiency is very high when there are few query conditions.

Except usernum and orderdate, they are located based on the rowkey range. Other fields are scanned and filtered in this range. Therefore, do not use too many query conditions.

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.