hbase分页功能的实现及源码

由于最近刚接触 hadoop 这么高端的东西,一开始搞的我也有点头晕,研究了一段时间,现在脑子稍微清醒点了,把自己实现的功能来跟大家分享一下吧

写的可能有点乱,各位就凑合看吧,第一次在csdn上写博客,具体的格式还不会捣鼓,各位见谅,有什么看的不明白的可以加我Q Q 695438455 上班时间一般都在线,

用到的资料和源码我会整理一份放到 百度网盘 

http://pan.baidu.com/s/1eRrXU

先说一下用到的技术吧

1、struts2

2、hbase(集群环境就不说了)

3、easyui(前台展示)


实现功能

hbase分页功能,并在jsp页面显示


大家应该都知道,hbase的主要功能不是干这活的,这样基本上就又回到关系型数据库了,就当是 自己没事干瞎玩研究一下吧,我现在在写供别人调用的接口,光在控制台输出也

没啥意思,所有就想在jsp页面显示一下,于是就搞了一下,显示很好弄,我在hbase里面弄了一万条数据,第一次直接把浏览器 干崩溃了,然后想得弄个分页,从网上搜了下都

说hbase没有分页功能,后来在度娘的帮助下找到了一篇关于hbase分页实现的文章,最棒的是还有源码哈哈,拿过来不能直接用,自己有根据现有的环境大修了一下,最终终于

把这骨头搞定了。


import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
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.KeyValue;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
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.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.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.util.Bytes;
import org.json.JSONArray;


import test.creattable.TBData;


import com.core.action.BaseAction;
import com.core.bean.DataGridJson;
import com.google.gson.JsonArray;


public class Users extends BaseAction<Model> {
private static Configuration config = null;
private static HTablePool tp = null;
static {
// 加载集群配置
config = HBaseConfiguration.create();
config.set("hbase.master", "10.158.7.190:60000");
config.set("hbase.zookeeper.quorum", "10.158.7.190");
config.set("hbase.zookeeper.property.clientPort", "2181");


// 创建表池(可伟略提高查询性能,具体说明请百度或官方API)
tp = new HTablePool(config, 10);
}
private String rowkey;// 开始


public Users() {
super(Model.class);
}


public String execute() {
return SUCCESS;
}


public String getAllData() throws IOException {// 查询所有数据
PrintWriter out = getResponse().getWriter();
dgdata = new DataGridJson();//基于easyui分页功能的公用类

ResultScanner scanner = null;
// 为分页创建的封装类对象,下面有给出具体属性

// page(第几页)、rows(每页多少行)这两个参数是页面传给后台的
System.out.println("rows:" + rows + " " + "page:" + page);


// 获取最大返回结果数量
if (rows == null || rows == 0L) {
rows = 20;
}
if (page == null || page == 0L) {
page = 1;
}
// 计算起始页和结束页
Integer firstPage = (page - 1) * rows;
System.out.println("fiestPage:" + firstPage);


Integer endPage = firstPage + rows;
System.out.println("endPage:" + endPage);
// 从表池中取出HBASE表对象
HTableInterface table = getTable("users");
// 获取筛选对象

Scan scan = getScan();
// 给筛选对象放入过滤器(true标识分页,具体方法在下面)
scan.setFilter(packageFilters(true));
// 缓存1000条数据
scan.setCaching(1000);
scan.setCacheBlocks(false);
scanner = table.getScanner(scan);
int i = 0;
List<byte[]> rowList = new LinkedList<byte[]>();
// 遍历扫描器对象, 并将需要查询出来的数据row key取出
List list1 = new ArrayList();
if (rowkey == null || rowkey.equals("")) {
for (Result result : scanner) {
String row = toStr(result.getRow());


if (i >= firstPage && i < endPage) {
// System.out.println("row; " + row);
for (KeyValue kv : result.raw()){

//Model 是我自己创建的,由于struts2基础类操作比较方便,于是我就把 hbase 里面的rowkey 、列族、列名和列值 创建了一个Model,代码在下面 
Model m = new Model();
m.setRowkey(new String(kv.getRow()));// 主键rowkey
m.setInfo(Bytes.toString(kv.getFamily()));// 列族
m.setLieming(new String(kv.getQualifier()));// 列名
m.setLiezhi(Bytes.toString(kv.getValue()));// 列值
System.out.println("lieming:" + new String(kv.getRow()));
list1.add(m);
}
rowList.add(getBytes(row));
}
i++;
// System.out.println("i:"+rowList.size());
}
} else {//单点值查询
Get g = new Get(Bytes.toBytes(rowkey));
// HTableInterface t = getTable("users");
Result r = table.get(g);
// System.out.println(r.size());


if (r.size() <= 0) {
System.out.println("点名称不存在");
out.print("0");
} else {
for (KeyValue kv : r.raw()) {
Model m = new Model();
m.setRowkey(new String(kv.getRow()));// 主键rowkey
m.setInfo(Bytes.toString(kv.getFamily()));// 列族
m.setLieming(new String(kv.getQualifier()));// 列名
m.setLiezhi(Bytes.toString(kv.getValue()));// 列值
// System.out.println("lieming:" + new
// String(kv.getRow()));
list1.add(m);
i++;
}
}
}

//下面的这两句是我基于easyui 的分页功能 生成的一个返回json的公用类,封装了一些分页必须使用的参数
// long total = i;//总记录数
dgdata.setRows(list1);// 每页的行数
dgdata.setTotal(i);// 用来显示总的记录数


return SUCCESS;
}



/*
 * 获取hbase的表
 */
public static HTableInterface getTable(String tableName) {


if (StringUtils.isEmpty(tableName))
return null;


return tp.getTable(getBytes(tableName));
}


/* 转换byte数组 */
public static byte[] getBytes(String str) {
if (str == null)
str = "";


return Bytes.toBytes(str);
}







private static Scan getScan() {
Scan scan = new Scan();
// scan.setStartRow(getBytes(startRow));
// scan.setStopRow(getBytes(stopRow));
return scan;
}


private static void closeScanner(ResultScanner scanner) {
if (scanner != null)
scanner.close();
}



private static String toStr(byte[] bt) {
return Bytes.toString(bt);
}




public String getRowkey() {
return rowkey;
}


public void setRowkey(String rowkey) {
this.rowkey = rowkey;
}
}

