我是靠谱客的博主 整齐宝贝,最近开发中收集的这篇文章主要介绍HBaseJavaAPI,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1.在pom.xml文件中加入相关依赖

<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.4.6</version>
        </dependency>

2.HBaseJavaAPI操作

(1)连接

//创建配置对象
Configuration conf = HBaseConfiguration,create();
//指定zk集群地址
conf.set("hbase.zookeeper.quorum","master:2181,node1:2181,node2:2181");
//创建与HBase的连接
Connection conn = ConnectionFactory.createConnection(conf);
//创建一个Admin对象
Admin admin = conn.getAdmin();

(2)创建表

 //创建表
    @Test
    public void createTable() throws IOException {
        TableName table = TableName.valueOf("test1");
        //判断test1是否存在,是则删除
        if (admin.tableExists(table)){
            //删除表之前,需要disable关闭表
            admin.disableTable(table);
            admin.deleteTable(table);
        }

        //创建HTableDescriptor对象
        HTableDescriptor test1 = new HTableDescriptor(table);
        //创建HColumnDescriptor对象
        HColumnDescriptor cf1 = new HColumnDescriptor("cf1");
        //设置版本号
        cf1.setMaxVersions(3);

        //添加列簇
        test1.addFamily(cf1);
        //创建表
        admin.createTable(test1);


    }

(3)列出表

//列出所有表
    @Test
    public void listTable() throws IOException {
        TableName[] tableNames = admin.listTableNames();
        for (TableName tableName : tableNames) {
            System.out.println(tableName);
        }

    }

(4)删除表

@Test
    public void dropTable() throws IOException {

        TableName table = TableName.valueOf("testJavaApi");
        //删除表之前,需要disable关闭表
        admin.disableTable(table);
        admin.deleteTable(table);

    }

(5) 修改表

@Test
    public void modifyTable() throws IOException {
        TableName table = TableName.valueOf("test1");
        //创建HTableDescriptor对象
//        HTableDescriptor hTableDescriptor = new HTableDescriptor(table);
        //通过Admin对象去创建HTableDescriptor对象,如果直接通过new方式创建  则获取不到表本身有的结构
        HTableDescriptor hTableDescriptor = admin.getTableDescriptor(table);

      /*  //获取表原有的结构
        for (HColumnDescriptor columnFamily : hTableDescriptor.getColumnFamilies()) {
            System.out.println(columnFamily);
            hTableDescriptor.addFamily(columnFamily);
        }
*/
        //添加列簇cf2
        hTableDescriptor.addFamily(new HColumnDescriptor("cf2"));
        admin.modifyTable(table,hTableDescriptor);

    }

(6)插入数据

@Test
    //插入一条数据
    public void put() throws IOException {
        //获取待插入数据的表
        Table test1 = conn.getTable(TableName.valueOf("test1"));
        Put put = new Put("003".getBytes());
        put.addColumn("cf1".getBytes(),"name".getBytes(),"王五".getBytes());
        put.addColumn("cf1".getBytes(),"age".getBytes(),Bytes.toBytes(20));
        put.addColumn("cf1".getBytes(),"gender".getBytes(),"male".getBytes());
        put.addColumn("cf2".getBytes(),"clazz".getBytes(),"文科一班".getBytes());
        test1.put(put);


    }

(7)获取数据

 @Test
    //通过rowkey获取一条数据
    public void get() throws IOException {
        Table test1 = conn.getTable(TableName.valueOf("test1"));
        Get get = new Get("001".getBytes());

        Result rs = test1.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(cf1,"clazz".getBytes()));
        System.out.println(name+","+age+","+gender+","+clazz);


    }

(8)全表扫描

 @Test
    //指定rk的范围进行scan
    public void scan() throws IOException {
        //连接表
        Table test1 = conn.getTable(TableName.valueOf("test1"));

        Scan scan = new Scan();
        //包含withStartRow  不包含withStopRow
        scan.withStartRow("001".getBytes());
        scan.withStopRow("004".getBytes());
        ResultScanner scanner = test1.getScanner(scan);
        for (Result rs : scanner) {
            //获取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()));
                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);

            }

        }

    }

(9)关闭连接

