hbase分頁(yè)查詢實(shí)現(xiàn)

Hbase本身是沒(méi)有分頁(yè)查詢的,我在網(wǎng)上找了很多資料來(lái)實(shí)現(xiàn)一個(gè)分頁(yè)功能,在這里做了一下記錄,分享給大家,有什么不足之處,請(qǐng)盡管指出。廢話不多說(shuō),看代碼。

創(chuàng)新互聯(lián)公司是一家專(zhuān)注于網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站與策劃設(shè)計(jì),關(guān)嶺網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專(zhuān)注于網(wǎng)站建設(shè)10年,網(wǎng)設(shè)計(jì)領(lǐng)域的專(zhuān)業(yè)建站公司;建站業(yè)務(wù)涵蓋:關(guān)嶺等地區(qū)。關(guān)嶺做網(wǎng)站價(jià)格咨詢:028-86922220

import java.io.IOException;

import java.util.LinkedHashMap;

import java.util.LinkedList;

import java.util.List;

import java.util.Map;

 

import org.apache.commons.lang.StringUtils;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.HBaseConfiguration;

import org.apache.hadoop.hbase.client.Get;

import org.apache.hadoop.hbase.client.HTableInterface;

import org.apache.hadoop.hbase.client.HTablePool;

import org.apache.hadoop.hbase.client.Result;

import org.apache.hadoop.hbase.client.ResultScanner;

import org.apache.hadoop.hbase.client.Scan;

import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;

import org.apache.hadoop.hbase.filter.Filter;

import org.apache.hadoop.hbase.filter.FilterList;

import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter;

import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;

import org.apache.hadoop.hbase.util.Bytes;

 

