HBase简单(初级)学生信息系统

HBase简单(初级)学生信息系统,第1张

HBase简单(初级)学生信息系统

上课跟着老师做的简单学生信息Hbase数据库

首先对 *** 作数据库的方法进行封装;创建命名空间、创建表、插入数据、查询数据、删除数据

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 HbaseUtil {
    // 连接
    public static Connection GetConn() throws IOException {
        Configuration conf = HbaseConfiguration.create();
        Connection connection = ConnectionFactory.createConnection(conf);
        return connection;
    }
    //  *** 作对象
    public static Admin GetAdmin() throws IOException {
        return GetConn().getAdmin();
    }
    // 创建命名空间
    public static boolean Exec_CreateNameSpace(String name) throws IOException {
        try {
            GetAdmin().getNamespaceDescriptor(name);
            return true;
        } catch (NamespaceNotFoundException e) {
            NamespaceDescriptor build = NamespaceDescriptor.create(name).build();
            GetAdmin().createNamespace(build);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
    // 创建表
    public static boolean Exec_CreateTable(String name, String info) throws IOException {
        TableName tableName = TableName.valueOf(name);
        if(GetAdmin().tableExists(tableName) == false){
            HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(info);
            hTableDescriptor.addFamily(hColumnDescriptor);
            GetAdmin().createTable(hTableDescriptor);
            return true;
        }
        return false;

    }
    // 添加数据
    public static boolean Exec_AddData(String tableName, String info, String rowkey, String column, String value) throws IOException {
        try{
            Put put = new Put(Bytes.toBytes(rowkey));
            put.addColumn(Bytes.toBytes(info), Bytes.toBytes(column), Bytes.toBytes(value));
            GetConn().getTable(TableName.valueOf(tableName)).put(put);
            return true;
        }catch (Exception ex){
            return false;
        }

    }
    // 查询数据
    public static Result Exec_GetDataByRowKey(String tablename, String rowkey) throws IOException {
        Get get = new Get(Bytes.toBytes(rowkey));
        Result result = GetConn().getTable(TableName.valueOf(tablename)).get(get);
        return result;
    }
    // 删除数据(行键)(单元格)
    public static boolean Exec_DelByRowKey(String tablename, String rowkey) throws IOException {
        Result result = Exec_GetDataByRowKey(tablename, rowkey);
        if(!result.isEmpty()){
            Delete delete = new Delete(Bytes.toBytes(rowkey));
            GetConn().getTable(TableName.valueOf(tablename)).delete(delete);
            return true;
        }else {
            return false;
        }
    }
    // 删除数据(单元格)
    public static boolean Exec_DelByRowKey(String tablename, String rowkey, String info, String column) throws IOException {
        Result result = Exec_GetDataByRowKey(tablename, rowkey);
        if(!result.isEmpty()){
            Delete delete = new Delete(Bytes.toBytes(rowkey));
            delete.addColumn(Bytes.toBytes(info), Bytes.toBytes(column));
            GetConn().getTable(TableName.valueOf(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.Result;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.Scanner;

public class TestDemo {
    static String tablname = "MySchool:stuinfo";
    public static void main(String[] args) throws IOException {
        HbaseUtil.Exec_CreateNameSpace("MySchool");
        HbaseUtil.Exec_CreateTable("MySchool:stuinfo", "info");
        while (true) {
            System.out.println("欢迎访问学生管理信息系统");
            System.out.println("1、注册学生信息");
            System.out.println("2、查看学生信息");
            System.out.println("3、删除学生信息");
            System.out.println("4、修改学生信息");
            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("调用修改的方法");
                    ChangeStu();
                    break;
            }
        }
    }
    public static void AddStu() throws IOException {
        System.out.println("----------------------------------------------------------------------------");
        System.out.println("这里是学生的注册功能界面");
        System.out.println("请输入学号:");
        Scanner scanner = new Scanner(System.in);
        String stuid = scanner.next();
        System.out.println("请输入姓名:");
        String stuname= scanner.next();
        System.out.println("请输入年龄:");
        String stuage = scanner.next();
        boolean r1 = HbaseUtil.Exec_AddData("MySchool:stuinfo", "info", stuid, "stuid", stuid);
        boolean r2 = HbaseUtil.Exec_AddData("MySchool:stuinfo", "info", stuid, "name", stuname);
        boolean r3 = HbaseUtil.Exec_AddData("MySchool:stuinfo", "info", stuid, "age", stuage);
        if(r1&&r2&&r3){
            System.out.println("注册成功!!");
        }
    }

    public static void DelStu() throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("----------------------------------------------------------------------------");
        System.out.println("这里是学生的删除功能界面");
        System.out.println("请选择:1、删除整个学生信息 2、删除单个列");
        String chioce = scanner.next();
        String stuid = "";
        if(chioce.equals("1")){
            System.out.println("请输入学号:");
            stuid = scanner.next();
            boolean r = HbaseUtil.Exec_DelByRowKey("MySchool:stuinfo", stuid);
            if(r){
                System.out.println("删除成功!!");
            }
        }else {
            System.out.println("请输入学号:");
            stuid = scanner.next();
            System.out.println("请输入要删除的列(stuid, name, age):");
            String delColumn = scanner.next();
            boolean r = HbaseUtil.Exec_DelByRowKey("MySchool:stuinfo", stuid, "info", delColumn);
            if(r){
                System.out.println("删除成功!!");
            }
        }
    }
    public static void GetStu() throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("----------------------------------------------------------------------------");
        System.out.println("这里是学生的查看功能界面");
        System.out.println("请输入学号:");
        String stuid = scanner.next();
        Result result = HbaseUtil.Exec_GetDataByRowKey("MySchool:stuinfo", stuid);
        if(result.isEmpty()){
            System.out.println("没有该学生");
        }else {
            for (Cell cell : result.rawCells()){
                System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)) + ":" + Bytes.toString(CellUtil.clonevalue(cell)));
            }
        }
    }

public static void ChangeStu() throws IOException {
    System.out.println("----------------------------------------------------------------------------");
    System.out.println("这里是学生信息的修改功能界面");
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入学号:");
    String stuid = scanner.next();
    Result result = HbaseUtil.Exec_GetDataByRowKey("MySchool:stuinfo", stuid);
    if(result.isEmpty()){
        System.out.println("没有该学生");
    }else {
        for (Cell cell : result.rawCells()){
            System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)) + ":" + Bytes.toString(CellUtil.clonevalue(cell)));
        }
    }
    System.out.println("请选择:1、修改名字 2、修改年龄");
    String chioce = scanner.next();
    if(chioce.equals("1")){
        System.out.println("请输入姓名:");
        String stuname = scanner.next();
        boolean r = HbaseUtil.Exec_AddData("MySchool:stuinfo", "info", stuid, "name", stuname);
        if(r){
            System.out.println("修改成功!!");
        }
    }else {
        System.out.println("请输入年龄:");
        String stuage = scanner.next();
        boolean r = HbaseUtil.Exec_AddData("MySchool:stuinfo", "info", stuid, "age", stuage);
        if(r){
            System.out.println("修改成功!!");
        }
    }
    }


}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5618531.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存