JavaAPI如何操作HBase完成CRUD

精选30+云产品,助力企业轻松上云!>>> hot3.png

1.只需要引入HBase的客户端依赖即可。

        <!--Hbase客户端-->
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.2.1</version>
        </dependency>

2.HBaseUtils.java 工具类抽取

public class HBaseUtils {

    private static Configuration configuration;
    // 1.获取配置对象
    static {
        configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum","hadoop1:2181,hadoop2:2181,hadoop3:2181");
    }

    // 2.获取连接对象
    public static Admin getAdmin() throws IOException {
        Connection connection = ConnectionFactory.createConnection(configuration);
        Admin admin = connection.getAdmin();
        return admin;
    }


    /*获取table*/
    public static Table getTable() throws IOException {
        return getTable("user_info");
    }

    public static Table getTable(String tablename) throws IOException {
        Connection connection = ConnectionFactory.createConnection(configuration);
        return connection.getTable(TableName.valueOf(tablename));
    }

    /*关闭table*/
    public static void close(Table table) throws IOException {
        if (table != null){
            table.close();
        }
    }
    // 3.释放admin
    public static void close(Admin admin) throws IOException {
        admin.close();
    }

    public static void close(Admin admin,Table table) throws IOException {
        close(admin);
        close(table);
    }
}

3.DDL 操作

public class TableDDL {

    private HBaseAdmin admin;

    @Before
    public void before() throws IOException {
        admin = (HBaseAdmin) HBaseUtils.getAdmin();
    }

    /*创建表*/
    @Test
    public void createTable() throws IOException {
        // 1.创建表描述器对象
        HTableDescriptor ht = new HTableDescriptor(TableName.valueOf("user_info"));
        // 2.添加列簇
        HColumnDescriptor familyColumn1 = new HColumnDescriptor("base_info");
        HColumnDescriptor familyColumn2 = new HColumnDescriptor("extra_info");
        ht.addFamily(familyColumn1);
        ht.addFamily(familyColumn2);
        admin.createTable(ht);
    }

    /*删除表*/
    @Test
    public void deleteTable() throws IOException {
        TableName tableName = TableName.valueOf("user_info");
        HTableDescriptor user_info = new HTableDescriptor(tableName);
        if (!admin.isTableDisabled(tableName)){
            admin.disableTable(tableName);
        }
        admin.deleteTable(tableName);
    }


    /*修改表*/
    @Test
    public void modifyTable() throws IOException {
        TableName tableName = TableName.valueOf("user_info");
        HTableDescriptor user_info = admin.getTableDescriptor(tableName);
        // 2.添加列簇
        HColumnDescriptor familyColumn1 = new HColumnDescriptor("base_info2");
        HColumnDescriptor familyColumn2 = new HColumnDescriptor("extra_info2");
        user_info.addFamily(familyColumn1);
        user_info.addFamily(familyColumn2);
        admin.modifyTable(tableName,user_info);
    }

    /*查询所有的列簇*/
    @Test
    public void listAllFamily() throws IOException {
        //查询所有的列簇
        HTableDescriptor user_info = admin.getTableDescriptor(TableName.valueOf("user_info"));
        HColumnDescriptor[] columnFamilies = user_info.getColumnFamilies();
        for (HColumnDescriptor ht : columnFamilies){
            System.out.println(ht.getNameAsString());
        }
    }

    /*删除一个列簇*/
    @Test
    public void removeFamily() throws IOException {
        TableName tableName = TableName.valueOf("user_info");
        HTableDescriptor user_info = admin.getTableDescriptor(tableName);
        //删除
        //user_info.removeFamily(Bytes.toBytes("extra_info"));
        //提交修改
        //admin.modifyTable(tableName,user_info);
        admin.deleteColumn(tableName,Bytes.toBytes("extra_info"));
    }



    @After
    public void after() throws IOException {
        HBaseUtils.close(admin);
    }
}

4.DML 操作

public class TableDML {

    private Table table;

    @Before
    public void before() throws IOException {
        table = HBaseUtils.getTable();
    }

    /*插入一条记录
    * put 'ns1:t1','r1','c1','value'
    * 批量插入的话,直接放入list集合即可
    * */
    @Test
    public void put() throws IOException {
        // 1.创建put对象
        Put put = new Put(Bytes.toBytes("001")); //行建
        // 2.添加列数据
        put.addColumn(Bytes.toBytes("base_info"),Bytes.toBytes("name"),Bytes.toBytes("wang"));
        table.put(put);
    }

    /*查询一条记录*/
    @Test
    public void get1() throws IOException {
        // 1.创建get对象
        Get get = new Get(Bytes.toBytes("001"));
        Result result = table.get(get);
        NavigableMap<byte[], byte[]> base_info = result.getFamilyMap(Bytes.toBytes("base_info"));
        // 2.便利有序集合
        Set<Map.Entry<byte[], byte[]>> entries = base_info.entrySet();
        for (Map.Entry<byte[], byte[]> entry : entries){
            System.out.println(new String(entry.getKey() + "--->"+new String(entry.getValue())));
        }
    }

    /*查询一条记录*/
    @Test
    public void get2() throws IOException {
        // 1.创建get对象
        Get get = new Get(Bytes.toBytes("001"));
        Result result = table.get(get);
        CellScanner cellScanner = result.cellScanner();
        // 2.遍历
        while (cellScanner.advance()){
            //当前的cell
            Cell cell = cellScanner.current();
            System.out.println(new String(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength()));//列簇
            System.out.println(new String(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()));//列名
            System.out.println(new String(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength()));//列值
        }
    }

    /*查询一条记录*/
    @Test
    public void get3() throws IOException {
        // 1.创建get对象
        Get get = new Get(Bytes.toBytes("001"));
        Result result = table.get(get);
        CellScanner cellScanner = result.cellScanner();
        // 2.遍历
        while (cellScanner.advance()){
            //当前的cell
            Cell cell = cellScanner.current();
            System.out.println(new String(CellUtil.cloneRow(cell)));//行建
            System.out.println(new String(CellUtil.cloneFamily(cell)));
            System.out.println(new String(CellUtil.cloneQualifier(cell)));
            System.out.println(new String(CellUtil.cloneValue(cell)));
        }
    }


    @After
    public void after() throws IOException {
        HBaseUtils.close(table);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coding路人王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值