jfinal+hbase+eclipse开发web项目详细步骤04---在web页面实现对hbase数据库数据的增删查改功能
首先提醒大家,本节是在步骤01、步骤02、步骤03都成功的基础上做进一步开发。如果在之前的任何一个步骤出现问题,那么希望你先解决好问题之后,再做本次的开发。步骤1:建表。1、打开我们虚拟机,并且启动hadoop、hbasestart-all.shstart-hbase.sh2、进入hbase shell 界面输入:hbase shell3、创建表并添加数据输...
首先提醒大家,本节是在步骤01、步骤02、步骤03都成功的基础上做进一步开发。如果在之前的任何一个步骤出现问题,那么希望你先解决好问题之后,再做本次的开发。
步骤1:建表。
1、打开我们虚拟机,并且启动hadoop、hbase
start-all.sh
start-hbase.sh
2、进入hbase shell 界面
输入:hbase shell
3、创建表并添加数据
输入: create 'student','base_info'
再输入:put 'student',1,'base_info:no','20181011'
再输入:put 'student',1,'base_info:cls','16'
再输入:put 'student',1,'base_info:name','xiaoming'
步骤2:修改HelloContorller类,Hbase类
全部复制之前的代码,代码如下:
HelloContorller.java
package com.demo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
import com.jfinal.core.Controller;
import com.plugin.Hbase;
public class HelloController extends Controller {
/**
* 进入主页面
*/
public void index() {
render("index.html");
}
/**
* 进入添加页面
*/
public void add() {
render("add.html");
}
/**
* 进入添加页面
*/
public void openupdate() {
//获取页面数据
String rowname=getPara("rowname");
Map<String, Object> resMap = new HashMap<String, Object>();
//获取数据库的数据
try {
resMap=Hbase.get("student", rowname);
} catch (Exception e) {
e.printStackTrace();
}
setAttr("info", resMap);
renderJsp("update.jsp");
}
public void indexData() {
ArrayList<Student> studnets = new ArrayList<>();
Student s1 = new Student();
s1.setNo("01");
s1.setCls("16计本1班");
s1.setName("小米");
Student s2 = new Student();
s2.setNo("02");
s2.setCls("16计本1班");
s2.setName("小花");
Student s3 = new Student();
s3.setNo("01");
s3.setCls("16计本2班");
s3.setName("旺旺");
studnets.add(s1);
studnets.add(s2);
studnets.add(s3);
setAttr("infos", studnets);
renderJson();
}
public void hbase() {
Map<String, Object> resMap = new HashMap<String, Object>();
try {
resMap = Hbase.get("stu", "2");
} catch (Exception e) {
e.printStackTrace();
}
renderText(resMap.toString());
}
// 查询student表信息
public void scanStudent() {
List<Map<String, Object>> studnets = new ArrayList<Map<String, Object>>();
try {
studnets = Hbase.scan("student");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setAttr("infos", studnets);
renderJson();
}
// 保存数据
public void save() {
// 获取页面数据
String rowname = getPara("rowname");
String no = getPara("no");
String name = getPara("name");
String cls = getPara("cls");
List<Put> puts = new ArrayList<Put>();
Put put1 = new Put(Bytes.toBytes(rowname));
put1.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("no"), Bytes.toBytes(no));
Put put2 = new Put(Bytes.toBytes(rowname));
put2.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("name"), Bytes.toBytes(name));
Put put3 = new Put(Bytes.toBytes(rowname));
put3.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("cls"), Bytes.toBytes(cls));
puts.add(put1);
puts.add(put2);
puts.add(put3);
// 保存数据
try {
Hbase.puts(puts, "student");
} catch (Exception e) {
e.printStackTrace();
}
render("index.html");
}
/**
* 删除数据
*/
public void delete() {
// 获取页面数据
String rowname = getPara("rowname");
try {
Hbase.deleteRow("student", rowname);
} catch (IOException e) {
e.printStackTrace();
}
render("index.html");
}
public void update() {
}
}
Hbase.java
package com.plugin;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.Put;
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.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
public class Hbase {
static Connection connection;
//新建表
public static boolean create(String tableName, String columnFamily) throws Exception {
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
if (admin.tableExists(tableName)) {
System.out.println(tableName + " exists!");
return false;
} else {
String[] columnFamilyArray = columnFamily.split(",");
HColumnDescriptor[] hColumnDescriptor = new HColumnDescriptor[columnFamilyArray.length];
for (int i = 0; i < hColumnDescriptor.length; i++) {
hColumnDescriptor[i] = new HColumnDescriptor(columnFamilyArray[i]);
}
HTableDescriptor familyDesc = new HTableDescriptor(TableName.valueOf(tableName));
for (HColumnDescriptor columnDescriptor : hColumnDescriptor) {
familyDesc.addFamily(columnDescriptor);
}
HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tableName), familyDesc);
admin.createTable(tableDesc);
System.out.println(tableName + " create successfully!");
return true;
}
}
//插入数据
public static boolean put(String tablename, String row, String columnFamily, String qualifier, String data)
throws Exception {
Table table = connection.getTable(TableName.valueOf(tablename));
Put put = new Put(Bytes.toBytes(row));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(qualifier), Bytes.toBytes(data));
table.put(put);
System.out.println("put '" + row + "', '" + columnFamily + ":" + qualifier + "', '" + data + "'");
return true;
}
//插入多条数据
public static boolean puts(List<Put> puts,String tablename)
throws Exception {
Table table = connection.getTable(TableName.valueOf(tablename));
table.put(puts);
return true;
}
//把result转换成map,方便返回json数据
private static Map<String, Object> resultToMap(Result result) {
Map<String, Object> resMap = new HashMap<String, Object>();
List<Cell> listCell = result.listCells();
Map<String, Object> tempMap = new HashMap<String, Object>();
String rowname = "";
List<String> familynamelist = new ArrayList<String>();
for (Cell cell : listCell) {
byte[] rowArray = cell.getRowArray();
byte[] familyArray = cell.getFamilyArray();
byte[] qualifierArray = cell.getQualifierArray();
byte[] valueArray = cell.getValueArray();
int rowoffset = cell.getRowOffset();
int familyoffset = cell.getFamilyOffset();
int qualifieroffset = cell.getQualifierOffset();
int valueoffset = cell.getValueOffset();
int rowlength = cell.getRowLength();
int familylength = cell.getFamilyLength();
int qualifierlength = cell.getQualifierLength();
int valuelength = cell.getValueLength();
byte[] temprowarray = new byte[rowlength];
System.arraycopy(rowArray, rowoffset, temprowarray, 0, rowlength);
String temprow = Bytes.toString(temprowarray);
byte[] tempqulifierarray = new byte[qualifierlength];
System.arraycopy(qualifierArray, qualifieroffset, tempqulifierarray, 0, qualifierlength);
String tempqulifier = Bytes.toString(tempqulifierarray);
byte[] tempfamilyarray = new byte[familylength];
System.arraycopy(familyArray, familyoffset, tempfamilyarray, 0, familylength);
String tempfamily = Bytes.toString(tempfamilyarray);
byte[] tempvaluearray = new byte[valuelength];
System.arraycopy(valueArray, valueoffset, tempvaluearray, 0, valuelength);
String tempvalue = Bytes.toString(tempvaluearray);
tempMap.put(tempfamily + ":" + tempqulifier, tempvalue);
rowname = temprow;
String familyname = tempfamily;
if (familynamelist.indexOf(familyname) < 0) {
familynamelist.add(familyname);
}
}
resMap.put("rowname", rowname);
for (String familyname : familynamelist) {
HashMap<String, Object> tempFilterMap = new HashMap<String, Object>();
for (String key : tempMap.keySet()) {
String[] keyArray = key.split(":");
if (keyArray[0].equals(familyname)) {
tempFilterMap.put(keyArray[1], tempMap.get(key));
}
}
resMap.put(familyname, tempFilterMap);
}
return resMap;
}
//查看某行
public static Map<String, Object> get(String tablename, String row) throws Exception {
Table table = connection.getTable(TableName.valueOf(tablename));
Get get = new Get(Bytes.toBytes(row));
Result result = table.get(get);
System.out.println("Get: " + result);
return resultToMap(result);
}
//查看全表
public static List<Map<String, Object>> scan(String tablename) throws Exception {
Table table = connection.getTable(TableName.valueOf(tablename));
Scan s = new Scan();
ResultScanner rs = table.getScanner(s);
List<Map<String, Object>> resList = new ArrayList<Map<String, Object>>();
for (Result r : rs) {
Map<String, Object> tempmap = resultToMap(r);
resList.add(tempmap);
}
return resList;
}
//删除表
public static boolean delete(String tableName) throws IOException {
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
if (admin.tableExists(tableName)) {
try {
admin.disableTable(tableName);
admin.deleteTable(tableName);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
return true;
}
//删除ColumnFamily
public static boolean deleteColumnFamily(String tableName, String columnFamilyName) throws IOException {
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
if (admin.tableExists(tableName)) {
try {
admin.deleteColumn(tableName, columnFamilyName);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
return true;
}
//删除row
public static boolean deleteRow(String tableName, String rowName) throws IOException {
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
Table table = connection.getTable(TableName.valueOf(tableName));
if (admin.tableExists(tableName)) {
try {
Delete delete = new Delete(rowName.getBytes());
table.delete(delete);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
return true;
}
//删除qualifier
public static boolean deleteQualifier(String tableName, String rowName, String columnFamilyName,
String qualifierName) throws IOException {
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
Table table = connection.getTable(TableName.valueOf(tableName));
if (admin.tableExists(tableName)) {
try {
Delete delete = new Delete(rowName.getBytes());
delete.addColumns(columnFamilyName.getBytes(), qualifierName.getBytes());
table.delete(delete);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
return true;
}
}
第四步:添加及修改页面
index.htm代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="../js/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
data:{"data1":"11","data2":"22"},
url:"/hello/scanStudent",
type:"post",
success:function(data){
var list=data.infos;
var tr="";
$.each(list, function(i, item){
tr +="<tr><td>"+item.rowname+"</td>"+"<td>"+item.base_info.no+"</td>"
+"<td>"+item.base_info.name+"</td>"+"<td>"+item.base_info.cls+"</td>"
+"<td><a href='/hello/delete?rowname="+item.rowname+"'>删除</a>|<a href='/hello/openupdate?rowname="+item.rowname+"'>修改</a></td>";
});
$("table").append(tr);
}
});
});
</script>
</head>
<body align="center">
这是学生信息页面<br>
<a href="/hello/add">添加</a>|<a href="/hello">刷新</a>|
<hr>
<table border="1" align="center">
<thead>
<tr>
<td>行号</td>
<td>学号</td>
<td>姓名</td>
<td>班级</td>
<td>操作</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
</html>
添加add.htm和update.jsp两张页面
add.html代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加学生信息页面</title>
</head>
<body>
请填写学生信息<br>
<form action="/hello/save" method="post">
行号:<input type="text" name="rowname"><br>
学号:<input type="text" name="no"><br>
姓名:<input type="text" name="name"><br>
班级:<input type="text" name="cls"><br>
<input type="submit" value="保存">
</form>
</body>
</html>
update.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>修改信息页面</title>
</head>
<body>
请修改学生信息<br>
<form action="/hello/save" method="post">
行号:<input type="text" name="rowname" value="${info.rowname}"><br>
学号:<input type="text" name="no" value="${info.base_info.no}"><br>
姓名:<input type="text" name="name" value="${info.base_info.name}"><br>
班级:<input type="text" name="cls" value="${info.base_info.cls}"><br>
<input type="submit" value="保存">
</form>
</body>
</html>
步骤5,:启动服务,查看运行结果
1、打开start.java,右键运行
2、打开浏览器,输入:http://localhost:8082/hello
如果看到以上图片的结果,说明成功!
接下来你就可以测试你的其他功能!!!!
更多推荐
所有评论(0)