/** * HbasePager.java * com.unicom.core * * Function: TODO * * ver date author * ────────────────────────────────── * Nov 1, 2013 qibaichao * * Copyright (c) 2013, TNT All Rights Reserved. */ /** * @Project: platform * @FileName HbasePager.java * @Author qibaichao * @Date Nov 1, 2013 */ package com.unicom.core; import java.net.URLEncoder; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.util.Bytes; import org.springframework.util.StringUtils; /** * @Author qibaichao * @ClassName HbasePager * @Date Nov 1, 2013 * @Description:hbase分頁對象 */ public class HbasePager { private Map<String, String> f = new HashMap<String, String>(); private Order o = new Order(); private List<Filter> filtersList = null; private String pageFlag = "pageFlag"; private List resultList = Collections.emptyList(); public int pageNo = 1; private int pageCount = 1; // 頁數 private int pageRows = 10; private Map<Integer, String> pageMap = new HashMap<Integer, String>(); // 下一頁:1 上一頁 :2 private int direction; // 是否有下一頁 private boolean hasNext = false; // 下一頁rowkey private String lastRow; private Scan scan = new Scan(); // 計算san @SuppressWarnings("unchecked") public Scan getScan(String rowkeyPre) { scan.setCaching(100); scan.setBatch(100); // 下一頁 if (direction == 1 && hasNext == true) { scan.setStartRow(Bytes.toBytes(lastRow)); scan.setStopRow(Bytes.toBytes(rowkeyPre + "~")); } else { if (this.pageMap.get(pageNo) != null) { scan.setStartRow(Bytes.toBytes(pageMap.get(pageNo))); scan.setStopRow(Bytes.toBytes(rowkeyPre + "~")); } else { scan.setStartRow(Bytes.toBytes(rowkeyPre)); scan.setStopRow(Bytes.toBytes(rowkeyPre + "~")); } } // 數據重置 hasNext = false; lastRow = null; return scan; } public String getPageFlag() { return pageFlag; } public void setPageFlag(String pageFlag) { this.pageFlag = pageFlag; } public List getResultList() { return resultList; } public void setResultList(List resultList) { this.resultList = resultList; } public int getPageNo() { return pageNo; } public void setPageNo(int pageNo) { this.pageNo = pageNo; } public int getPageRows() { return pageRows; } public void setPageRows(int pageRows) { this.pageRows = pageRows; } public boolean isHasNext() { return hasNext; } public void setHasNext(boolean hasNext) { this.hasNext = hasNext; } public String getLastRow() { return lastRow; } public void setLastRow(String lastRow) { this.lastRow = lastRow; } public int getDirection() { return direction; } public void setDirection(int direction) { this.direction = direction; } public Map<String, String> getF() { return f; } public void setF(Map<String, String> f) { this.f = f; } public Order getO() { return o; } public void setO(Order o) { this.o = o; } public List<Filter> getFiltersList() { return filtersList; } public void setFiltersList(List<Filter> filtersList) { this.filtersList = filtersList; } public Map<Integer, String> getPageMap() { return pageMap; } public void setPageMap(Map<Integer, String> pageMap) { this.pageMap = pageMap; } public Scan getScan() { return scan; } public void setScan(Scan scan) { this.scan = scan; } public int getPageCount() { return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; } private String url; public String getUrl() { if (url == null) { StringBuffer sbUrl = new StringBuffer(); // 查詢參數拼接 Set<Entry<String, String>> entrySet = this.f.entrySet(); for (Entry<String, String> entry : entrySet) { sbUrl.append("&").append("f[").append(entry.getKey()) .append("]").append("=") .append(URLEncoder.encode(entry.getValue())); } sbUrl.append("&hasNext").append("=").append(hasNext); sbUrl.append("&pageCount").append("=").append(pageCount); sbUrl.append("&lastRow").append("=").append(lastRow); Integer key = new Integer(0); String value = ""; if (pageMap != null && pageMap.size() != 0) { Set<Integer> keySet = pageMap.keySet(); for (Iterator<Integer> it = keySet.iterator(); it.hasNext();) { key = (Integer) it.next(); value = StringUtils.trimWhitespace(pageMap.get(key)); sbUrl.append("&pageMap[" + key + "]").append("=") .append(value); } } url = sbUrl.toString(); } return url; } private String fullUrl; public String getFullUrl() { if (fullUrl == null) { StringBuffer sbUrl = new StringBuffer(); if (o.getField() != null && o.getSort() != null) sbUrl.append("&o.field=") .append(URLEncoder.encode(o.getField())) .append("&o.sort=") .append(URLEncoder.encode(o.getSort())); fullUrl = getUrl() + sbUrl.toString(); } return fullUrl; } } //////////////////////////////第二步 分頁dao/////////////////////////////////////// /** * @Author qibaichao * @MethodName queryByCondition * @param condition * @return * @Date Oct 29, 2013 * @Description:用戶畫像-用戶終端查詢 */ @Override public HbasePager queryByCondition(HbasePager pager) throws Exception { StringBuffer rowkeyPre = new StringBuffer(); String mobile = pager.getF().get("mobile"); String provinceCode = pager.getF().get("provinceCode"); String cityCode = pager.getF().get("cityCode"); String tableName = Constant.CDR_DRAW_USER_INTERNET_USE_MONTH; // 拼接rowkey rowkeyPre.append(provinceCode + "|" + cityCode + "|"); if (StringUtils.isNotBlank(mobile)) { rowkeyPre.append(mobile); } HTable table = HbaseUtils.getHTable(tableName); // 表不存在 if (table == null) { return pager; } List<UserTerminalPo> poList = new ArrayList<UserTerminalPo>(); FilterList FilterList = new FilterList(); // 條件查詢 // Filter filter = null; // String startTime = ""; // String endTime = ""; // 根據查詢日期查詢 // if (StringUtils.isNotBlank(selectTime)) { // startTime = selectTime + " 00:00:00"; // endTime = selectTime + " 23:59:59"; // filter = new SingleColumnValueFilter(Bytes.toBytes("colfam"), // Bytes.toBytes("update_time"), CompareOp.GREATER_OR_EQUAL, // Bytes.toBytes(startTime)); // FilterList.addFilter(filter); // filter = new SingleColumnValueFilter(Bytes.toBytes("colfam"), // Bytes.toBytes("update_time"), CompareOp.LESS_OR_EQUAL, // Bytes.toBytes(endTime)); // FilterList.addFilter(filter); // } // log.info("用戶畫像-用戶終端查詢rowkey--->" + rowkeyPre + "startTime=" + // startTime // + "endTime=" + endTime); // 分頁查詢 Scan scan = pager.getScan(rowkeyPre.toString()); Filter pageFilter = new PageFilter(pager.getPageRows() + 1); FilterList.addFilter(pageFilter); scan.setFilter(FilterList); int totalRows = 0; ResultScanner rs = table.getScanner(scan); if (rs != null) { UserTerminalEntity entity = null; UserTerminalPo po = null; for (Result result : rs) { // 第一次查詢 totalRows++; if (totalRows == 1) { pager.getPageMap().put(pager.getPageNo(), Bytes.toString(result.getRow())); pager.setPageCount(pager.getPageMap().size()); } if (totalRows > pager.getPageRows()) { pager.setLastRow(Bytes.toString(result.getRow())); pager.setHasNext(true); } else { entity = this.buildEntity(result); po = this.buildPo(entity); poList.add(po); } } } pager.setResultList(poList); return pager; } //////////////////////////////第三步 分頁hbasePage.js///////////////////////////////////////////// /* 分頁js Example ---------------------- var pg = new showPages('pg'); pg.pageCount = 12; //定義總頁數(必要) pg.argName = 'p'; //定義參數名(可選,缺省爲page) pg.printHtml(); //顯示頁數 Supported in Internet Explorer, Mozilla Firefox */ function showPages(name) { //初始化屬性 this.name = name; //對象名稱 this.pageNo = 1; //當前頁數 this.pageCount = 1; //總頁數 this.argName = 'pageNo'; //參數名 this.hasNext=false; //是否有下一頁 this.fullUrl=""; } showPages.prototype.checkPages = function(){ //進行當前頁數和總頁數的驗證 if (isNaN(parseInt(this.pageNo))) this.pageNo = 1; if (isNaN(parseInt(this.pageCount))) this.pageCount = 1; if (this.pageNo < 1) this.pageNo = 1; if (this.pageCount < 1) this.pageCount = 1; if (this.pageNo > this.pageCount) this.pageNo = this.pageCount; this.pageNo = parseInt(this.pageNo); this.pageCount = parseInt(this.pageCount); } showPages.prototype.createHtml = function(){ //生成html代碼 var strHtml = ''; prevPage = this.pageNo - 1, nextPage = this.pageNo + 1; //(先後縮略,頁數,首頁,前頁,後頁,尾頁) //strHtml += '<span class="count">Pages: ' + this.pageNo + '</span>'; //strHtml += '<span class="number">'; if (prevPage < 1) { strHtml += '<li class="pgNext pgEmpty">上一頁</li>'; } else { strHtml += '<li class="pageNext"><a href="javascript:' + this.name + '.toPage(' + prevPage + ');">上一頁</a></li>'; } if (this.pageNo != 1) strHtml += '<li class="page-number"><a href="javascript:' + this.name + '.toPage(1);">[1]</a></li>'; if (this.pageNo >= 5) strHtml += '<li>...</li>'; if (this.pageCount > this.pageNo + 2) { var endPage = this.pageNo + 2; } else { var endPage = this.pageCount; } for (var i = this.pageNo - 2; i <= endPage; i++) { if (i > 0) { if (i == this.pageNo) { strHtml += '<li class="page-number pgCurrent">[' + i + ']</li>'; } else { if (i != 1 && i != this.pageCount) { strHtml += '<li class="page-number"><a href="javascript:' + this.name + '.toPage(' + i + ');">[' + i + ']</a></li>'; } } } } if (this.pageNo + 3 < this.pageCount) strHtml += '<li>...</li>'; if (this.pageNo != this.pageCount) strHtml += '<li class="page-number"><a href="javascript:' + this.name + '.toPage(' + this.pageCount + ');">[' + this.pageCount + ']</a></li>'; if (this.hasNext==false) { strHtml += '<li class="pgNext pgEmpty">下一頁</li>'; } else { strHtml += '<li class="pgNext"><a href="javascript:' + this.name + '.toNextPage(' + nextPage + ');">下一頁</a></li>'; } strHtml += '</li><br />'; return strHtml; } showPages.prototype.toPage = function(pageNo){ //頁面跳轉 //頁碼跳轉 self.location.href=this.fullUrl+"&pageNo="+pageNo; } //下一頁 showPages.prototype.toNextPage = function(pageNo){ //頁面跳轉 self.location.href=this.fullUrl +"&pageNo="+pageNo+"&direction=1"; } showPages.prototype.printHtml = function(mode){ //顯示html代碼 this.checkPages(); document.write('<div id="pages" class="pages"></div>'); document.getElementById('pages').innerHTML = this.createHtml(mode); } //////////////////////////////第四步頁面添加此處需引入 hbasePage.js////////////////////////////////////// <div class="page"> <div class="linle-pag fr"> <span id="pager"> <ul class="pages"> <script language="JavaScript"> var page = new showPages('page'); page.pageCount= ${pager.pageCount!}; page.pageNo=${pager.pageNo!}; //是否有下一頁 page.hasNext= ${pager.hasNext!?string('true','false')}; page.fullUrl="userTerminalList.j?"+"${pager.fullUrl}"; page.printHtml(); </script> </ul> </span> </div> </div>