千家信息网

HBase2 java Api接口举例分析

发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,这篇文章主要介绍"HBase2 java Api接口举例分析",在日常操作中,相信很多人在HBase2 java Api接口举例分析问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对
千家信息网最后更新 2025年12月02日HBase2 java Api接口举例分析

这篇文章主要介绍"HBase2 java Api接口举例分析",在日常操作中,相信很多人在HBase2 java Api接口举例分析问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"HBase2 java Api接口举例分析"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

接口介绍

拦截器

  • CompareOperator.LESS 匹配小宇设定值的值

  • CompareOperator.LESS_OR_EQUAL 匹配小宇或等于设定值的值

  • CompareOperator.EQUAL 匹配等于设定值的值

  • CompareOperator.NOT_EQUAL 匹配与设定值不相等的值

  • CompareOperator.GREATER_OR_EQUAL 匹配大于或等于设定值的值

  • CompareOperator.GREATER 匹配大于设定值的值 CompareOperator.NO_OP 排除一切值

比较器种类

  • RowFilter :基于行键来过滤数据;

  • FamilyFilterr :基于列族来过滤数据;

  • QualifierFilterr :基于列限定符(列名)来过滤数据;

  • ValueFilterr :基于单元格 (cell) 的值来过滤数据;

  • DependentColumnFilter :指定一个参考列来过滤其他列的过滤器,过滤的原则是基于参考列的时间戳来进行筛选 。

比较器

  • BinaryComparator 使用Bytes.compareTo()比较当前值与阈值

  • BinaryPrefixComparator 与上面类似,但是是从左端开始前缀匹配

  • NullComparator 不做匹配,只判断当前值是不是null

  • BitComparator 通过BitwiseOp类提供的按位与(AND)、或(OR)、异或(XOR)操作执行位级比较 ,只能用EQUAL和NOT_EQUAL

  • RegexStringComparator 根据一个正则表达式,在实例化这个比较器的时候去匹配表中的数据 ,只能用* EQUAL和NOT_EQUAL

  • SubStringComparator 把阈值和表中数据当做String实例,同时通过contains()操作匹配字符串,只能用EQUAL和NOT_EQUAL

专用过滤器

  • 单列列值过滤器 (SingleColumnValueFilter)

  • 单列列值排除器 (SingleColumnValueExcludeFilter)

  • 行键前缀过滤器 (PrefixFilter)

  • 列名前缀过滤器 (ColumnPrefixFilter)

  • 分页过滤器 (PageFilter)

  • 时间戳过滤器 (TimestampsFilter)

  • 首次行键过滤器 (FirstKeyOnlyFilter)

包装过滤器

  • SkipFilter过滤器,遇到需要过滤keyvalue实例时,拓张过滤整行数据

  • WhileMatchFilter过滤器 遇到一个需要过滤的 KeyValue 实例时,WhileMatchFilter 则结束本次扫描,返回已经扫描到的结果

  • FilterList 过滤器类组合,多种类型过滤器组合。

java代码

maven导包

  • 日志接口框架使用slf4j,这里去除commons-logging。

                org.apache.hbase        hbase-client        2.0.2                                            commons-logging                commons-logging                         

java代码

  • 实现对hbase库数据的增删改查,支持kerberos认证,为避免复杂参数设置,这里直接引入hadoop和hbase配置文件。

