1. 程式人生 > >jfinal+hbase+eclipse開發web專案詳細步驟03---jfinal工程中加入hbase外掛

jfinal+hbase+eclipse開發web專案詳細步驟03---jfinal工程中加入hbase外掛

首先,這個步驟是在01工程步驟之上進行修改。
其次,要準備好hbase開發jar包(我們沒有用mevan,所以要下載我準備好的jar包,下載地址:hbase1.2.6開發jar包,如果已經在01中下載好,那麼就不需要再下載)。

1、將下載好的hbase開發jar包全部複製(Ctrl+A,Ctrl+C)到工程目錄下的lib資料夾(Ctrl+V)

在這裡插入圖片描述

在這裡插入圖片描述

2、在工程目錄com包下新建plugin包
選中com.demo,右鍵新建package,點選finish
在這裡插入圖片描述
在這裡插入圖片描述

3、在plugin包中分別新建Hbase、HbasePlugin類
在這裡插入圖片描述

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; } //把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 String 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).toString(); } //檢視全表 public static String 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.toString(); } //刪除表 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; } }

HbasePlugin.java

package com.plugin;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.ConnectionFactory;

import com.jfinal.plugin.IPlugin;

public class HbasePlugin implements IPlugin{

    private String quorum;

    public HbasePlugin(String  quorum) {
        this.quorum = quorum;
    }
    @Override
    public boolean start() {
        Configuration config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.quorum", quorum);   
        config.set("hbase.zookeeper.property.clientPort", "2181");
        try {
            Hbase.connection = ConnectionFactory.createConnection(config);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }
    @Override
    public boolean stop() {
        if (!Hbase.connection.isClosed()) {
            try {
                Hbase.connection.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }
}

4、在DemoConfig類的configPlugin方法中新增以下程式碼

//新增hbase外掛
		String quorum="192.168.80.128";//這裡是你虛擬機器的ip
		HbasePlugin hp=new HbasePlugin(quorum);
		me.add(hp);

在這裡插入圖片描述

5、在HelloController中新增一個方法,程式碼如下

在這裡插入圖片描述

	public void hbase() {

		String r = "資料有問題!!";
		try {
			r = Hbase.get("stu", "1");//注意,這是你hbase的表名和行
		} catch (Exception e) {
			e.printStackTrace();
		}
		renderText(r);
	}

6、修改虛擬機器linux下的hosts檔案
開啟linux終端,輸入sudo gedit /etc/hosts
並且在127.0.0.1下面新增你虛擬機器ip 空格 在寫你虛擬機器的名稱
在這裡插入圖片描述

7、修改你電腦windows下hosts檔案
開啟C:\Windows\System32\drivers\etc
在這裡插入圖片描述
用記事本開啟hosts檔案,新增一條和虛擬機器一樣的資訊
在這裡插入圖片描述

8、啟動虛擬機器的hadoop、hbase

start-all.sh
start-hbase.sh

9、建立表
進入虛擬機器輸入hbase shell
輸入:create ‘stu’,‘fm1’
再輸入:put ‘stu’,1,‘fm1:name’,‘xiaoming’

10、啟動工程

開啟start.java類
run as

10、檢視是否連線成功!!

在瀏覽器中輸入:http://localhost:8082/hello/hbase
在這裡插入圖片描述