史上最全面最完整的大数据学习第八章 Hbase(三)Java API

Hbase ---- 初识 与 环境搭建

Hbase(二) shell操作

Hbase(三)Java API

Hbase(四)MR on Hbase & Hbase的 架构

Hbase(五) Hbase 的读写流程

Hbase完结篇 Hbase 集群搭建以及相关操作

四、Java API

4.1 依赖

 <dependency>
   <groupId>org.apache.hbasegroupId>
   <artifactId>hbase-clientartifactId>
   <version>1.2.4version>
dependency>

4.2 获取客户端

private Connection connection;
    private Admin admin;

    @Before
    public void getAdmin() throws Exception {

        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum", "HadoopNode00");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        connection = ConnectionFactory.createConnection(conf);

        admin = connection.getAdmin();

    }

4.3 关闭资源


    @After
    public void close() throws Exception {
        admin.close();
        connection.close();
    }

4.4 命名空间操作

创建命名空间

 @Test
    public void createNameSpace() throws Exception {

        NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create("hadoop1").addConfiguration("csdn", "123").build();

        admin.createNamespace(namespaceDescriptor);

    }

修改命名空间

 @Test
    public void changeNameSpace()  throws Exception{

        NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create("hadoop").removeConfiguration("csdn").build();

        admin.modifyNamespace(namespaceDescriptor);

    }

删除命名空间

@Test
    public void deleteNameSpace() throws Exception{


        admin.deleteNamespace("hadoop");

    }

列出命名空间


    @Test
    public void listNameSpace() throws Exception {

        NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();


        for (NamespaceDescriptor namespaceDescriptor : namespaceDescriptors) {


            System.out.println(namespaceDescriptor.getName());
        }

    }

4.5 表操作

创建表

 @Test
    public void createTable() throws Exception {
        /*
         * 将表的名字信息封装到TableName中
         * */
        TableName tableName = TableName.valueOf("csdn:t_java");


        /*
         *
         * 创建描述表的对象 并提供表的名字
         * */
        HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);


        /*
         * 描述列簇的对象  并指定列簇的名字
         * */
        HColumnDescriptor cf1 = new HColumnDescriptor("cf1");
        // 设置 最大可存的版本
        cf1.setMaxVersions(3);

        /*
         * 描述列簇的对象  并指定列簇的名字
         * */
        HColumnDescriptor cf2 = new HColumnDescriptor("cf2");
        // 设置 最大可存的版本
        cf2.setMaxVersions(3);

        // 在表中添加必要的属性:列簇
        tableDescriptor.addFamily(cf1);
        tableDescriptor.addFamily(cf2);


        /*
        * 使用admin对象创建表
        * */
        admin.createTable(tableDescriptor);

    }

    

删除表

    @Test
    public void deleteTable() throws Exception {
        TableName tableName = TableName.valueOf("csdn:t_java");
        if (admin.tableExists(tableName)) {

            admin.disableTable(tableName);

            admin.deleteTable(tableName);
        }

    }

4.6 CURD

put

插入|更新单个记录


    @Test
    public void putData() throws Exception {

        TableName tableName = TableName.valueOf("csdn:t_user");

        Table table = connection.getTable(tableName);

        /*
         * 封装  一行 数据
         * */
        Put put = new Put("1".getBytes());
        /*
         * 参数列表  :  列簇  列名  值
         * */
        put.addColumn("cf1".getBytes(), "name".getBytes(), "zhangsan".getBytes());
        put.addColumn("cf1".getBytes(), "pwd".getBytes(), "123".getBytes());
        put.addColumn("cf2".getBytes(), "age".getBytes(), "18".getBytes());
        put.addColumn("cf2".getBytes(), "salary".getBytes(), "1000".getBytes());


        table.put(put);
        table.close();

    }

批量插入

 @Test
    public void putManyData() throws Exception {

        TableName tableName = TableName.valueOf("csdn:t_user");

        Table table = connection.getTable(tableName);

        /*
         * 封装  一行 数据
         * */
        Put put = new Put("2".getBytes());
        /*
         * 参数列表  :  列簇  列名  值
         * */
        put.addColumn("cf1".getBytes(), "name".getBytes(), "lisi".getBytes());
        put.addColumn("cf1".getBytes(), "pwd".getBytes(), "123".getBytes());
        put.addColumn("cf2".getBytes(), "age".getBytes(), "20".getBytes());
        put.addColumn("cf2".getBytes(), "salary".getBytes(), "20000".getBytes());

        ArrayList<Put> puts = new ArrayList<Put>();
        puts.add(put);


        table.put(puts);
        table.close();

    }