Model  类 


public class Model {

private String rowkey;//主键rowkey
private String info;//列族
private String lieming;//列
private String liezhi;//值

public String getRowkey() {
return rowkey;
}
public void setRowkey(String rowkey) {
this.rowkey = rowkey;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getLieming() {
return lieming;
}
public void setLieming(String lieming) {
this.lieming = lieming;
}
public String getLiezhi() {
return liezhi;
}
public void setLiezhi(String liezhi) {
this.liezhi = liezhi;
}

}


DataGridJson 类


public class DataGridJson implements Serializable{
private long total=0;
private List rows=new ArrayList();

public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public List getRows() {
return rows;
}
public void setRows(List rows) {
this.rows = rows;
}

}

前台页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/base/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>${systemOption.systemTitle}</title>
    <%@ include file="/base/head.jsp"%>
    <script type="text/javascript" charset="UTF-8">

 function formatRQ(val,row){
  if(val!=null){
return val.replace('T',' ');
}
 }
 
  //分页查询
  function doQuery(){
   var queryParams=$('#jbxx').datagrid('options').queryParams;
   queryParams.rowkey=$('#rowkey').val();
   //重置当前页数为1
   resetDG('#jbxx');
  } 




    </script>
  </head>
  
  <body>
 
<!-- 主窗口 -->    
    <table id="jbxx" class="easyui-datagrid" data-options="fit:true,
     rownumbers:true,
singleSelect:true,
striped:true,
fitColumns:true,
striped:true,
toolbar:'#tb',
pagination:true,
pageSize:20,
pageList:[20,30,50,100],
url:'${ctx}/json/hbase.action'">

<thead>
<tr>
<th data-options="field:'rowkey',align:'center'" width="50"><strong>rowkey</strong></th>
<th data-options="field:'info',align:'center'" width="180"><strong>列族</strong></th>
<th data-options="field:'lieming',align:'center'" width="60"><strong>列名称</strong></th>
<th data-options="field:'liezhi',align:'center'" width="180" ><strong>列值</strong></th>
</tr>
</thead>

</table>
<!-- Datagrid工具栏 -->
<div id="tb">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
   <td class="toolDiv">  
   <a href="javascript:void(0)" οnclick="add_jbxx()" class="easyui-linkbutton" data-options="iconCls:'icon-user-add',plain:true">登记新用户</a><span class="vline">|</span>
<a href="javascript:void(0)" οnclick="edit_jbxx()" class="easyui-linkbutton" data-options="iconCls:'icon-user-edit',plain:true">修改用户</a><span class="vline">|</span>
</td>
</tr>
<tr>
   <td class="serchDiv">
rowkey:<!--  <input id="rowkey" class="easyui-validatebox" type="text">-->

<input id="rowkey" name="rowkey" class="easyui-validatebox"   validType="remote['${ctx}/json/checkRowkey.action','rowkey']" invalidMessage="点名称不存在" />

&nbsp;&nbsp;
    <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" οnclick="doQuery()">查询</a>
</td>
</tr>
</table>
</div>
  </body>
</html>


struts2 配置文件

<action name="hbase" class="com.core.haction.Users" method="getAllData">
<result name="success" type="json">
<param name="root">dgdata</param>
</result>
</action>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值