?
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class HbaseHelps
{
public static Connection GetConnection() throws IOException {
Configuration conf = HBaseConfiguration.create();
//创建和HBASE的连接
return ConnectionFactory.createConnection(conf);
}
//操作对象的封装
public static Admin Getadmin() throws IOException{
return GetConnection().getAdmin();
}
//创建命名空间
public static boolean Exec_NameSpace(String namespace) throws IOException {
try{
Getadmin().getNamespaceDescriptor(namespace);
return true;
}catch (NamespaceNotFoundException ex){
NamespaceDescriptor build=NamespaceDescriptor.create(namespace).build();
Getadmin().createNamespace(build);
return true;
}
catch (IOException e){
e.printStackTrace();
}
return false;
}
//创建表
public static boolean Exec_CreateTable(String tablename,String family) throws IOException{
boolean result=false;
//查看该表是否已经存在
TableName tableName =TableName.valueOf(tablename);
if (Getadmin().tableExists(tableName)==false){
//如不存在
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);//表
HColumnDescriptor hColumnDescriptor=new HColumnDescriptor(family);//列族
hTableDescriptor.addFamily(hColumnDescriptor);//添加列族
Getadmin().createTable(hTableDescriptor);//添加表
result=true;
}
return result;
}
//插入数据
public static boolean Insert_Data(String tablename,String family,String rowkey,String colum,String value) throws Exception {
boolean result=false;
try {
Put put = new Put(Bytes.toBytes(rowkey));
put.addColumn(Bytes.toBytes(family), Bytes.toBytes(colum), Bytes.toBytes(value));
TableName table = TableName.valueOf(tablename);
GetConnection().getTable(table).put(put);
result=true;
}
catch (Exception ex){
result =false;
throw new Exception("插入出现问题 ");
}
return result;
}
//查询
public static Result Exec_GetDataByRowKey(String tname,String rowkey) throws IOException {
Get get =new Get(Bytes.toBytes(rowkey));
TableName tableName = TableName.valueOf(tname);
Result result =GetConnection().getTable(tableName).get(get);
return result;
}
//第一种删除
public static boolean Exec_DelData(String tname,String rowkey) throws IOException {
TableName tableName=TableName.valueOf(tname);
//删除前先查询是否存在
if (!Exec_GetDataByRowKey(tname,rowkey).isEmpty()){
Delete delete = new Delete(Bytes.toBytes(rowkey));
GetConnection().getTable(tableName).delete(delete);
return true;
}
else {
System.out.print("不存在 ");
return false;
}
}
//第二种删除方法重载同名 输入内容不同
public static boolean Exec_DelData(String tname,String rowkey,String family,String colum) throws IOException {
TableName tableName=TableName.valueOf(tname);
//删除前查看此数据是否存在
if (!Exec_GetDataByRowKey(tname,rowkey).isEmpty()){
Delete delete = new Delete(Bytes.toBytes(rowkey));
delete.addColumn(Bytes.toBytes(family),Bytes.toBytes(colum));
GetConnection().getTable(tableName).delete(delete);
return true;
}
else{
return false;
}
}
}
下面进行代码实现
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.Scanner;
public class dome11_17 {
static String tablename="Myschool8848:stuinfo";
public static void main(String[] args) throws Exception {
HbaseHelps.Exec_NameSpace("Myschool8848");
HbaseHelps.Exec_CreateTable("Myschool8848:stuinfo", "info");
while (true){
System.out.print("欢迎访问学生信息系统");
System.out.print("1:注册");
System.out.print("2:查看");
System.out.print("3:删除");
System.out.print("4:修改");
Scanner scanner = new Scanner(System.in);
String choice = scanner.next();
switch (choice){
case "1":
AddStu();
break;
case "2":
GetStu();
break;
case "3":
DelStu();
break;
case "4":
xiugai();
break;
}
}
}
public static void AddStu() throws Exception {
Scanner scanner= new Scanner(System.in);
System.out.print("+++++++++++++++++1:注册++++++++++++++++++++++++++++++++++++");
System.out.print("请输入学号:");
String stuid =scanner.next();
System.out.print("请输入姓名:");
String stuname =scanner.next();
System.out.print("请输入年龄:");
String stuage =scanner.next();
boolean r1=HbaseHelps.Insert_Data(tablename,"info",stuid,"stuid",stuid);
boolean r2=HbaseHelps.Insert_Data(tablename,"info",stuid,"name",stuname);
boolean r3=HbaseHelps.Insert_Data(tablename,"info",stuid,"age",stuage);
if (r1&&r2&&r3){
System.out.print("注册成功");
}
}
public static void DelStu() throws IOException {
Scanner scanner=new Scanner(System.in);
System.out.print("+++++++++++++++++2:删除++++++++++++++++++++++++++++++++++++");
System.out.print("请选择删除方式:1.删除整个学生信息 2.删除单项数据");
String choice=scanner.next();
if(choice.equals("1")){
System.out.print("+++++++++++++++++++++++++++++++++++");
System.out.print("请输入删除学生学号");
String stuid =scanner.next();
boolean r=HbaseHelps.Exec_DelData(tablename,stuid);
if (r==true){
System.out.print("删除成功");
System.out.print("+++++++++++++++++++++++++++++++++++");
}
}
else {
System.out.print("+++++++++++++++++++++++++++++++++++");
System.out.print("请输入删除学生学号");
String stuid =scanner.next();
System.out.print("请输入需要删除的列(name,age):");
String st =scanner.next();
boolean r =HbaseHelps.Exec_DelData(tablename,stuid,"info",st);
if (r){
System.out.print("删除成功");
System.out.print("+++++++++++++++++++++++++++++++++++");
}
}
}
public static void xiugai() throws Exception {
Scanner scanner= new Scanner(System.in);
System.out.print("+++++++++++++++++修改++++++++++++++++++++++++++++++++++++");
System.out.print("请输入学号:");
String stuid =scanner.next();
System.out.print("请输入修改内容:姓名/年龄/全部");
String neirong=scanner.next();
if(neirong.equals("全部")){
System.out.print("请输入姓名:");
String stuname =scanner.next();
System.out.print("请输入年龄:");
String stuage =scanner.next();
boolean r2=HbaseHelps.Insert_Data(tablename,"info",stuid,"name",stuname);
boolean r3=HbaseHelps.Insert_Data(tablename,"info",stuid,"age",stuage);
if (r2&&r3){
System.out.print("成功");
}
}
else if(neirong.equals("姓名")){
System.out.print("请输入姓名:");
String stuname =scanner.next();
boolean r2=HbaseHelps.Insert_Data(tablename,"info",stuid,"name",stuname);
if (r2==true){
System.out.print("成功");
}
}
else if(neirong.equals("年龄")){
System.out.print("请输入年龄:");
String stuage =scanner.next();
boolean r3=HbaseHelps.Insert_Data(tablename,"info",stuid,"age",stuage);
if (r3==true){
System.out.print("成功");
}
}
}
public static void GetStu() throws IOException {
Scanner scanner =new Scanner(System.in);
System.out.print("+++++++++++++++++++++++++++++++++++");
System.out.print("这里是学生查找系统");
System.out.print("请输入查询学生学号");
String stuid =scanner.next();
Result result =HbaseHelps.Exec_GetDataByRowKey(tablename,stuid);
if(result.isEmpty()){
System.out.print("学生不存在");
System.out.print("");
}
else {
for (Cell cell:result.rawCells()){
System.out.print("rowkey:"+ Bytes.toString(CellUtil.cloneRow(cell))+" ");
System.out.print("family:"+ Bytes.toString(CellUtil.cloneFamily(cell))+" ");
System.out.print("column:"+ Bytes.toString(CellUtil.cloneQualifier(cell))+" ");
System.out.print("value:"+ Bytes.toString(CellUtil.cloneValue(cell))+" ");
}
}
}
}
|