Java连接HBASE对表的增删改查(含封装)
前言
Java连接HBASE对表中数据进行操作(增删改查,分页)
具体题目如下:
创建表school:stuifo
列名为:id,name,sex, phone,address
使用api操作,要求每一个题目用一个函数封装
1、 插入测试数据如:
‘001’,‘张三’,0,1396523658,‘陈家湾’
‘002’,‘李司’,1, 15836952468,‘半月楼’
2、 实现根据行键删除整行数据
3、 实现删除某个列的数据
4、 根据行键实现修改功能
5、 根据输入的学号查询对应的学生信息
6、 查询学号是00开头的数据
7、 查询出姓张的学员信息
8、 查询出学员的姓名列、电话列
9、 查询姓名是张三,并且电话是1396523658的数据
10、实现学生信息的分页
一、封装类
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class helper {
//连接的封装
public static Connection GetConn() throws IOException {
// 加载配置空间
Configuration conf = HBaseConfiguration.create();
// 连接HBASE
Connection connection = ConnectionFactory.createConnection(conf);
return connection;
}
//操作对象的封装
public static Admin Getadmin() throws IOException {
Admin admin = GetConn().getAdmin();
return admin;
}
//创建命名空间
public static boolean Exec_CreateNamesplace(String namesplace) throws IOException {
boolean result = false;
try {
Getadmin().getNamespaceDescriptor(namesplace);
} catch (NamespaceNotFoundException e) {
NamespaceDescriptor build = NamespaceDescriptor.create(namesplace).build();
Getadmin().createNamespace(build);
result = true;
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
//创建表
public static boolean Exec_CreateTable(String tablename, String family) throws IOException {
//查看表是否存在
//得到表名对象
TableName Tname = TableName.valueOf(tablename);
if (Getadmin().tableExists(Tname) == false) {
//如果不存在
HTableDescriptor hTableDescriptor = new HTableDescriptor(Tname);
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(family);
hTableDescriptor.addFamily(hColumnDescriptor);
Getadmin().createTable(hTableDescriptor);
return true;
} else {
return false;
}
}
//插入数据
public static boolean Exec_AddData(String Tname, String family, String rowkey, String column, String value) {
try {
Put put = new Put(Bytes.toBytes(rowkey));
put.addColumn(Bytes.toBytes(family), Bytes.toBytes(column), Bytes.toBytes(value));
//得到表名对象
TableName tableName = TableName.valueOf(Tname);
GetConn().getTable(tableName).put(put);
return true;
} catch (IOException e) {
return false;
}
}
//查询数据
public static Result Exec_GetDataByRowKey(String tname, String rowkey) throws IOException {
Get get = new Get(Bytes.toBytes(rowkey));
Result result = GetConn().getTable(TableName.valueOf(tname)).get(get);
if (result.isEmpty()) {
return null;
} else {
return result;
}
}
//删除数据(删除整行的数据)
public static boolean Exec_DelDataByRow(String tname, String rowkey) throws IOException {
TableName tableName = TableName.valueOf(tname);
//删除之前,先查看该条数据是否存在
if (Exec_GetDataByRowKey(tname, rowkey) != null) {
Delete delete = new Delete(Bytes.toBytes(rowkey));
GetConn().getTable(tableName).delete(delete);
return true;
} else {
return false;
}
}
//删除数据(删除单元格的数据)
public static boolean Exec_DelDataByCell(String tname, String rowkey, String family, String column) throws IOException {
//得到表名对象
TableName tableName = TableName.valueOf(tname);
//删除之前,先查看该数据是否存在
if (Exec_GetDataByRowKey(tname, rowkey) != null) {
Delete delete = new Delete(Bytes.toBytes(rowkey));
// 相比之下只多了这一行代码
delete.addColumn(Bytes.toBytes(family), Bytes.toBytes(column));
GetConn().getTable(tableName).delete(delete);
return true;
} else {
return false;
}
}
//封装查询
public static void output1(Scan scan) throws IOException {
//获取表
Table table = helper.GetConn().getTable(TableName.valueOf("school:stuinfo"));
//获取表中数据
ResultScanner rs = table.getScanner(scan);
for (Result r : rs) {
String name = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
String phone = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("phone")));
String str = "{姓名:" + name + " 电话:" + phone + "}";
System.out.println(str);
}
}
public static void output(Scan scan) throws IOException {
//获取表
Table table = helper.GetConn().getTable(TableName.valueOf("school:stuinfo"));
//获取表中数据
ResultScanner rs = table.getScanner(scan);
//加强版for循环
for (Result r : rs) {
String id = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("id")));
String name = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
String sex = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("sex")));
String phone = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("phone")));
String address = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("address")));
String str = "{学号:" + id + " 姓名:" + name + " 性别:" + sex + " 电话:" + phone + " 地址:" + address + "}";
System.out.println(str);
}
}
// 扫描结果,返回结果中的最后一条数据
// ResultScanner 查询结果
public static byte[] printResult(ResultScanner rs) {
//遍历查询结果,并得到最后一条数据,赋值位lastrowkey
byte[] lastrowkey = null;
for (Result r : rs) {
String id = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("id")));
String name = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
String sex = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("sex")));
String phone = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("phone")));
String address = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("address")));
String str = "{编号:" + Bytes.toString(r.getRow()) + " 学号:" + id + " 姓名:" + name + " 性别:" + sex + " 电话:" + phone + " 地址:" + address + "}";
System.out.println(str);
lastrowkey = r.getRow();
}
return lastrowkey;
}
}
二、主函数
import org.apache.hadoop.hbase.TableName;
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.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Scanner;
public class term_1 {
static String tablename = "school:stuinfo";
public static void main(String[] args) throws IOException {
while (true) {
helper.Exec_CreateNamesplace("school");
helper.Exec_CreateTable(tablename, "info");
System.out.println("----------------------------------------");
System.out.println("!!!欢迎访问学生管理系统!!!");
System.out.println("1.注册学生信息");
System.out.println("2.查看学生信息");
System.out.println("3.删除学生信息");
System.out.println("4.修改学生信息");
System.out.println("5.实现学生信息的分页");
System.out.println("6.退出学生管理系统");
System.out.println("----------------------------------------");
Scanner scanner = new Scanner(System.in);
String choice = scanner.next();
switch (choice) {
case "1":
System.out.println("----------------------------------------");
AddStu();
break;
case "2":
System.out.println("----------------------------------------");
GetStu();
break;
case "3":
System.out.println("----------------------------------------");
DelStu();
break;
case "4":
System.out.println("----------------------------------------");
UpdataStu();
break;
case "5":
System.out.println("----------------------------------------");
System.out.println("这里是学生信息分页的界面:");
changePage(1);
break;
case "6":
System.out.println("----------------------------------------");
System.out.println("成功退出学生管理系统!");
System.exit(0);
}
}
}
public static void GetStu() throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.println("这里是学生管理系统的查找界面");
System.out.println("请选择:\n" +
" 1.已知学号\n " +
"2.模糊查询学号(比如学号00开头)\n " +
"3.模糊查询只知道一个信息的数据(比如姓张的学员信息)\n" +
" 4.查询出学生的姓名列、电话列\n" +
" 5.模糊查询只记得两个信息的数据(比如姓名是张三,电话是1396523658)\n " +
"6.退出");
String choice = scanner.next();
if (choice.equals("1")) {
System.out.println("请输入想要查询学生的学号:");
String sid = scanner.next();
Result r = helper.Exec_GetDataByRowKey(tablename, sid);
if (r == null) {
System.out.println("没有找到该学生信息!");
} else {
String id = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("id")));
String name = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
String sex = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("sex")));
String phone = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("phone")));
String address = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("address")));
String str = "{学号:" + id + " 姓名:" + name + " 性别:" + sex + " 电话:" + phone + " 地址:" + address + "}";
System.out.println(str);
}
} else if (choice.equals("2")) {
System.out.println("----------------------------------------");
System.out.println("这里是模糊查询学号界面:");
System.out.println("请输入部分学号,比如00:");
String id = scanner.next();
ScanRowFilter(id);
} else if (choice.equals("3")) {
System.out.println("----------------------------------------");
System.out.println("这里是通过已知的一个信息查询一名完整学生信息的界面:");
System.out.println("请输入列族info:");
String family = scanner.next();
System.out.println("请输入所查找的列名(id,name,sex,phone,address):");
String column = scanner.next();
System.out.println("请输入值的一部分:");
String value = scanner.next();
ScanSingleColumnValueFilter(family, column, value);
} else if (choice.equals("4")) {
System.out.println("----------------------------------------");
System.out.println("这里是查询出全部学生的姓名列,电话列的界面:");
ScanMultipleColumPrefixFilter();
} else if (choice.equals("5")) {
System.out.println("----------------------------------------");
System.out.println("这里是通过已知的两个信息查询一名完整学生信息的界面:");
System.out.println("请输入列族info:");
String family1 = scanner.next();
System.out.println("请输入所查找的列名(id,name,sex,phone,address):");
String column1 = scanner.next();
System.out.println("请输入该列值的一部分:");
String value1 = scanner.next();
System.out.println("请输入所查找的列名(id,name,sex,phone,address):");
String column2 = scanner.next();
System.out.println("请输入该列值的一部分:");
String value2 = scanner.next();
ScanValueFilter(family1, column1, column2, value1, value2);
} else if (choice.equals("6")) {
return;
}
}
public static void AddStu() {
Scanner scanner = new Scanner(System.in);
System.out.println("这里是学生注册功能界面");
System.out.println("请输入学号:");
String id = scanner.next();
System.out.println("请输入姓名:");
String name = scanner.next();
System.out.println("请输入性别:");
String sex = scanner.next();
System.out.println("请输入电话号码:");
String phone = scanner.next();
System.out.println("请输入地址:");
String address = scanner.next();
boolean r1 = helper.Exec_AddData(tablename, "info", id, "id", id);
boolean r2 = helper.Exec_AddData(tablename, "info", id, "name", name);
boolean r3 = helper.Exec_AddData(tablename, "info", id, "sex", sex);
boolean r4 = helper.Exec_AddData(tablename, "info", id, "phone", phone);
boolean r5 = helper.Exec_AddData(tablename, "info", id, "address", address);
if (r1 && r2 && r3 && r4 && r5) {
System.out.println("注册成功!");
}
}
public static void DelStu() throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.println("这里是学生管理系统的删除功能界面");
System.out.println("请选择:1.删除整个学生信息(按行键删除) 2.删除单个列");
String choice = scanner.next();
String id = "";
if (choice.equals("1")) {
System.out.println("请输入学号:");
id = scanner.next();
boolean r = helper.Exec_DelDataByRow(tablename, id);
if (r) {
System.out.println("删除整个学生信息成功!");
}
} else {
System.out.println("请输入学号:");
id = scanner.next();
System.out.println("请输入要删除的列(id,name,sex,phone,address)");
String delColumn = scanner.next();
boolean r = helper.Exec_DelDataByCell(tablename, id, "info", delColumn);
if (r) {
System.out.println("该单个列表被删除成功!");
}
}
}
public static void UpdataStu() {
Scanner scanner = new Scanner(System.in);
System.out.println("这里是学生管理系统的修改界面");
System.out.println("请输入想要修改的学生的学号:");
String id = scanner.next();
System.out.println("请输入新学生姓名:");
String name = scanner.next();
System.out.println("请输入新学生性别:");
String sex = scanner.next();
System.out.println("请输入新学生电话号码:");
String phone = scanner.next();
System.out.println("请输入新学生地址:");
String address = scanner.next();
boolean r1 = helper.Exec_AddData(tablename, "info", id, "id", id);
boolean r2 = helper.Exec_AddData(tablename, "info", id, "name", name);
boolean r3 = helper.Exec_AddData(tablename, "info", id, "sex", sex);
boolean r4 = helper.Exec_AddData(tablename, "info", id, "phone", phone);
boolean r5 = helper.Exec_AddData(tablename, "info", id, "address", address);
if (r1 && r2 && r3 && r4 && r5) {
System.out.println("修改成功!");
}
}
// 查询学号是00开头的数据(学号设为行键)
public static void ScanRowFilter(String str) throws IOException {
//实例化scan对象
Scan scan = new Scan();
//构建过滤器(行键过滤器;第一个参数:等于)
RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator(str));
// 为scan对象设置过滤器
scan.setFilter(rowFilter);
// 在表上查找数据
helper.output(scan);
}
//查询出姓张的学员信息
public static void ScanSingleColumnValueFilter(String family, String column, String value) throws IOException {
//实例化scan对象
Scan scan = new Scan();
//构建过滤器(值过滤器:在指定的列族和列中进行比较)
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(Bytes.toBytes(family), Bytes.toBytes(column), CompareFilter.CompareOp.EQUAL, new SubstringComparator(value));
//为scan对象设置过滤器
scan.setFilter(singleColumnValueFilter);
// 在表上查找数据
helper.output(scan);
}
//查询出学员的姓名列、电话列
public static void ScanMultipleColumPrefixFilter() throws IOException {
//实例化scan对象
Scan scan = new Scan();
//列过滤器(可以指定多个前缀对列名称过滤)
byte[][] bytes = new byte[][]{Bytes.toBytes("name"), Bytes.toBytes("phone")};
MultipleColumnPrefixFilter columnPrefixFilter = new MultipleColumnPrefixFilter(bytes);
//为scan对象设置过滤器
scan.setFilter(columnPrefixFilter);
// 用scan扫描
helper.output1(scan);
}
//查询姓名是张三,并且电话是1396523658的数据
public static void ScanValueFilter(String family, String column1, String column2, String value1, String value2) throws IOException {
//实例化scan对象
Scan scan = new Scan();
//构建过滤器(值过滤器:在指定的列族和列中进行比较)
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(Bytes.toBytes(family), Bytes.toBytes(column1), CompareFilter.CompareOp.EQUAL, new SubstringComparator(value1));
SingleColumnValueFilter singleColumnValueFilter1 = new SingleColumnValueFilter(Bytes.toBytes(family), Bytes.toBytes(column2), CompareFilter.CompareOp.EQUAL, new SubstringComparator(value2));
//两个过滤器合并
ArrayList<Filter> Filters = new ArrayList<>();
Filters.add(singleColumnValueFilter);
Filters.add(singleColumnValueFilter1);
//and
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL, Filters);
// 为scan设置过滤器
scan.setFilter(filterList);
// 在表上查找数据
helper.output(scan);
}
//实现学生信息的分页
// 当前页数是pageindex ,一页有Pagesize的数据量
public static void changePage(int pageindex) throws IOException {
//构建分页过滤器
PageFilter pageFilter = new PageFilter(1);
//实例化scan对象
Scan scan = new Scan();
// 为scan设置过滤器
scan.setFilter(pageFilter);
Table table = helper.GetConn().getTable(TableName.valueOf(tablename));
while (true) {
System.out.println("当前是" + pageindex + "页数的数据:");
ResultScanner scanner = table.getScanner(scan);
//返回每页最后一行的数据
byte[] lastrow = helper.printResult(scanner);
System.out.println("是否显示下一页数据:(y/n)");
String choice = new Scanner(System.in).next();
if (choice.equals("y")) {
// 改变101变成了1010,这样子hbase会查不到101,就会从102开始查询
byte[] startrowkwy = Bytes.add(lastrow, new byte[1]);
scan.setStartRow(Bytes.toBytes(ByteBuffer.wrap(startrowkwy)));
pageindex++;
continue;
} else {
break;
}
}
}
}