Java操作Hbase数据库

```plaintext
/*
    Hbase api中基础我们要做的任务:
        1、如何创建一张表
        2、如何删除一张表
        3、如何向hbase表中插入一列数据
        4、如何向hbase表中插入一批数据
        5、如何获取一列数据
        6、如何获取批量列数据
        7、如何创建预分region表
 */


import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class HbaseAPI {
    private Connection conn;
    private Admin admin;

    @Before
    public void init() {
        try {
            //获取配置文件对象
            HBaseConfiguration conf = new HBaseConfiguration();

            //获取zookeeper集群信息
            conf.set("hbase.zookeeper.quorum", "master,node1,node2");

            //获取数据库连接对象
            conn = ConnectionFactory.createConnection(conf);
            System.out.println("Hbase数据库连接成功: " + conn);

            //获取数据库操作对象
            admin = conn.getAdmin();
            System.out.println("成功获取Hbase数据库操作对象:" + admin);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //1、创建一张表

    /**
     * 创建一张表至少要指定表名和列簇
     * create tablename 'cf'
     */
    @Test
    public void createOneTable() {
        try {
            //将表明封装成对象
            TableName tn = TableName.valueOf("students");

            //判断表是否存在
            boolean b = admin.tableExists(tn);
            if (!b) {
                //创建表描述器对象
                TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tn);

                //创建列簇描述器对象
                ColumnFamilyDescriptor info = ColumnFamilyDescriptorBuilder.of(Bytes.toBytes("info"));

                //添加列簇描述器对象到表描述器对象中
                //添加一个列簇
                //TableDescriptorBuilder setColumnFamily(ColumnFamilyDescriptor family)
                tableDescriptorBuilder.setColumnFamily(info);

                //添加多个列簇
                //TableDescriptorBuilder setColumnFamilies(Collection<ColumnFamilyDescriptor> families)

                admin.createTable(tableDescriptorBuilder.build());

            } else {
                System.out.println(tn + "表已存在");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //2、删除一张表

    /**
     * 先禁用,再删除
     */

    @Test
    public void deleteOneTable() {
        try {
            //将表明封装成对象
            TableName tn = TableName.valueOf("students");

            //判断表是否存在
            boolean b = admin.tableExists(tn);
            if (b) {
                admin.disableTable(tn);
                admin.deleteTable(tn);
            } else {
                System.out.println(tn + "表不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //3、如何向hbase表中插入一列数据

    /**
     * 3、如何向hbase表中插入一列数据
     * 1500100001,施笑槐,22,女,文科六班
     * put 'students','1500100001','info:name','施笑槐'
     * put 'students','1500100001','info:age','22'
     * put 'students','1500100001','info:gender','女'
     * put 'students','1500100001','info:clazz','文科六班'
     * <p>
     * <p>
     * 命令中的put: 代表一列
     * api中的Put:代表一行,可以设置多列
     */

    @Test
    public void putOneData() {
        try {
            //将表明封装成对象
            TableName tn = TableName.valueOf("students");

            //判断表是否存在
            boolean b = admin.tableExists(tn);
            if (b) {
                //获取表对象
                Table students = conn.getTable(tn);

                //将数据封装成Put对象
                Put put = new Put(Bytes.toBytes("1500100001"));

                //方式一:
                //public Put addColumn(byte[] family, byte[] qualifier, byte[] value)
//                put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("施笑槐"));
//                put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes("22"));
//                put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("sex"),Bytes.toBytes("女"));
//                put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("clazz"),Bytes.toBytes("文科六班"));

                //方式二:每一列都封装成cell对象
                //public KeyValue(byte[] row, byte[] family, byte[] qualifier, byte[] value)
                put.add(new KeyValue(Bytes.toBytes("1500100001"), Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("胡海祥")));
                put.add(new KeyValue(Bytes.toBytes("1500100001"), Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("22")));
                put.add(new KeyValue(Bytes.toBytes("1500100001"), Bytes.toBytes("info"), Bytes.toBytes("sex"), Bytes.toBytes("男")));
                put.add(new KeyValue(Bytes.toBytes("1500100001"), Bytes.toBytes("info"), Bytes.toBytes("clazz"), Bytes.toBytes("文科一班")));

                students.put(put);
            } else {
                System.out.println(tn + "表不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //4、向hbase表中插入一批数据
    @Test
    public void putMoreData() {
        try {
            //将表明封装成对象
            TableName tn = TableName.valueOf("students");

            //判断表是否存在
            boolean b = admin.tableExists(tn);
            if (b) {
                //获取表对象
                Table students = conn.getTable(tn);

                //创建一个集合存放Put对象
                ArrayList<Put> list = new ArrayList<>();

                //创建输入流
                BufferedReader br = new BufferedReader(new FileReader("data/student.txt"));
                String line = null;
                while ((line=br.readLine())!=null){
                    String[] split = line.split(",");
                    Put put = new Put(Bytes.toBytes(split[0]));
                    put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes(split[1]));
                    put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes(split[2]));
                    put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("sex"),Bytes.toBytes(split[3]));
                    put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("clazz"),Bytes.toBytes(split[4]));
                    list.add(put);
                }

                br.close();
                //void put(List<Put> puts)
                //需要将一个个的Put对象封装到list集合里面
                students.put(list);
            } else {
                System.out.println(tn + "表不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //5、获取一列数据
    @Test
    public void getOneData(){
        try {
            //将表明封装成对象
            TableName tn = TableName.valueOf("students");

            //判断表是否存在
            boolean b = admin.tableExists(tn);
            if (b) {
                //获取表对象
                Table students = conn.getTable(tn);
                //将要获取的数据封装成Get对象
                Get get = new Get(Bytes.toBytes("1500100001"));
                //根据行键获取的是一行数据,获取每一列需要解析result
                Result result = students.get(get);
                //获取行键
//                String id = Bytes.toString(result.getRow());

                //获取方式一
                //根据列簇列名获取列值
                //public byte[] getValue(byte[] family, byte[] qualifier)
//                String name = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
//                String age = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age")));
//                String sex = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("sex")));
//                String clazz = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("clazz")));
//
//                System.out.println("学号:"+id+", 姓名:"+name+", 年龄:"+age+", 性别:"+age+", 班级:"+clazz);

                //获取方式二
                List<Cell> cells = result.listCells();
                StringBuilder sb = new StringBuilder();
                for (Cell cell : cells) {
                    //获取单元格的行键
                    String rowKey = Bytes.toString(CellUtil.cloneRow(cell));
                    //获取单元格的列簇
//                    String cf = Bytes.toString(CellUtil.cloneFamily(cell));
                    //获取单元格列名
                    String colName = Bytes.toString(CellUtil.cloneQualifier(cell));
                    //获取单元格列值
                    String colValue = Bytes.toString(CellUtil.cloneValue(cell));

                    sb.append(colName).append(":").append(colValue).append(", ");
                }
                String str = sb.toString();
                str = str.substring(0, str.length() - 2);
                System.out.println(str);


                //default Result[] get(List<Get> gets)
                //将要获取的数据封装成Get对象并添加到集合中
//                BufferedReader br = new BufferedReader(new FileReader("data/student.txt"));
//                String line = null;
//                while ((line=br.readLine())!=null){
//                    String[] split = line.split(",");
//                    Get get = new Get(Bytes.toBytes(split[0]));
//                    list.add(get);
//                }
//
//
//                Result[] results = students.get(list);
//                for (Result result : results) {
//                    //获取行键
//                    String id = Bytes.toString(result.getRow());
//
//                    //获取方式一
//                    //根据列簇列名获取列值
//                    //public byte[] getValue(byte[] family, byte[] qualifier)
//                    String name = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
//                    String age = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age")));
//                    String sex = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("sex")));
//                    String clazz = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("clazz")));
//
//                    System.out.println("学号:"+id+", 姓名:"+name+", 年龄:"+age+", 性别:"+age+", 班级:"+clazz);
//                }
//
//                br.close();

            } else {
                System.out.println(tn + "表不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //6、取批量列数据

    /**
     * scan 'tableName'
     */
    @Test
    public void getMoreData(){
        try {
            //将表明封装成对象
            TableName tn = TableName.valueOf("students");

            //判断表是否存在
            boolean b = admin.tableExists(tn);
            if (b) {
                //获取表对象
                Table students = conn.getTable(tn);
                //创建全表扫描对象
                Scan scan = new Scan();//无参构造方法默认全表扫描

                ResultScanner results = students.getScanner(scan);//results中有多行数据

                //获取数据组成的迭代器
                //遍历迭代器
                for (Result result : results) {
                    String id = Bytes.toString(result.getRow());
                    //根据列簇列名获取列值
                    //public byte[] getValue(byte[] family, byte[] qualifier)
                    String name = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
                    String age = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age")));
                    String sex = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("sex")));
                    String clazz = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("clazz")));

                    System.out.println("学号:" + id + ", 姓名:" + name + ", 年龄:" + age + ", 性别:" + age + ", 班级:" + clazz);
                }

            } else {
                System.out.println(tn + "表不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //7、创建预分region表
    @Test
    public void createPreviewTable(){
        try {
            //将表明封装成对象
            TableName tn = TableName.valueOf("students2");

            //判断表是否存在
            boolean b = admin.tableExists(tn);
            if (!b) {
                //创建表描述器对象
                TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tn);

                //创建列簇描述器对象
                ColumnFamilyDescriptor info = ColumnFamilyDescriptorBuilder.of(Bytes.toBytes("info"));

                //添加列簇描述器对象到表描述器对象中
                //添加一个列簇
                //TableDescriptorBuilder setColumnFamily(ColumnFamilyDescriptor family)
                tableDescriptorBuilder.setColumnFamily(info);

                //添加多个列簇
                //TableDescriptorBuilder setColumnFamilies(Collection<ColumnFamilyDescriptor> families)

                //创建普通的表
//                admin.createTable(tableDescriptorBuilder.build());

                byte[][] splitKeys = {
                        Bytes.toBytes("e"),
                        Bytes.toBytes("h"),
                        Bytes.toBytes("l"),
                        Bytes.toBytes("o"),
                        Bytes.toBytes("t"),
                };

                //创建预分表
                //void createTable(TableDescriptor desc, byte[][] splitKeys)
                admin.createTable(tableDescriptorBuilder.build(),splitKeys);

            } else {
                System.out.println(tn + "表已存在");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    @After
    public void close(){
        if (admin!=null){
            try {
                admin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (conn!=null){
            try {
                conn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
posted @ 2024-11-20 16:04  w我自横刀向天笑  阅读(14)  评论(0编辑  收藏  举报