HBase API操作

HBase API操作

环境准备

新建项目后在pom.xml中添加依赖:


    org.apache.hbase
    hbase-server
    1.3.1



    org.apache.hbase
    hbase-client
    1.3.1



HBaseAPI

测试代码

package com.dev1.demo01;


public class TestHBase {
     
    @Test
    public void test01() throws Exception {
     
        //需要定义一个工具类,调用它的静态方法,等价于一个shell指令
        Configuration conf = HBaseUtil.getConf();
        System.out.println(conf);
        //判断一张表是否存在
        boolean flag = HBaseUtil.isExist("t2");
        System.out.println(flag);
        //先调用再实现
        HBaseUtil.createTable("student","info");
        //删除表
        //HBaseUtil.dropTable("student");
    }
    @Test
    public void test02() throws Exception {
     
        HBaseUtil.putRow("student","10002","info","name","rose");
    }
    @Test
    public void test03() throws Exception {
     
        HBaseUtil.deleteRow("student","10001");
    }
    @Test
    public void test04() throws Exception {
     
        HBaseUtil.printRows("student");
    }
    @Test
    public void test05() throws Exception {
     
        HBaseUtil.printOneRow("student","10001");
        HBaseUtil.printOneRow("student","10002");
    }
    @Test
    public void test06() throws Exception {
     
        HBaseUtil.printOneRow2("student","10001","info","age");


    }
    @Test
    public void test07() throws Exception {
     


    }
}

工具类代码

package com.dev1.util;

public class HBaseUtil {
     
    //配置信息hdfs  core-site.xml  hdfs-site.xml
    public static Configuration conf;
    public static  Connection connection;
    static{
     
        //使用HBaseConfiguration的单例方法实例化
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.26.102");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        try {
     
            connection = ConnectionFactory.createConnection(conf);//返回一个可以使用的连接
        } catch (IOException e) {
     
            e.printStackTrace();
        }

    }

    public static Configuration getConf() {
     
        return conf;
    }
    //判断一个表是否存在
    public static boolean isExist(String tableName)throws Exception{
     
        //HBaseAdmin admin = new HBaseAdmin(conf);或时
        Admin admin = connection.getAdmin(); //获取表的管理对象,创建,删表,查表
        return admin.tableExists(TableName.valueOf(tableName));
    }

    public static void createTable(String tableName, String... columnFamily) throws Exception {
     
        //管理表
        //HBaseAdmin admin = new HBaseAdmin(conf);
        Admin admin = connection.getAdmin(); //获取表的管理对象,创建,删表,查表
        //判断表是否存在,如果不存在才创建
        if(!isExist(tableName)){
     
            //创建表   表的描述  表的元数据
            HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
            //因为可能存在多个列,所以使用循环
            for(String column:columnFamily){
     
                //列族的元数据
                HColumnDescriptor hcd = new HColumnDescriptor(column);
                desc.addFamily(hcd);
            }
            admin.createTable(desc);
        }
    }
    //删除表,必须先禁用表,然后才能删除表
    public static void dropTable(String tableName) throws Exception{
     
//        HBaseAdmin admin = new HBaseAdmin(conf);
        Admin admin = connection.getAdmin(); //获取表的管理对象,创建,删表,查表
        //如果表存在,我们才能进行删除
        if(isExist(tableName)){
     
            //禁用
            admin.disableTable(TableName.valueOf(tableName));
            //删除
            admin.deleteTable(TableName.valueOf(tableName));
        }
    }

    public static void putRow(String tableName, String rowKey, String columnFamily, String name, String value) throws Exception {
     
        //创建HTable对象
        //HTable hTable = new HTable(conf, tableName);
        Table hTable = connection.getTable(TableName.valueOf(tableName));
        //向表中插入数据
        Put put = new Put(Bytes.toBytes(rowKey));
        //向Put对象中组装数据
        put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(name), Bytes.toBytes(value));
        hTable.put(put);
        hTable.close();
    }

    public static void deleteRow(String tableName, String...rows) throws Exception {
     
//        HTable hTable = new HTable(conf, tableName);
        Table hTable = connection.getTable(TableName.valueOf(tableName));
        List<Delete> deleteList = new ArrayList<Delete>();
        for(String row : rows){
     
            Delete delete = new Delete(Bytes.toBytes(row));
            deleteList.add(delete);
        }
        hTable.delete(deleteList);
        hTable.close();
    }

    public static void printRows(String tableName)throws Exception {
     
        //select * from student
        //scan 'student'
        //HTable hTable = new HTable(conf, tableName);
        Table hTable = connection.getTable(TableName.valueOf(tableName));
        //创建扫描器
        Scan scan = new Scan();
        //扫描  代表查询的结果  ResultScanner
        ResultScanner results = hTable.getScanner(scan);
        for(Result result:results){
     
            //Result相当于一个行
            print(result);
        }
    }
    //打印一行的代码是一样的,只需要传入表示一行的Result对象 就可以
    //不使用复制代码,而使用封装方法
    private static void print(Result result) {
     
        //Cell当前行的一个单元格
        Cell[] cells = result.rawCells();

        for(Cell cell:cells){
     
            //打印 行键
            System.out.print(Bytes.toString(CellUtil.cloneRow(cell))+" | ");
            //列族
            System.out.print(Bytes.toString(CellUtil.cloneFamily(cell))+" | ");
            //列标识
            System.out.print(Bytes.toString(CellUtil.cloneQualifier(cell))+" | ");
            //值
            System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
            System.out.println("-----------------");
        }

    }

    public static void printOneRow(String tableName, String rowkey) throws Exception{
     
        HTable hTable = new HTable(conf, tableName);
        //get 'student','10001'
        //Get对象表示查询一条记录
        Get  get = new Get(Bytes.toBytes(rowkey));

        Result result = hTable.get(get);
        print(result);

    }

    public static void printOneRow2(String tableName, String rowKey, String family, String column) throws Exception{
     
        HTable hTable = new HTable(conf, tableName);
        //get 'student','1001','info:name'
        //Get对象表示查询一条记录
        Get  get = new Get(Bytes.toBytes(rowKey));
        //指定列族下的一个列的值
        get.addColumn(Bytes.toBytes(family),Bytes.toBytes(column));
        Result result = hTable.get(get);
        print(result);
    }
}

你可能感兴趣的:(hbase)