javaweb项目实战之hbase
这是我第一次写博客,不足之处希望多多谅解。
首先该实例是我学习完hbase之后的总结,都是对hbase的基本操作(即hbase的增、删、改、查);Web方面使用的是Jsp+servlet技术。由于主要目的在于Hbase的操作,因此web页面比较简单,没有设计酷炫的页面。
1.工程环境
实例的工程环境为:jdk1.7+tomcat6+hbase1.2.0。
Tomcat版本有点老了,但不影响使用,大家凑合一下吧。由于没有使用maven创建工程,现在将项目所用jar包也列举一下。这个应该是hbase开发的最小环境了。这些jar包绝大多数都可以在hbase的安装包的lib目录下找到。可以根据自己的实际环境进行替换。
项目采用经典的mvc模式进行编写。
2.登陆功能介绍:
该功能只是我用来复习web相关知识所用。没有将用户名和密码保存到数据库。
login.jsp
-------------------------------------------------------------------------------------
<body>
<div style="width: 300px; height: 400px">
<h1>登录</h1>
<hr />
<p style="font-weight: 900; color: red;">${msg }</p>
<form action="${pageContext.request.contextPath}/LoginServlet"
method="post">
用户名:<input type="text" name="username" value="${user.username }" /><br />
密 码:<input type="password" name="password" value="${user.password }" /><br />
<input type="submit" value="登录" />
</form>
</div>
</body>
LoginServlet.java
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username+":"+password);
if(username.equals("lhs") && password.equals("123")){
//response.sendRedirect("/Hbaseweb/welcom.jsp");
request.getRequestDispatcher("/index.jsp").forward(request, response);
}else{
request.setAttribute("msg", "用户名或密码错误");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
3.关于Hbase的操作
3.1列出hbase中所有的表和列簇
该功能的实现主要是通过hbase的Admin类的listTables()的相关方法实现。其中Servlet类对HttpServlet进行了封装,可以实现一个Servlet实现多个处理方法。
list.jsp
-------------------------------------------------------------------------
<body>
<h3 align="center">客户列表</h3>
<table border="1" width="70%" align="center">
<tr>
<th>序号</th>
<th>表名</th>
<th>列族名</th>
<th>操作</th>
</tr>
<c:forEach items="${requestScope.tables}" var="table">
<tr>
<td>${table.tableId }</td>
<td>${table.table }</td>
<td>${table.columnFamily }</td>
<td><a
href="<c:url value='/tableEdit.jsp?table=${table.table }&columnFamily=${table.columnFamily }'/>">编辑</a>
</td>
</tr>
</c:forEach>
</table>
</body>
HbaseServlet.java
----------------------------------------------------------------------------------
/**数据表查询
* @param request
* @param response
*/
public String findAllTable(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
List<HbaseBean> tables = hbaseService.findAllTable();
req.setAttribute("tables", tables);
return "f:/list.jsp";
}
HBaseService.java
------------------------------------------------------------------------------
public List<HbaseBean> findAllTable() throws IOException{
List<HbaseBean> tables = new ArrayList<HbaseBean>();
//调用userdao方法
HTableDescriptor[] result = hbaseDao.findAllTable();
for(int i=0;i<result.length;i++){
HbaseBean table = new HbaseBean();
//得到列族
Collection<HColumnDescriptor> families = result[i].getFamilies();
Iterator<HColumnDescriptor> iterator = families.iterator();
String name = null;
while(iterator.hasNext()){
HColumnDescriptor next = iterator.next();
name= next.getNameAsString();
}
table.setTableId(i+1);
table.setTable(result[i].getTableName().toString());
table.setColumnFamily(name);
tables.add(table);
}
return tables;
}
HBaseDao.java
----------------------------------------------------------------------------------
public HTableDescriptor[] findAllTable() throws IOException{
Connection conn = null;
HTableDescriptor[] tables=null;
try{
conn = this.connect();
Admin admin = conn.getAdmin();
//listTableNames = admin.listTableNames();
tables = admin.listTables();
}finally{
if(conn !=null){
conn.close();
}
}
return tables;
}
3.2查询某张表的所有记录
该功能的实现主要涉及Scan类的相关操作。我这里实现的是将一张表的所有记录查询出来在页面展示。展示的信息包括rowKey和各个列的最新值,没有展示时间戳和旧版本的值。为了简化功能没有做分页展示,有需要的可以查看别的博客。由于篇幅有限相关jsp页面的代码不再贴出,都是基本的form和table很容易实现的。
HbaseServlet.java
--------------------------------------------------------------------------------
/*
* 查询所有
*/
public String findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String table = request.getParameter("table");
List<TableBean> tabBeans = hbaseService.findAll(table);
// Iterator<TableBean> iterator = tabBeans.iterator();
// while(iterator.hasNext()){
// TableBean tableBean = iterator.next();
// System.out.println(tableBean.toString());
// }
System.out.println("findAll。。。。");
request.setAttribute("tabBeans", tabBeans);
return "f:/query.jsp";
}
/*
* 添加记录
*/
public void insert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
TableBean tBean = new TableBean();
tBean.setRowKey(request.getParameter("rowKey"));
tBean.setName(request.getParameter("name"));
tBean.setIncome(Integer.valueOf(request.getParameter("income")));
tBean.setExpense(Integer.valueOf(request.getParameter("expense")));
hbaseService.insert(tBean);
System.out.println("insert。。。。。。。。。。。。。。。。");
}
/*
* 更新记录
*/
public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
TableBean tBean = new TableBean();
tBean.setRowKey(request.getParameter("rowKey"));
tBean.setName(request.getParameter("name"));
tBean.setIncome(Integer.valueOf(request.getParameter("income")));
tBean.setExpense(Integer.valueOf(request.getParameter("expense")));
hbaseService.updatePre(tBean);
System.out.println("update。。。。。。。。。。。。。。。。");
}
/*
* 删除记录
*/
public String delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String rowKey = request.getParameter("rowKey");
hbaseService.delete(rowKey);
System.out.println("delete。。。。。。。。。。。。。。。。");
return "f:/success.jsp";
}
/*
*条件查询
*/
public String select(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String rowKey = request.getParameter("rowKey");
//System.out.println("rowKey:"+rowKey+".............");
String name = request.getParameter("name");
String income = request.getParameter("income");
String expense = request.getParameter("expense");
List<TableBean> tabBeans = hbaseService.select(rowKey,name,income,expense);
System.out.println("select。。。。。。。。。。。。。。。。");
request.setAttribute("tabBeans", tabBeans);
return "f:/query.jsp";
}
HBaseService.java
------------------------------------------------------------------------------------
public List<TableBean> findAll(String table) throws IOException {
return hbaseDao.find(table);
}
public void insert(TableBean tBean) {
hbaseDao.insert(tBean);
}
public void delete(String rowKey) throws IOException {
hbaseDao.delete(rowKey);
}
public void updatePre(TableBean tBean) {
hbaseDao.insert(tBean);
}
public List<TableBean> select(String rowKey, String name, String income, String expense) throws IOException {
List<TableBean> tabBeans = null;
System.out.println(rowKey);
if(rowKey == null || rowKey == ""){
tabBeans=hbaseDao.select(name,income,expense);
}else{
tabBeans= hbaseDao.selectRowKey(rowKey);
}
return tabBeans;
}
HBaseDao.java
------------------------------------------------------------------------------
public List<TableBean> find(String table) throws IOException {
Connection conn = null;
Table tableDes = null;
ResultScanner rs = null;
List<TableBean> tables = new ArrayList<TableBean>();
try{
conn = this.connect();
Scan scan = new Scan();
tableDes = conn.getTable(TableName.valueOf(table));
rs = tableDes.getScanner(scan);
System.out.println(rs.toString());
Iterator<Result> iterator = rs.iterator();
while(iterator.hasNext()) {
TableBean tabBean = new TableBean();
Result result = iterator.next();
String rowId = Bytes.toString(result.getRow());
Cell cName = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("name"));
Cell cIncome = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("income"));
Cell cExpense = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("expense"));
String name = Bytes.toString(CellUtil.cloneValue(cName));
int income = Bytes.toInt(CellUtil.cloneValue(cIncome));
int expense = Bytes.toInt(CellUtil.cloneValue(cExpense));
tabBean.setRowKey(rowId);
tabBean.setName(name);
tabBean.setIncome(income);
tabBean.setExpense(expense);
tables.add(tabBean);
// System.out.println(tabBean.toString());
}
}catch (Exception e) {
}finally{
if(rs != null){
rs.close();
}
if(tableDes !=null){
tableDes.close();
}
if(conn !=null){
conn.close();
}
}
return tables;
}
public void insert(TableBean tBean) {
//使用的try...with结构,用来替换try...catch...finally
try(Connection conn = this.connect();
Table tableDes =conn.getTable(TableName.valueOf("lhs_mymoney")) ){
Put put = new Put(Bytes.toBytes(tBean.getRowKey()));
HTableDescriptor htab = tableDes.getTableDescriptor();
HColumnDescriptor[] columnFamilies = htab.getColumnFamilies();
String famName = columnFamilies[0].getNameAsString();
put.addColumn(Bytes.toBytes(famName), Bytes.toBytes("name"), Bytes.toBytes(tBean.getName()))
.addColumn(Bytes.toBytes(famName), Bytes.toBytes("income"), Bytes.toBytes(tBean.getIncome()))
.addColumn(Bytes.toBytes(famName), Bytes.toBytes("expense"), Bytes.toBytes(tBean.getExpense()));
//将修改更新到表
tableDes.put(put);
}catch(Exception e){
}
}
public void delete(String rowKey) {
try(Connection conn = this.connect();
Table tableDes =conn.getTable(TableName.valueOf("lhs_mymoney")) ){
Delete delete = new Delete(Bytes.toBytes(rowKey));
tableDes.delete(delete);
}catch(Exception e){
}
}
/*
Get get = new Get(Bytes.toBytes(rowKey));
//get.setMaxVersions(10);
Table table =con.getTable(TableName.valueOf(tableName));
return table.get(get);
*/
public List<TableBean> selectRowKey(String rowKey) {
List<TableBean> tabBeans = new ArrayList<TableBean>();
TableBean tabBean = new TableBean();
try(Connection conn = this.connect();
Table tableDes =conn.getTable(TableName.valueOf("lhs_mymoney")) ){
Get get = new Get(Bytes.toBytes(rowKey));
Result result = tableDes.get(get);
String rowId = Bytes.toString(result.getRow());
Cell cName = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("name"));
Cell cIncome = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("income"));
Cell cExpense = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("expense"));
String name = Bytes.toString(CellUtil.cloneValue(cName));
int income = Bytes.toInt(CellUtil.cloneValue(cIncome));
int expense = Bytes.toInt(CellUtil.cloneValue(cExpense));
tabBean.setRowKey(rowId);
tabBean.setName(name);
tabBean.setIncome(income);
tabBean.setExpense(expense);
tabBeans.add(tabBean);
}catch(Exception e){
}
return tabBeans;
}
public List<TableBean> select(String name, String income, String expense) throws IOException {
List<Filter> filters = new ArrayList<Filter>();
List<TableBean> tabBeans = new ArrayList<TableBean>();
Filter nameFilter = null;
Filter incomeFilter = null;
Filter expenseFilter = null;
if(name != "" || name != null){
//设置name查询过滤条件
nameFilter = new SingleColumnValueFilter(
Bytes.toBytes("info"), Bytes.toBytes("name"), CompareOp.EQUAL, new SubstringComparator(name));
filters.add(nameFilter);
}
//!income.trim().equals("")
if(!(income == null || income == "")){
System.out.println(income);
int incomeInt = Integer.valueOf(income);
incomeFilter = new SingleColumnValueFilter(
Bytes.toBytes("info"), Bytes.toBytes("income"), CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(incomeInt)));
filters.add(incomeFilter);
}
if(expense != "" && expense != null){
Integer expenseInt = Integer.valueOf(expense);
Filter filter = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("expense"),
CompareOp.GREATER_OR_EQUAL, new BinaryComparator(Bytes.toBytes(10000)));
// expenseFilter = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("expense"),
// CompareOp.GREATER_OR_EQUAL, new BinaryComparator(Bytes.toBytes(10000)));
filters.add(filter);
}
//创建FilterList
FilterList filterList = new FilterList(Operator.MUST_PASS_ONE, filters);
//FilterList filterList = new FilterList(filters);
Scan scan = new Scan();
scan.setFilter(filterList);
try(Connection conn = this.connect();
Table tableDes =conn.getTable(TableName.valueOf("lhs_mymoney"));
ResultScanner rs = tableDes.getScanner(scan);){
Iterator<Result> iterator = rs.iterator();
while(iterator.hasNext()) {
TableBean tabBean = new TableBean();
Result result = iterator.next();
String rowId = Bytes.toString(result.getRow());
Cell cName = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("name"));
Cell cIncome = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("income"));
Cell cExpense = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("expense"));
if(cName == null || cIncome ==null || cExpense==null){
break;
}
String nameQuery = Bytes.toString(CellUtil.cloneValue(cName));
int incomeQuery = Bytes.toInt(CellUtil.cloneValue(cIncome));
int expenseQuery = Bytes.toInt(CellUtil.cloneValue(cExpense));
tabBean.setRowKey(rowId);
tabBean.setName(nameQuery);
tabBean.setIncome(incomeQuery);
tabBean.setExpense(expenseQuery);
tabBeans.add(tabBean);
// System.out.println(tabBean.toString());
}
}
return tabBeans;
}
完整源码可以查看下面连接:
https://download.****.net/download/qq_33347071/10921821