java操作hbase使用hbase-client2依赖

介绍了单一curd和批量curd

1. 导入maven依赖

<!--        hbaseClient依赖包,需要排除日志log4j,不然和springboot的冲突了-->
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>2.0.1</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>

            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>2.0.1</version>
            <exclusions>
                <exclusion>
                    <groupId>*</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

2. 单一curd

   /**
     * 向表"hello_ssx_tab";中的列族"family1";添加一条记录
     *             String colName = "col_a";
     *             String rowKey = "row_001";
     * @param value
     * @return
     */
    public boolean setValue(String value,String colName,String rowKey){
        //指定表名
        TableName tableName1 = TableName.valueOf(this.tableName);
        try {

            //获取表对象
            Table table = hbaseConnection.getTable(tableName1);
            //设置行键值
            Put row001 = new Put(Bytes.toBytes(rowKey));


            //设置列族,列,值
            row001.addColumn(Bytes.toBytes(familyName),
                    Bytes.toBytes(colName),Bytes.toBytes(value));




            //添加记录
            table.put(row001);
            // logger.info("成功hbase添加数据信息:目标表:"+tableName+",目标行键:"+rowKey+",目标列族:"+familyName+",目标列:"+ colName +",目标值:"+value);
            return true;

        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;

    }







   /**
     * 查询表中的记录 指定rowkey
     * String rowKey = "row_001";
     * @param rowKey
     * @return
     */
    public String getValue(String rowKey){
        //指定表名
        TableName tableName1 = TableName.valueOf(this.tableName);
        try {
            //设置表
            Table table = hbaseConnection.getTable(tableName1);
            //指定要查询的行键
            Get get = new Get(Bytes.toBytes(rowKey));
            //查询返回结果
            Result result = table.get(get);
            //获取结果集合
            Cell[] cells = result.rawCells();
            //遍历结果集
            StringBuilder returnData = new StringBuilder();
            for (Cell cell:cells){
                byte[] bytes = CellUtil.cloneRow(cell);
                String s = Bytes.toString(bytes);
                 logger.info("查询hbase数据信息:目标表:"+tableName+",目标行键:"+rowKey+",查询出的信息: 行键:"+s);
                returnData.append(s).append('\n');
                String s1 = Bytes.toString(CellUtil.cloneFamily(cell));
                 logger.info("查询hbase数据信息:目标表:"+tableName+",目标行键:"+rowKey+",查询出的信息: 列族:"+s1);
                returnData.append(s1).append('\n');
                String s2 = Bytes.toString(CellUtil.cloneQualifier(cell));
                 logger.info("查询hbase数据信息:目标表:"+tableName+",目标行键:"+rowKey+",查询出的信息: 列名:"+s2);
                returnData.append(s2).append('\n');
                String s3 = Bytes.toString(CellUtil.cloneValue(cell));
                 logger.info("查询hbase数据信息:目标表:"+tableName+",目标行键:"+rowKey+",查询出的信息: 值:"+s3);
                returnData.append(s3).append('\n');
            }
            return returnData.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "查询失败";
    }

3. 批量curd

    /**
     * 批量插入、读取、删除、修改
     *  param value 值
     *  param colName 列名
     *  param rowKey 行键
     * @return
     */
    public boolean setValues() throws IOException {

        //指定表名
        TableName tableName1 = TableName.valueOf(this.tableName);
        try {


            //设置行键值- 待添加的数据
            Put put = new Put(Bytes.toBytes("rowKey001")) //设置行键
                            .addColumn(Bytes.toBytes(familyName), //设置列族
                                       Bytes.toBytes("colName01"), //设置列
                                       Bytes.toBytes("value01")); //设置值
            put.addColumn(Bytes.toBytes(familyName), //设置列族
                                       Bytes.toBytes("colName02"), //设置列
                                       Bytes.toBytes("value02")); //设置值
            //...

            //设置待查询的数据
            Get get1 = new Get(Bytes.toBytes("rowKey002")).addFamily(Bytes.toBytes(familyName));
            Get get2 = new Get(Bytes.toBytes("rowKey003")).addFamily(Bytes.toBytes(familyName));
            Get get3 = new Get(Bytes.toBytes("rowKey004")).addFamily(Bytes.toBytes(familyName));
            //...

            //设置删除数据
            Delete delete = new Delete(Bytes.toBytes("rowKey005")).addFamily(Bytes.toBytes(familyName));

            //方法1:批量提交 简单实现 start
            {
                Table table = hbaseConnection.getTable(tableName1);
                ArrayList<Put> puts = new ArrayList<>();
                ArrayList<Get> gets = new ArrayList<>();
                ArrayList<Delete> deletes = new ArrayList<>();
                table.put(puts);
                Result[] results = table.get(gets);
                for (Result result : results) {
                    Cell[] cells = result.rawCells();
                    for (Cell cell : cells) {
                        byte[] bytes = CellUtil.cloneValue(cell);
                        System.out.println("值:" + Bytes.toString(bytes));
                    }
                }
                table.delete(deletes);
            }
            //方法1:批量提交 简单实现 end



            //方法2:批量提交 通过线程池 高度自定义 好像不支持查询
            //下面是创建缓存、批量提交到hbase 可以自定义线程池
            {
                BufferedMutator bufferedMutator = hbaseConnection.getBufferedMutator(tableName1);
                BufferedMutator bufferedMutator2 = hbaseConnection.getBufferedMutator(new BufferedMutatorParams(tableName1).pool(null));

                ArrayList<Mutation> mutations = new ArrayList<>();
                mutations.add(put);
                mutations.add(delete);
                bufferedMutator.mutate(mutations);
            }






            //添加记录
             // logger.info("成功hbase添加数据信息:目标表:"+tableName+",目标行键:"+rowKey+",目标列族:"+familyName+",目标列:"+ colName +",目标值:"+value);
            return true;

        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;

    }

项目源码gitee

https://gitee.com/shenshuxin01/first_-spring-boot_-demo/blob/master/HBase2Demo/src/main/java/ssx/demo/hbase2demo/service/SerDemo.java

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值