jfinal+hbase+eclipse开发web项目详细步骤04---在web页面实现对hbase数据库数据的增删查改功能

首先提醒大家,本节是在步骤01、步骤02、步骤03都成功的基础上做进一步开发。如果在之前的任何一个步骤出现问题,那么希望你先解决好问题之后,再做本次的开发。

步骤1:建表。

1、打开我们虚拟机,并且启动hadoop、hbase

start-all.sh

start-hbase.sh

2、进入hbase shell 界面

输入:hbase shell

jfinal+hbase+eclipse开发web项目详细步骤04---在web页面实现对hbase数据库数据的增删查改功能_第1张图片

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类

jfinal+hbase+eclipse开发web项目详细步骤04---在web页面实现对hbase数据库数据的增删查改功能_第2张图片

全部复制之前的代码,代码如下:

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 resMap = new HashMap();
		//获取数据库的数据		
		try {
			resMap=Hbase.get("student", rowname);
		} catch (Exception e) {
			e.printStackTrace();
		}
		setAttr("info", resMap);
		renderJsp("update.jsp");
	}

	public void indexData() {
		ArrayList 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 resMap = new HashMap();
		try {
			resMap = Hbase.get("stu", "2");
		} catch (Exception e) {
			e.printStackTrace();
		}
		renderText(resMap.toString());
	}

	// 查询student表信息
	public void scanStudent() {
		List> studnets = new ArrayList>();
		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 puts = new ArrayList();

		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 puts,String tablename)
				throws Exception {
			Table table = connection.getTable(TableName.valueOf(tablename));
			table.put(puts);
			return true;
		}
//把result转换成map,方便返回json数据
	private static Map resultToMap(Result result) {
		Map resMap = new HashMap();
		List listCell = result.listCells();
		Map tempMap = new HashMap();
		String rowname = "";
		List familynamelist = new ArrayList();
		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 tempFilterMap = new HashMap();
			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 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> scan(String tablename) throws Exception {
		Table table = connection.getTable(TableName.valueOf(tablename));
		Scan s = new Scan();
		ResultScanner rs = table.getScanner(s);

		List> resList = new ArrayList>();
		for (Result r : rs) {
			Map 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;
	}
}

第四步:添加及修改页面

jfinal+hbase+eclipse开发web项目详细步骤04---在web页面实现对hbase数据库数据的增删查改功能_第3张图片

index.htm代码如下:





Insert title here




这是学生信息页面
添加|刷新|
行号 学号 姓名 班级 操作

添加add.htm和update.jsp两张页面

add.html代码如下:





添加学生信息页面


请填写学生信息
行号:
学号:
姓名:
班级:

update.jsp代码如下:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>




修改信息页面



请修改学生信息
行号:
学号:
姓名:
班级:

步骤5,:启动服务,查看运行结果

1、打开start.java,右键运行

2、打开浏览器,输入:http://localhost:8082/hello

jfinal+hbase+eclipse开发web项目详细步骤04---在web页面实现对hbase数据库数据的增删查改功能_第4张图片

如果看到以上图片的结果,说明成功!

接下来你就可以测试你的其他功能!!!!

 

你可能感兴趣的:(jfinal+hbase+eclipse开发web项目详细步骤04---在web页面实现对hbase数据库数据的增删查改功能)