Java对HBase的CRUD操作的小demo

本文提供了一个使用Java操作HBase数据库的示例,包括创建表、插入数据、获取单行数据和扫描数据等基本操作。示例代码展示了如何通过HBaseAdmin和HTable接口进行表的管理,并使用Put和Get类进行数据的写入和读取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


public class HBaseDemo {
    private String tn = "teacher";
    HBaseAdmin admin = null;
    HTable table = null;
    @Before
    public void init() throws Exception {
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum","master,slave1,slave2,slave3,slave4,slave5,slave6,slave7");
        admin = new HBaseAdmin(conf);
        table = new HTable(conf,tn.getBytes());
    }

    @Test
    public void createTable() throws Exception {
        HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tn));
        HColumnDescriptor addFamily = new HColumnDescriptor("teacherInfo".getBytes());
        desc.addFamily(addFamily);
        if(admin.tableExists(tn)){
            admin.disableTable(tn);
            admin.deleteTable(tn);
        }
        admin.createTable(desc);
    }

    @Test
    public void insertHBase() throws Exception {
//        String rowKey = "110101";
//        Put put = new Put(rowKey.getBytes());
//        put.addColumn("teacherInfo".getBytes(),"name".getBytes(),"XieDong".getBytes());
//        put.addColumn("teacherInfo".getBytes(),"age".getBytes(),"26".getBytes());
//        put.addColumn("teacherInfo".getBytes(),"gender".getBytes(),"Male".getBytes());
//        put.addColumn("teacherInfo".getBytes(),"cat".getBytes(),"Senior".getBytes());
//        put.addColumn("teacherInfo".getBytes(),"tag".getBytes(),"Chemistry".getBytes());
//        put.addColumn("teacherInfo".getBytes(),"level".getBytes(),"S".getBytes());
        String rowKey = "110102";
        Put put = new Put(rowKey.getBytes());
        put.addColumn("teacherInfo".getBytes(),"name".getBytes(),"DongSao".getBytes());
        put.addColumn("teacherInfo".getBytes(),"age".getBytes(),"24".getBytes());
        put.addColumn("teacherInfo".getBytes(),"gender".getBytes(),"Female".getBytes());
        put.addColumn("teacherInfo".getBytes(),"cat".getBytes(),"Junior".getBytes());
        put.addColumn("teacherInfo".getBytes(),"tag".getBytes(),"Chinese".getBytes());
        put.addColumn("teacherInfo".getBytes(),"level".getBytes(),"M".getBytes());
        table.put(put);
    }

    @Test
    public void get() throws Exception {
        String rowKey = "110101";
        Get get = new Get(rowKey.getBytes());
        //指定要查询的列,选加,不加类似于select *
        get.addColumn("teacherInfo".getBytes(),"name".getBytes());
        get.addColumn("teacherInfo".getBytes(),"age".getBytes());
        get.addColumn("teacherInfo".getBytes(),"gender".getBytes());
        get.addColumn("teacherInfo".getBytes(),"cat".getBytes());
        get.addColumn("teacherInfo".getBytes(),"tag".getBytes());
        get.addColumn("teacherInfo".getBytes(),"level".getBytes());
        Result result = table.get(get);
        Cell cell1 = result.getColumnLatestCell("teacherInfo".getBytes(), "name".getBytes());
        Cell cell2 = result.getColumnLatestCell("teacherInfo".getBytes(), "age".getBytes());
        Cell cell3 = result.getColumnLatestCell("teacherInfo".getBytes(), "gender".getBytes());
        Cell cell4 = result.getColumnLatestCell("teacherInfo".getBytes(), "cat".getBytes());
        Cell cell5 = result.getColumnLatestCell("teacherInfo".getBytes(), "tag".getBytes());
        Cell cell6 = result.getColumnLatestCell("teacherInfo".getBytes(), "level".getBytes());
        //过时
//        System.out.println(new String(cell1.getValue()));
//        System.out.println(new String(cell2.getValue()));
//        System.out.println(new String(cell3.getValue()));
//        System.out.println(new String(cell4.getValue()));
//        System.out.println(new String(cell5.getValue()));
//        System.out.println(new String(cell6.getValue()));
        System.out.println(new String(CellUtil.cloneValue(cell1)));
        System.out.println(new String(CellUtil.cloneValue(cell2)));
        System.out.println(new String(CellUtil.cloneValue(cell3)));
        System.out.println(new String(CellUtil.cloneValue(cell4)));
        System.out.println(new String(CellUtil.cloneValue(cell5)));
        System.out.println(new String(CellUtil.cloneValue(cell6)));
    }

    @Test
    public void scan() throws Exception {
        Scan scan = new Scan();
        scan.setStartRow("110101".getBytes());
        scan.setStopRow("110101".getBytes());
        ResultScanner results = table.getScanner(scan);
        for(Result result:results){
            Cell cell1 = result.getColumnLatestCell("teacherInfo".getBytes(), "name".getBytes());
            Cell cell2 = result.getColumnLatestCell("teacherInfo".getBytes(), "age".getBytes());
            Cell cell3 = result.getColumnLatestCell("teacherInfo".getBytes(), "gender".getBytes());
            Cell cell4 = result.getColumnLatestCell("teacherInfo".getBytes(), "cat".getBytes());
            Cell cell5 = result.getColumnLatestCell("teacherInfo".getBytes(), "tag".getBytes());
            Cell cell6 = result.getColumnLatestCell("teacherInfo".getBytes(), "level".getBytes());
            System.out.println(new String(CellUtil.cloneValue(cell1)));
            System.out.println(new String(CellUtil.cloneValue(cell2)));
            System.out.println(new String(CellUtil.cloneValue(cell3)));
            System.out.println(new String(CellUtil.cloneValue(cell4)));
            System.out.println(new String(CellUtil.cloneValue(cell5)));
            System.out.println(new String(CellUtil.cloneValue(cell6)));
        }
    }

    @After
    public void close() throws Exception {
        if(admin != null){
            admin.close();
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值