/**
* 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>
转载于:https://my.oschina.net/u/569297/blog/188185