Java对HBase的CRUD操作的小demo

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();
        }
    }
}