Hbase 的API调用

Hbase API 类和数据模型之间的对应关系

在这里插入图片描述

HBaseAdmin

HBaseAdmin提供了一个接口来管理 HBase 数据库的表信息。它提供的方法包括:创建表,删 除表,列出表项,使表有效或无效,以及添加或删除表列族成员等。
在这里插入图片描述

HBaseConfiguration

HBaseConfiguration的作用:对 HBase 进行配置。
在这里插入图片描述

HTableDescriptor

HTableDescriptor作用:包含了表的名字极其对应表的列族。
在这里插入图片描述

HColumnDescriptor

HColumnDescriptor作用:维护着关于列族的信息,例如版本号,压缩设置等。它通常在创建表或者为表添 加列族的时候使用。列族被创建后不能直接修改,只能通过删除然后重新创建的方式。列族被删除的时候,列族里面的数据也会同时被删除。
在这里插入图片描述
其他的可以去参考官网API文档

Java代码示例

package hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HbaseTest {

    private HBaseAdmin admin;
    HConnection connection;

    /**
     * 执行Test之前执行的代码块
     */
    @Before
    public void init() {
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum", "master:2181,node1:2181,node2:2181");
        try {
            /**
             * 创建admin对象,和HMaster建立连接,执行创建,删除,修改表的操作
             *
             */
            admin = new HBaseAdmin(conf);

            //创建zookeeper连接
            connection = HConnectionManager.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void createTable() {
        try {
            //创建表的描述对象
            HTableDescriptor tableDescriptor = new HTableDescriptor("student");
            //增加列簇
            HColumnDescriptor columnDescriptor = new HColumnDescriptor("info");
            //设置列簇的版本数量
            columnDescriptor.setMaxVersions(5);
            tableDescriptor.addFamily(columnDescriptor);
            //判断表是否存在
            if (admin.tableExists("student")) {
                System.out.println("表已存在");
                return;
            }
            //执行建表操作
            admin.createTable(tableDescriptor);

            //判断表是否存在
            if (admin.tableExists("student")) {
                System.out.println("建表成功");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void deleteTable() {
        try {

            //获取表的描述信息
            HTableDescriptor tableDescriptor = admin.getTableDescriptor(TableName.valueOf("student"));
            HColumnDescriptor[] columnFamilies = tableDescriptor.getColumnFamilies();
            for (HColumnDescriptor columnFamily : columnFamilies) {
                //获取列簇的名称
                //Bytes  hbase提供的把字节数组转换成具体类型的工具
                String name = Bytes.toString(columnFamily.getName());
                System.out.println("列簇名:" + name);
                int maxVersions = columnFamily.getMaxVersions();
                System.out.println("版本数:" + maxVersions);
            }

            /**
             * 删除表之前先进行disable
             *
             */
            admin.disableTable("student");
            //删除表
            admin.deleteTable("student");

            if (!admin.tableExists("student")) {
                System.out.println("表以删除");
            }

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


    /**
     * 插入一条数据
     */
    @Test
    public void insert() {
        HTableInterface table = null;
        try {
            //获取表对象
            table = connection.getTable("student");
            //创建put对象指向rowkey
            Put put = new Put("001".getBytes());
            put.add("info".getBytes(), "name".getBytes(), "张三".getBytes());
            put.add("info".getBytes(), "age".getBytes(), Bytes.toBytes(23));
            put.add("info".getBytes(), "clazz".getBytes(), "文科一班".getBytes());
            put.add("info".getBytes(), "clazz".getBytes(), "文科一班".getBytes());
            put.add("info".getBytes(), "gender".getBytes(), "男".getBytes());

            //插入一行数据数据
            table.put(put);

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


    /**
     * 一次插入多行数据
     */
    @Test
    public void insertAll() {

        HTableInterface table = null;
        try {
            table = connection.getTable("student");
            //加载磁盘上的数据
            ArrayList<Student> students = DataUtil.load("E:\\bigdata\\bigdata\\data\\students.txt", Student.class);

            ArrayList<Put> puts = new ArrayList<Put>();

            for (Student student : students) {
                byte[] rowkey = student.getId().getBytes();

                Put put = new Put(rowkey);
                put.add("info".getBytes(), "age".getBytes(), Bytes.toBytes(student.getAge()));
                put.add("info".getBytes(), "name".getBytes(), Bytes.toBytes(student.getName()));
                put.add("info".getBytes(), "clazz".getBytes(), Bytes.toBytes(student.getClazz()));
                put.add("info".getBytes(), "gender".getBytes(), Bytes.toBytes(student.getGender()));
                puts.add(put);
            }

            //插入多行数据
            table.put(puts);


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

    }


    @Test
    public void query() {
        HTableInterface table = null;
        try {
            table = connection.getTable("student");
            Get get = new Get("1500100007".getBytes());
            //执行查询返回结果
            Result result = table.get(get);
            //获取所有列
            List<Cell> cells = result.listCells();

            //遍历每一个单元格
            for (Cell cell : cells) {
                String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));

                if ("age".equals(qualifier)) {
                    Integer value = Bytes.toInt(CellUtil.cloneValue(cell));
                    System.out.println(qualifier + ":" + value);
                } else {
                    String value = Bytes.toString(CellUtil.cloneValue(cell));
                    System.out.println(qualifier + ":" + value);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (table != null) {
                try {
                    table.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    @Test
    public void scanner() {
        HTableInterface table = null;
        try {
            table = connection.getTable("student");

            //创建扫描器对象
            Scan scan = new Scan();

            //指定列簇扫描
            scan.addFamily("info".getBytes());

            //执行送秒操作,返回多行结果
            ResultScanner results = table.getScanner(scan);

            Result line;
            //迭代结果集合
            while ((line = results.next()) != null) {
                //获取这一行的所有列
                List<Cell> cells = line.listCells();
                //遍历每一个单元格
                for (Cell cell : cells) {

                    //获取rowkey
                    String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
                    System.out.print(rowkey+"\t");
                    String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
                    if ("age".equals(qualifier)) {
                        Integer value = Bytes.toInt(CellUtil.cloneValue(cell));
                        System.out.print(qualifier + ":" + value + "\t");
                    } else {
                        String value = Bytes.toString(CellUtil.cloneValue(cell));
                        System.out.print(qualifier + ":" + value + "\t");
                    }
                }

                System.out.println();
            }


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


    @Test
    /**
     * 查询文科班的所有学生
     *
     */
    public void queryWenke(){
        HTableInterface table = null;
        try {
            table = connection.getTable("student");

            //创建扫描器对象
            Scan scan = new Scan();

            //指定列簇扫描
            scan.addFamily("info".getBytes());


            //增加过滤器,过滤文科班的学生
            RegexStringComparator regexStringComparator = new RegexStringComparator("文科");
            SingleColumnValueFilter columnValueFilter = new SingleColumnValueFilter("info".getBytes(), "clazz".getBytes(), CompareFilter.CompareOp.EQUAL, regexStringComparator);

            SubstringComparator comp = new SubstringComparator("男");
            SingleColumnValueFilter columnValueFilter1 = new SingleColumnValueFilter("info".getBytes(), "gender".getBytes(), CompareFilter.CompareOp.EQUAL, comp);

            //过滤器集合
            FilterList fl = new FilterList(FilterList.Operator.MUST_PASS_ALL);

            //前缀过滤
            BinaryPrefixComparator prefixComparator = new BinaryPrefixComparator(Bytes.toBytes("吕"));

            SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("name"),CompareFilter.CompareOp.EQUAL, prefixComparator);

            //过滤文科班的学生
            fl.addFilter(columnValueFilter);

            //过滤性别为男的学生
            fl.addFilter(columnValueFilter1);

            //增加前缀过滤
            fl.addFilter(filter);

            scan.setFilter(fl);
            //执行送秒操作,返回多行结果
            ResultScanner results = table.getScanner(scan);

            Result line;
            //迭代结果集合
            while ((line = results.next()) != null) {
                //获取这一行的所有列
                List<Cell> cells = line.listCells();
                //遍历每一个单元格
                for (Cell cell : cells) {
                    //获取rowkey
                    String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
                    System.out.print(rowkey+"\t");
                    String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
                    if ("age".equals(qualifier)) {
                        Integer value = Bytes.toInt(CellUtil.cloneValue(cell));
                        System.out.print(qualifier + ":" + value + "\t");
                    } else {
                        String value = Bytes.toString(CellUtil.cloneValue(cell));
                        System.out.print(qualifier + ":" + value + "\t");
                    }
                }
                System.out.println();
            }

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


    /**
     * 行键过滤器
     *
     */
    @Test
    public void rowFilter() {
        HTableInterface table = null;
        try {
            table = connection.getTable("student");

            //创建扫描器对象
            Scan scan = new Scan();

            //指定列簇扫描
            scan.addFamily("info".getBytes());


            /**
             * 设置开始key 和结束key
             *
             */
            /*scan.setStartRow("1500100199".getBytes());
            scan.setStopRow("1500100253".getBytes());*/


            /**
             * rowkey前缀过滤
             *
             */
            BinaryPrefixComparator binaryPrefixComparator = new BinaryPrefixComparator("15001002".getBytes());
            RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, binaryPrefixComparator);

            scan.setFilter(rowFilter);

            //执行送秒操作,返回多行结果
            ResultScanner results = table.getScanner(scan);

            Result line;
            //迭代结果集合
            while ((line = results.next()) != null) {
                //获取这一行的所有列
                List<Cell> cells = line.listCells();
                //遍历每一个单元格
                for (Cell cell : cells) {
                    //获取rowkey
                    String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
                    System.out.print(rowkey + "\t");
                    String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
                    if ("age".equals(qualifier)) {
                        Integer value = Bytes.toInt(CellUtil.cloneValue(cell));
                        System.out.print(qualifier + ":" + value + "\t");
                    } else {
                        String value = Bytes.toString(CellUtil.cloneValue(cell));
                        System.out.print(qualifier + ":" + value + "\t");
                    }
                }
                System.out.println();
            }

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



            /**
             * 最后执行
             */

    @After
    public void close() {
        System.out.println("回收资源");
        if (admin != null) {
            try {
                admin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

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


}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值