版权声明:请点击关注 https://blog.csdn.net/weixin_42914677/article/details/83016181
首先,这个步骤是在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
9、创建表
进入虚拟机输入hbase shell
输入:create ‘stu’,‘fm1’
再输入:put ‘stu’,1,‘fm1:name’,‘xiaoming’
10、启动工程
打开start.java类
run as
10、查看是否连接成功!!