import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.hbase.Cell;import org.apache.hadoop.hbase.CompareOperator;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.Connection;import org.apache.hadoop.hbase.client.ConnectionFactory;import org.apache.hadoop.hbase.client.Get;import org.apache.hadoop.hbase.client.HBaseAdmin;import org.apache.hadoop.hbase.client.Result;import org.apache.hadoop.hbase.client.ResultScanner;import org.apache.hadoop.hbase.client.Scan;import org.apache.hadoop.hbase.client.Table;import org.apache.hadoop.hbase.filter.Filter;import org.apache.hadoop.hbase.filter.RowFilter;import org.apache.hadoop.hbase.filter.SubstringComparator;import org.apache.hadoop.hbase.util.Bytes;import org.apache.hadoop.security.UserGroupInformation;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public final class HbaseUtil {                private static Logger log= LoggerFactory.getLogger(HbaseUtil.class);                private Connection connection;                public HbaseUtil() throws IOException {                Configuration conf = HBaseConfiguration.create();                conf.addResource(new Path(ConfigUtil.hbaseFile));                conf.addResource(new Path(ConfigUtil.coreFile));                conf.addResource(new Path(ConfigUtil.hdfsFile));                                if(ConfigUtil.kerberos==1) {                        System.setProperty("java.security.krb5.conf", "/etc/krb5.conf");                                                UserGroupInformation.setConfiguration(conf);                        UserGroupInformation.loginUserFromKeytab(ConfigUtil.kerberosUser,ConfigUtil.kerberosFile);                        HBaseAdmin.available(conf);                }                                this.connection=ConnectionFactory.createConnection(conf);        }                                /**         * 范围查询         * @param tableName 表名         * @param startRowkey 开始rowkey         * @param endRowkey 结束rowkey不查询         * @return         */        public List getData(String tableName,String startRowkey,String endRowkey){                                log.info("Search Table {} ,Startrowkey:{} ,Endrowkey:{}",tableName,startRowkey,endRowkey);                List dataList=new ArrayList<>();                ResultScanner resultList = null;                String rowkey;                                String filterString=startRowkey.substring(4);                try {                        Table tableModel=connection.getTable(TableName.valueOf(tableName));                                                Scan scan = new Scan();                                                //添加start和end                        scan.withStartRow(Bytes.toBytes(startRowkey));                        scan.withStopRow(Bytes.toBytes(endRowkey));                        scan.addColumn(Bytes.toBytes(ConfigUtil.familyName), Bytes.toBytes(ConfigUtil.cloumnName));                                        resultList = tableModel.getScanner(scan);                        if(resultList!=null) {                                for (Result result : resultList) {          //TODO 添加rowkey规范验证                                        rowkey = Bytes.toString(result.getValue(Bytes.toBytes(ConfigUtil.familyName), Bytes.toBytes(ConfigUtil.cloumnName)));                                        if(StringUtil.isNotEmpty(rowkey)) {                                                dataList.add(rowkey);                                        }                                }                        }                                        tableModel.close();                                        } catch (Exception e) {                        log.error(e.toString(),e);                }                return dataList;        }                        /**         * 根据rowkey批量查询         * @param tableName 表名         * @param rowkeyList rowkey列表         * @return         */        public List getDataList(String tableName,List rowkeyList){                                log.info("Search Table {} ,rowkeyList:{} ",tableName,JsonUtil.toJson(rowkeyList));                                List dataList=new ArrayList<>();                try {                        Table tableModel=connection.getTable(TableName.valueOf(tableName));                                                                        List getList=new ArrayList<>();                        for(String rowkey:rowkeyList) {                                getList.add(new Get(Bytes.toBytes(rowkey)));                        }                                                //查询                        Result[] resultList=tableModel.get(getList);                                                //存储数据                        if(resultList!=null&&resultList.length>0) {                                Cell[] cellList;                                for(Result result:resultList) {                                        cellList=result.rawCells();                                        for(Cell cell:cellList) {                                                dataList.add(Bytes.toString(cell.getValueArray()));                                        }                                }                        }                                        tableModel.close();                                        } catch (Exception e) {                        log.error(e.toString(),e);                }                return dataList;        }                /**         * 创建表         * @param tableName         */        public boolean createTable(String tableName){                try {                        //判断数据库是否存在                        Admin admin=this.connection.getAdmin();                                                NamespaceDescriptor[] namespace=admin.listNamespaceDescriptors();                        int state=0;                                                //获取命名空间                        if(namespace.length>0) {                                for(NamespaceDescriptor name:namespace){                                        if(name.getName().equals(ConfigUtil.dataName)){                                                state=1;                                        }                                }                        }                                                //创建命名空间                        if(state==0){                                log.info("Create NameSpace {}",ConfigUtil.dataName);                                admin.createNamespace(NamespaceDescriptor.create(ConfigUtil.dataName).build());                        }                                                TableName table= TableName.valueOf(ConfigUtil.dataName+":"+tableName);                        //创建表                        if(admin.tableExists(table)){                                log.info("{} tables Exists!",tableName);                                                        }else{                                log.info("Create Table {}",tableName);                    //表描述器构造器                    TableDescriptorBuilder  tdb  =TableDescriptorBuilder.newBuilder(table);                    //列族描述起构造器                    ColumnFamilyDescriptorBuilder cdb =  ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(ConfigUtil.familyName));                    //存储时间                    cdb.setTimeToLive(ConfigUtil.saveTime*24*60*60);                    //获得列描述起                    ColumnFamilyDescriptor  cfd = cdb.build();                    //添加列族                    tdb.setColumnFamily(cfd);                    //获得表描述器                    TableDescriptor td = tdb.build();                    //创建表                    admin.createTable(td);                                log.info("{} Table Create Success!",tableName);                        }                        return true;                } catch (Exception e) {                        log.error(e.toString(),e);                }                return false;        }                /**         * 判断表是否存在         * @param tableName         * @return         */        public boolean getTableStatus(String tableName){                try {                        Admin admin=this.connection.getAdmin();                        return admin.tableExists(TableName.valueOf(ConfigUtil.dataName+":"+tableName));                                        } catch (Exception e) {                        log.error(e.toString(),e);                }                return false;        }                /**         * 删除表         */        public boolean delTable(String tableName){                try {                        TableName table=TableName.valueOf(ConfigUtil.dataName+":"+tableName);                        Admin admin=this.connection.getAdmin();                        if(admin.tableExists(table)){                                admin.disableTable(table);                                admin.deleteTable(table);                                log.info("Delete {} Success!",tableName);                        }else{                                log.info("No Found Table:{}",tableName);                        }                                                return true;                } catch (Exception e) {                        log.error(e.toString(),e);                }                return false;        }                /**         * 添加数据         * @param tableName         * @param data         */        public void addData(String tableName,Map data) {                try {                        Table tableModel=connection.getTable(TableName.valueOf(ConfigUtil.dataName+":"+tableName));                                                List puts = new ArrayList<>();                                                Put put;                        for(Map.Entry entry:data.entrySet()) {                                put= new Put(Bytes.toBytes(entry.getKey()));                        put.addColumn(Bytes.toBytes(ConfigUtil.familyName),Bytes.toBytes(ConfigUtil.cloumnName), Bytes.toBytes(entry.getValue()));                        puts.add(put);                        }                tableModel.put(puts);                tableModel.close();                                        } catch (Exception e) {                        log.error(e.toString(),e);                }        }                        /**         * 关闭连接         */        public void close() {                try {                        this.connection.close();                } catch (IOException e) {                        log.error(e.toString(),e);                }        }}

到此,关于"HBase2 java Api接口举例分析"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

过滤器 数据 接口 设定值 分析 实例 学习 查询 前缀 时间 比较器 代码 更多 空间 阈值 构造器 参考 存储 帮助 组合 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 人大复印资料数据库叶嘉莹 网络技术如何带动农村发展 网络安全能从事什么工作 网络安全blog 青铜峡管理软件开发 网络安全管理制度(意见) 数据库查找指定字段的语句 服务器管理薪酬 荔湾物联网软件开发价格表 巴彦淖尔市网络安全处置 ldap管理邮件服务器 省厅网络安全检查 网络安全技术的案例 sql数据库结束事务的两条语句 导入的web项目数据库怎么修改 河北南水北调网络安全 华中科大网络安全专业就业前景 软件开发周记500字 软件开发定金未交齐 上海计算机软件开发中心知乎 数据库修改数据回滚 房地产数据库商函 怎么快速替换数据库 c 数据库添加代码怎么写 苏州共营陈互联网科技有限公司 csgo自定义服务器录制比赛 荔湾物联网软件开发价格表 乡镇网络安全领导机构成立情况 方舟服务器管理器网络状态 营口新欣互联网科技有限公司
0