HbaseAPI

一、建立连接,创建表
public class Demo01Test {
    //使用javaAPI创建表
    public static void main(String[] args) throws IOException {
        //创建配置对象
        Configuration conf = HBaseConfiguration.create();
        //指定zk集群地址
        conf.set("hbase.zookeeper.quorum","master:2181,node1:2181,node2:2181");

        //todo 先创建与Hbase的连接
        Connection conn = ConnectionFactory.createConnection(conf);

        //todo 创建一个Admin对象
        //通过Admin对表进行操作
        Admin admin = conn.getAdmin();

        //todo 创建HTableDescriptor对象
        //通过HTableDescriptor对象可以对列簇进行操作
        HTableDescriptor testAPI = new HTableDescriptor(TableName.valueOf("TestJavaAPI"));

        //todo 添加ColumnDescriptor对象
        //通过ColumnDescriptor对象对列簇进行一些配置
        HColumnDescriptor cf1 = new HColumnDescriptor("cf1");
        //todo 设置版本号
        cf1.setMaxVersions(5);
        //todo 设置存活时间s
        cf1.setTimeToLive(5);

        //todo 添加列簇
        testAPI.addFamily(cf1);
        //todo 创建表
        admin.createTable(testAPI);
        //关闭连接
        admin.close();
        conn.close();
    }

二、一些常用的方法
public class Demo02API {
    //todo 初始化Admin对象
    Admin admin=null;
    Connection conn = null;
    @Before
    public void init(){
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","master:2181,node1:2181,node2:2181");
        try {
            conn = ConnectionFactory.createConnection(conf);
            admin = conn.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void createTable() throws Exception{
        HTableDescriptor test2 = new HTableDescriptor(TableName.valueOf("test2"));
        HColumnDescriptor cf1 = new HColumnDescriptor("cf1");
        cf1.setMaxVersions(3);
        test2.addFamily(cf1);
        admin.createTable(test2);
    }
    //todo 列出所有的表
    @Test
    public void listTable()throws Exception{
        TableName[] tableNames = admin.listTableNames();
        for (TableName tableName : tableNames) {
            System.out.println(tableName);
        }
    }


    @Test
    public void dropTable() throws Exception{
        //todo 删除表之前应该先关闭表
        TableName table = TableName.valueOf("test001");
        admin.disableTable(table);
        admin.deleteTable(table);
    }

    @Test
    public void modifyTable() throws Exception{
        TableName table2 = TableName.valueOf("test2");
        //HTableDescriptor hTableDescriptor = new HTableDescriptor(table2);
        //todo 通过admin对象去创建HTableDescriptor对象,如果直接创建那么就不会获取原有的表数据
        HTableDescriptor hTableDescriptor = admin.getTableDescriptor(table2);
//        for (HColumnDescriptor hColumnDescriptor : hTableDescriptor.getColumnFamilies()) {
            //todo 遍历获取然后添加
            hTableDescriptor.addFamily(hColumnDescriptor);
        }
        //todo 添加列簇
        hTableDescriptor.addFamily(new HColumnDescriptor("cf2"));
        admin.modifyTable(table2,hTableDescriptor);
    }

    @Test
    public void put() throws Exception{
        Table test2 = conn.getTable(TableName.valueOf("test2"));
        Put put = new Put("003".getBytes());
        put.addColumn("cf1".getBytes(),"name".getBytes(),"王五".getBytes());
        put.addColumn("cf1".getBytes(),"age".getBytes(), Bytes.toBytes(22));
        put.addColumn("cf1".getBytes(),"gender".getBytes(),"man".getBytes());
        put.addColumn("cf2".getBytes(),"clazz".getBytes(),"文科三班".getBytes());
        test2.put(put);

    }
    //todo 通过rk去获取一条数据
    @Test
    public void get()throws Exception{
        Table test2 = conn.getTable(TableName.valueOf("test2"));
        Get get = new Get("001".getBytes());
        Result rs = test2.get(get);
        byte[] cf1 = "cf1".getBytes();
        byte[] cf2 = "cf2".getBytes();
        String name = Bytes.toString(rs.getValue(cf1, "name".getBytes()));
        int age = Bytes.toInt(rs.getValue(cf1, "age".getBytes()));
        String gender = Bytes.toString(rs.getValue(cf1, "gender".getBytes()));
        String clazz = Bytes.toString(rs.getValue(cf2, "clazz".getBytes()));
        System.out.println(name+","+age+","+gender+","+clazz);

    }

    //todo 根据rk指定的范围进行scan
    @Test
    public void scan()throws Exception{
        Table test2 = conn.getTable(TableName.valueOf("test2"));
        Scan scan = new Scan();
        scan.withStartRow("001".getBytes());
        scan.withStopRow("004".getBytes());
        ResultScanner scanner = test2.getScanner(scan);
        for (Result rs : scanner) {
            //todo 获取rowkey
            String rowkey = Bytes.toString(rs.getRow());
            if("001".equals(rowkey) || "002".equals(rowkey)){
                byte[] cf1 = "cf1".getBytes();
                byte[] cf2 = "cf2".getBytes();
                String name = Bytes.toString(rs.getValue(cf1, "name".getBytes()));
                int age = Bytes.toInt(rs.getValue(cf1, "age".getBytes()));
                String gender = Bytes.toString(rs.getValue(cf1, "gender".getBytes()));
                String clazz = Bytes.toString(rs.getValue(cf2, "clazz".getBytes()));
                System.out.println(name+","+age+","+gender+","+clazz);
            }
           else if("003".equals(rowkey)){
                byte[] cf1 = "cf1".getBytes();
                byte[] cf2 = "cf2".getBytes();
                String name = Bytes.toString(rs.getValue(cf1, "name".getBytes()));
                String clazz = Bytes.toString(rs.getValue(cf2, "clazz".getBytes()));
                System.out.println(name+","+clazz);
            }
        }
    }

    //todo 很明显上面的方法不太现实获取大量数据
    //todo 所以hbase里有一个专门的方法获取大量数据:cellUtil
    @Test
    public void cellUtil()throws Exception{
        Table test2 = conn.getTable(TableName.valueOf("test2"));
        Scan scan = new Scan();
        scan.withStartRow("001".getBytes());
        scan.withStopRow("004".getBytes());
        ResultScanner scanner = test2.getScanner(scan);
        for (Result rs : scanner) {
            //todo 获取当前rowkey对应的这条数据的所有cell
            List<Cell> columnCells = rs.listCells();
            for (Cell cell : columnCells) {
                String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
                String cf = Bytes.toString(CellUtil.cloneFamily(cell));
                String qf = Bytes.toString(CellUtil.cloneQualifier(cell));
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.println("rowkey: "+rowkey+" columnFamily: "+cf+" Qualifier: "+qf+" value: "+value);

            }
        }
    }
    //todo 将学生数据逐条写入表格
    @Test
    public void putAll()throws Exception{
        //先创建一个学生表
        TableName students = TableName.valueOf("students");
        if(!admin.tableExists(students)){
            HTableDescriptor hTableDescriptor = new HTableDescriptor(students);
            HColumnDescriptor info = new HColumnDescriptor("info");
            hTableDescriptor.addFamily(info);
            admin.createTable(hTableDescriptor);
        }
        Table table = conn.getTable(students);
        //todo 数据的读写流 将学生数据获取再写入
        BufferedReader bf = new BufferedReader(new FileReader("data/students.txt"));
        String line = null;

        while ((line = bf.readLine()) != null){
            String[] s = line.split(",");
            String id = s[0];
            //todo id作为rowkey其他的作为列值
            String name = s[1];
            String age = s[2];
            String gender = s[3];
            String clazz = s[4];
            Put put = new Put(id.getBytes());
            byte[] info = "info".getBytes();
            put.addColumn(info,"name".getBytes(),name.getBytes());
            put.addColumn(info,"age".getBytes(),age.getBytes());
            put.addColumn(info,"gender".getBytes(),gender.getBytes());
            put.addColumn(info,"clazz".getBytes(),clazz.getBytes());
            table.put(put);
        }
    }

    //todo 获取所有数据
    @Test
    public void getAll() throws Exception{
        Table students = conn.getTable(TableName.valueOf("students"));
        Scan scan = new Scan();
        ResultScanner scanner = students.getScanner(scan);
        for (Result rs : scanner) {
            String rowkey = Bytes.toString(rs.getRow());
            System.out.print(rowkey + " ");
            //todo rs表示一条数据
            List<Cell> cells = rs.listCells();
            for (Cell cell : cells) {
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.print(value + " ");
            }
            System.out.println(" ");
        }
    }

    @Test
    //todo 获取最新10个位置的数据
    //todo 如果只需要根据rowkey获取一条记录,那么直接用get就行
    //todo 如果想获取多条记录 那么就需要用到scan
    //todo 如果一条数据有多个版本,则需要使用CellUtil及每个Result对象的listCells方法一起获取
    public void loadData()throws Exception{
        TableName students = TableName.valueOf("students");
        Table table = conn.getTable(students);
        String rowkey = "1500100485";
        System.out.print(rowkey + " ");
        Get get = new Get(rowkey.getBytes());
//        //todo 设置获取多少个版本的数据
//        get.setMaxVersions(10);
        Result rs = table.get(get);
        for (Cell cell : rs.listCells()) {
            String value = Bytes.toString(CellUtil.cloneValue(cell));
            System.out.print(value + " ");
        }
        System.out.println(" ");
    }

    //todo 删除数据
    @Test
    public void delet()throws Exception{
        Table test2 = conn.getTable(TableName.valueOf("test2"));
        Delete delete = new Delete("001".getBytes());
        test2.delete(delete);
    }

    @After
    //todo 关闭连接
    public void close(){

        try {
            conn.close();
            admin.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值