@After
    public void close(){
        try {
            admin.close();
            conn.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

3.完整代码

public class Demo2HBaseAPI {
    Connection conn = null;
    Admin admin = 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 IOException {
        TableName table = TableName.valueOf("test1");
        //判断test1是否存在,是则删除
        if (admin.tableExists(table)){
            //删除表之前,需要disable关闭表
            admin.disableTable(table);
            admin.deleteTable(table);
        }

        //创建HTableDescriptor对象
        HTableDescriptor test1 = new HTableDescriptor(table);
        //创建HColumnDescriptor对象
        HColumnDescriptor cf1 = new HColumnDescriptor("cf1");
        //设置版本号
        cf1.setMaxVersions(3);

        //添加列簇
        test1.addFamily(cf1);
        //创建表
        admin.createTable(test1);


    }

    //列出所有表
    @Test
    public void listTable() throws IOException {
        TableName[] tableNames = admin.listTableNames();
        for (TableName tableName : tableNames) {
            System.out.println(tableName);
        }

    }

    @Test
    public void dropTable() throws IOException {

        TableName table = TableName.valueOf("testJavaApi");
        //删除表之前,需要disable关闭表
        admin.disableTable(table);
        admin.deleteTable(table);

    }

    @Test
    public void modifyTable() throws IOException {
        TableName table = TableName.valueOf("test1");
        //创建HTableDescriptor对象
//        HTableDescriptor hTableDescriptor = new HTableDescriptor(table);
        //通过Admin对象去创建HTableDescriptor对象,如果直接通过new方式创建  则获取不到表本身有的结构
        HTableDescriptor hTableDescriptor = admin.getTableDescriptor(table);
      /*  //获取表原有的结构
        for (HColumnDescriptor columnFamily : hTableDescriptor.getColumnFamilies()) {
            System.out.println(columnFamily);
            hTableDescriptor.addFamily(columnFamily);
        }
*/
        //添加列簇cf2
        hTableDescriptor.addFamily(new HColumnDescriptor("cf2"));
        admin.modifyTable(table,hTableDescriptor);

    }

    @Test
    //插入一条数据
    public void put() throws IOException {
        //获取待插入数据的表
        Table test1 = conn.getTable(TableName.valueOf("test1"));
        Put put = new Put("003".getBytes());
        put.addColumn("cf1".getBytes(),"name".getBytes(),"王五".getBytes());
        put.addColumn("cf1".getBytes(),"age".getBytes(),Bytes.toBytes(20));
        put.addColumn("cf1".getBytes(),"gender".getBytes(),"male".getBytes());
        put.addColumn("cf2".getBytes(),"clazz".getBytes(),"文科一班".getBytes());
        test1.put(put);

    }

    @Test
    //通过rowkey获取一条数据
    public void get() throws IOException {
        Table test1 = conn.getTable(TableName.valueOf("test1"));
        Get get = new Get("001".getBytes());

        Result rs = test1.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(cf1,"clazz".getBytes()));
        System.out.println(name+","+age+","+gender+","+clazz);

    }

    @Test
    //指定rk的范围进行scan
    public void scan() throws IOException {
        //连接表
        Table test1 = conn.getTable(TableName.valueOf("test1"));

        Scan scan = new Scan();
        //包含withStartRow  不包含withStopRow
        scan.withStartRow("001".getBytes());
        scan.withStopRow("004".getBytes());
        ResultScanner scanner = test1.getScanner(scan);
        for (Result rs : scanner) {
            //获取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()));
                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);

            }

        }

    }
    //使用cellUtil获取数据
    @Test
    public void cellUtil() throws IOException {
        //连接表
        Table test1 = conn.getTable(TableName.valueOf("test1"));

        Scan scan = new Scan();
        //包含withStartRow  不包含withStopRow
        scan.withStartRow("001".getBytes());
        scan.withStopRow("004".getBytes());
        ResultScanner scanner = test1.getScanner(scan);
        for (Result rs : scanner) {
            List<Cell> cells = rs.listCells();
            for (Cell cell : cells) {
                String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
                String cf = Bytes.toString(CellUtil.cloneFamily(cell));
                String qf = Bytes.toString(CellUtil.cloneQualifier(cell));

                String value = null;

                if ("age".equals(qf)){
                    value = Bytes.toInt(CellUtil.cloneValue(cell))+"";
                }else {
                    value = Bytes.toString(CellUtil.cloneValue(cell));
                }

                System.out.println(rowkey+"-->"+cf+"-->"+qf+"-->"+value);



            }
            System.out.println();

        }

    }


    @After
    public void close(){
        try {
            admin.close();
            conn.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

最后

以上就是整齐宝贝为你收集整理的HBaseJavaAPI的全部内容,希望文章能够帮你解决HBaseJavaAPI所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(44)

评论列表共有 0 条评论

立即
投稿
返回
顶部