publicclass HBaseUtils {

   privatestatic Configurationconfig =null;

   privatestatic HTablePooltp =null;

   static {

       //加載集群配置

       config = HBaseConfiguration.create();

       config.set("hbase.zookeeper.quorum","xx.xx.xx");

       config.set("hbase.zookeeper.property.clientPort","2181");

       //創(chuàng)建表池(可偉略提高查詢性能,具體說(shuō)明請(qǐng)百度或官方API)

       tp =new HTablePool(config, 10);

    }

 

   /*

     *獲取hbase的表

     */

   publicstatic HTableInterface getTable(StringtableName) {

 

       if (StringUtils.isEmpty(tableName))

           returnnull;

 

       returntp.getTable(getBytes(tableName));

    }

 

   /*轉(zhuǎn)換byte數(shù)組 */

   publicstaticbyte[] getBytes(String str) {

       if (str ==null)

            str="";

 

       return Bytes.toBytes(str);

    }

 

   /**

     *查詢數(shù)據(jù)

     *@param tableKey表標(biāo)識(shí)

     *@param queryKey查詢標(biāo)識(shí)

     *@param startRow開(kāi)始行

     *@param paramsMap參數(shù)集合

     *@return結(jié)果集

     */

   publicstatic TBData getDataMap(StringtableName, String startRow,

            StringstopRow, Integer currentPage, Integer pageSize)

           throws IOException {

        List<Map<String, String>>mapList =null;

        mapList =new LinkedList<Map<String,String>>();

 

        ResultScanner scanner =null;

       //為分頁(yè)創(chuàng)建的封裝類(lèi)對(duì)象,下面有給出具體屬性

        TBData tbData =null;

       try {

           //獲取最大返回結(jié)果數(shù)量

           if (pageSize ==null || pageSize == 0L)

                pageSize = 100;

 

           if (currentPage ==null || currentPage == 0)

                currentPage = 1;

 

           //計(jì)算起始頁(yè)和結(jié)束頁(yè)

            IntegerfirstPage = (currentPage - 1) * pageSize;

 

            IntegerendPage = firstPage + pageSize;

 

           //從表池中取出HBASE表對(duì)象

            HTableInterfacetable = getTable(tableName);

           //獲取篩選對(duì)象

            Scanscan = getScan(startRow, stopRow);

           //給篩選對(duì)象放入過(guò)濾器(true標(biāo)識(shí)分頁(yè),具體方法在下面)

            scan.setFilter(packageFilters(true));

           //緩存1000條數(shù)據(jù)

            scan.setCaching(1000);

            scan.setCacheBlocks(false);

            scanner= table.getScanner(scan);

           int i = 0;

            List<byte[]> rowList =new LinkedList<byte[]>();

           //遍歷掃描器對(duì)象, 并將需要查詢出來(lái)的數(shù)據(jù)row key取出

           for (Result result : scanner) {

                String row = toStr(result.getRow());

               if (i >= firstPage && i< endPage) {

                    rowList.add(getBytes(row));

                }

                i++;

            }

 

           //獲取取出的row key的GET對(duì)象

            List<Get>getList = getList(rowList);

            Result[]results = table.get(getList);

           //遍歷結(jié)果

           for (Result result : results) {

                Map<byte[],byte[]> fmap = packFamilyMap(result);

                Map<String, String> rmap = packRowMap(fmap);

                mapList.add(rmap);

            }

 

           //封裝分頁(yè)對(duì)象

            tbData=new TBData();

            tbData.setCurrentPage(currentPage);

            tbData.setPageSize(pageSize);

            tbData.setTotalCount(i);

            tbData.setTotalPage(getTotalPage(pageSize, i));

            tbData.setResultList(mapList);

        }catch (IOException e) {

            e.printStackTrace();

        }finally {

            closeScanner(scanner);

        }

 

       return tbData;

    }

 

   privatestaticint getTotalPage(int pageSize,int totalCount) {

       int n = totalCount / pageSize;

       if (totalCount % pageSize == 0) {

           return n;

        }else {

           return ((int) n) + 1;

        }

    }

 

   //獲取掃描器對(duì)象

   privatestatic Scan getScan(String startRow,String stopRow) {

        Scan scan =new Scan();

        scan.setStartRow(getBytes(startRow));

        scan.setStopRow(getBytes(stopRow));

 

       return scan;

    }

 

   /**

     *封裝查詢條件

     */

   privatestatic FilterList packageFilters(boolean isPage) {

        FilterList filterList =null;

       // MUST_PASS_ALL(條件 AND) MUST_PASS_ONE(條件OR)

        filterList =new FilterList(FilterList.Operator.MUST_PASS_ALL);

        Filter filter1 =null;

        Filter filter2 =null;

        filter1 = newFilter(getBytes("family1"), getBytes("column1"),

                CompareOp.EQUAL, getBytes("condition1"));

        filter2 = newFilter(getBytes("family2"), getBytes("column1"),

                CompareOp.LESS, getBytes("condition2"));

        filterList.addFilter(filter1);

        filterList.addFilter(filter2);

       if (isPage) {

            filterList.addFilter(new FirstKeyOnlyFilter());

        }

       return filterList;

    }

 

   privatestatic Filter newFilter(byte[] f,byte[] c, CompareOp op,byte[] v) {

       returnnew SingleColumnValueFilter(f, c, op,v);

    }

 

   privatestaticvoid closeScanner(ResultScannerscanner) {

       if (scanner !=null)

            scanner.close();

    }

 

   /**

     *封裝每行數(shù)據(jù)

     */

   privatestatic Map<String, String>packRowMap(Map<byte[],byte[]> dataMap) {

        Map<String, String> map =new LinkedHashMap<String, String>();

 

       for (byte[] key : dataMap.keySet()) {

 

           byte[] value = dataMap.get(key);

 

            map.put(toStr(key), toStr(value));

 

        }

       return map;

    }

 

   /*根據(jù)ROW KEY集合獲取GET對(duì)象集合 */

   privatestatic List<Get> getList(List<byte[]> rowList) {

        List<Get> list =new LinkedList<Get>();

       for (byte[] row : rowList) {

            Getget =new Get(row);

 

            get.addColumn(getBytes("family1"), getBytes("column1"));

            get.addColumn(getBytes("family1"), getBytes("column2"));

            get.addColumn(getBytes("family2"), getBytes("column1"));

            list.add(get);

        }

       return list;

    }

 

   /**

     *封裝配置的所有字段列族

     */

   privatestatic Map<byte[],byte[]> packFamilyMap(Result result){

        Map<byte[],byte[]> dataMap =null;

        dataMap =new LinkedHashMap<byte[],byte[]>();

        dataMap.putAll(result.getFamilyMap(getBytes("family1")));

        dataMap.putAll(result.getFamilyMap(getBytes("family2")));

       return dataMap;

    }

 

   privatestatic String toStr(byte[] bt) {

       return Bytes.toString(bt);

    }

 

   publicstaticvoid main(String[] args)throws IOException {

       //拿出row key的起始行和結(jié)束行

       // #<0<9<:

        String startRow ="aaaa#";

        String stopRow ="aaaa:";

       int currentPage = 1;

       int pageSize = 20;

       //執(zhí)行hbase查詢

        getDataMap("table", startRow, stopRow, currentPage,pageSize);

 

    }

}

 

class TBData {

   private IntegercurrentPage;

   private IntegerpageSize;

   private IntegertotalCount;

   private IntegertotalPage;

   private List<Map<String, String>>resultList;

 

   public Integer getCurrentPage() {

       returncurrentPage;

    }

 

   publicvoid setCurrentPage(IntegercurrentPage) {

       this.currentPage = currentPage;

    }

 

   public Integer getPageSize() {

       returnpageSize;

    }

 

   publicvoid setPageSize(Integer pageSize) {

       this.pageSize = pageSize;

    }

 

   public Integer getTotalCount() {

       returntotalCount;

    }

 

   publicvoid setTotalCount(Integer totalCount){

       this.totalCount = totalCount;

    }

 

   public Integer getTotalPage() {

       returntotalPage;

    }

 

   publicvoid setTotalPage(Integer totalPage) {

       this.totalPage = totalPage;

    }

 

   public List<Map<String, String>> getResultList() {

       returnresultList;

    }

 

   publicvoidsetResultList(List<Map<String, String>> resultList) {

       this.resultList = resultList;

    }

}

網(wǎng)頁(yè)名稱(chēng):hbase分頁(yè)查詢實(shí)現(xiàn)
本文路徑:http://bm7419.com/article6/geigig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開(kāi)發(fā)微信小程序、電子商務(wù)、動(dòng)態(tài)網(wǎng)站、軟件開(kāi)發(fā)、網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁(yè)設(shè)計(jì)公司