@Test
    public void putManyData() throws Exception {

        TableName tableName = TableName.valueOf("csdn:t_user");


        BufferedMutator bufferedMutator = connection.getBufferedMutator(tableName);


        /*
         * 封装  一行 数据
         * */
        Put put = new Put("2".getBytes());
        /*
         * 参数列表  :  列簇  列名  值
         * */
        put.addColumn("cf1".getBytes(), "name".getBytes(), "ls".getBytes());
        put.addColumn("cf1".getBytes(), "pwd".getBytes(), "123".getBytes());
        put.addColumn("cf2".getBytes(), "age".getBytes(), "20".getBytes());
        put.addColumn("cf2".getBytes(), "salary".getBytes(), "20000".getBytes());

        ArrayList<Put> puts = new ArrayList<Put>();
        puts.add(put);

        bufferedMutator.mutate(puts);
        bufferedMutator.close();


    }

delete


    @Test
    public void deleteData() throws Exception {
        TableName tableName = TableName.valueOf("csdn:t_user");

        Table table = connection.getTable(tableName);

        Delete delete = new Delete("2".getBytes());

        table.delete(delete);
        table.close();


    }

批量删除


    @Test
    public void deleteManyData() throws Exception {
        TableName tableName = TableName.valueOf("csdn:t_user");


        Table table = connection.getTable(tableName);

        Delete delete = new Delete("1".getBytes());

        table.delete(delete);
        table.close();


    }

get

 @Test
    public void getData() throws Exception{

        Table table = connection.getTable(TableName.valueOf("csdn:t_user"));

        Get get = new Get("2".getBytes());

        Result result = table.get(get);


        /*
        * 列簇    列名
        * */
        byte[] name = result.getValue("cf1".getBytes(), "name".getBytes());
        byte[] pwd = result.getValue("cf1".getBytes(), "pwd".getBytes());
        byte[] age = result.getValue("cf2".getBytes(), "age".getBytes());
        byte[] salary = result.getValue("cf2".getBytes(), "salary".getBytes());

        System.out.println("名字为:"+Bytes.toString(name)+", 密码为:"+Bytes.toString(pwd)+",年龄为:"+Bytes.toString(age)+",工资为:"+Bytes.toString(salary));

    }

获取多个版本的数据

 @Test
    public void getManyData() throws Exception {

        Table table = connection.getTable(TableName.valueOf("csdn:t_user"));
        Get get = new Get("2".getBytes());
        get.setMaxVersions(3);
        get.addColumn("cf1".getBytes(), "name".getBytes());

        Result result = table.get(get);
        List<Cell> columnCells = result.getColumnCells("cf1".getBytes(), "name".getBytes());

        for (Cell columnCell : columnCells) {


            byte[] rowData = CellUtil.cloneRow(columnCell);
            byte[] cfData = CellUtil.cloneFamily(columnCell);
            byte[] qualifierData = CellUtil.cloneQualifier(columnCell);
            byte[] data = CellUtil.cloneValue(columnCell);
            System.out.println("行健为:" + Bytes.toString(rowData) + ", 列簇为:" + Bytes.toString(cfData) + ",列名为:" + Bytes.toString(qualifierData) + ",名字为:" + Bytes.toString(data));

        }
    }

scan

@Test
    public void scanData() throws Exception {


        Table table = connection.getTable(TableName.valueOf("csdn:t_user"));

        Scan scan = new Scan();

        // scan.addFamily("cf1".getBytes());
        //scan.addColumn("cf1".getBytes(),"name".getBytes());

        PrefixFilter prefixFilter1 = new PrefixFilter("1".getBytes());
        PrefixFilter prefixFilter2 = new PrefixFilter("2".getBytes());

        FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE, prefixFilter1, prefixFilter2);


        scan.setFilter(filterList);

        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {
            System.out.println("------------------");
            byte[] name = result.getValue("cf1".getBytes(), "name".getBytes());
            byte[] pwd = result.getValue("cf1".getBytes(), "pwd".getBytes());
            byte[] age = result.getValue("cf2".getBytes(), "age".getBytes());
            byte[] salary = result.getValue("cf2".getBytes(), "salary".getBytes());

            System.out.println("名字为:" + Bytes.toString(name) + ", 密码为:" + Bytes.toString(pwd) + ",年龄为:" + Bytes.toString(age) + ",工资为:" + Bytes.toString(salary));


        }

    scanner.close();
        table.close();
    }

加更加更加更!献给每一个正在努力的我们,就算在忙,也要注意休息和饮食哦!我就是我,一个在互联网跌跌撞撞,摸爬滚打的热忱,给个三连吧~

你可能感兴趣的:(大数据笔记)