Hbase的表查询操作
目录
1.scan
2.get
1.filterBySingleColumnValueFilter(String tablename, String strF, String strC, String strClass)
1.1基本代码
1.2 过滤值的大小范围
RegexStringComparator
SubstringComparator
BinaryPrefixComparator
比较运算符
LESS <
LESS_OR_EQUAL <=
EQUAL =
NOT_EQUAL <>
GREATER_OR_EQUAL >=
GREATER >
NO_OP 排除所有
1.scan
可以不用输入rowkey,能按照一定的规则查出来对应的rowkey和值
public void scan(String tablename,String cf,String column) throws Exception{HTable table=new HTable(conf,tablename);Scan s=new Scan();s.setStartRow(Bytes.toBytes("0"));//可以限定rowkey的范围,字典序s.setStopRow(Bytes.toBytes("g"));
// s.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));ResultScanner res=table.getScanner(s);for(Result r:res) {byte[] row=r.getRow();byte[] val=r.getValue(Bytes.toBytes(cf), Bytes.toBytes(column));System.out.println("Scan:"+Bytes.toString(row)+"的"+ column+" values is "+Bytes.toString(val));}res.close();table.close();
}
2.get
需要传入这4个字段,能找出对应的值
String tablename,String rowkey,String cf(列族),String cq(列限定符)
public void get(String tablename,String row,String info,String name) throws Exception{HTable table=new HTable(conf,tablename);Get g=new Get(Bytes.toBytes(row));Result result = table.get(g);byte[] val = result.getValue(Bytes.toBytes(info),Bytes.toBytes(name));System.out.println(info+" "+name+" "+"Values =" + Bytes.toString(val));
}
下面是一些过滤器的使用
1.filterBySingleColumnValueFilter(String tablename, String strF, String strC, String strClass)
1.1基本代码
public void filterBySingleColumnValueFilter(String tablename, String strF, String strC, String strClass) throws Exception
{HTable table = new HTable(conf, tablename);Scan s = new Scan();SingleColumnValueFilter sf = new SingleColumnValueFilter(Bytes.toBytes(strF), Bytes.toBytes(strC), CompareOp.NOT_EQUAL, Bytes.toBytes(strClass));s.setFilter(sf);ResultScanner rs = table.getScanner(s);for (Result r : rs) {byte[] row = r.getRow();byte[] value = r.getValue(Bytes.toBytes(strF), Bytes.toBytes(strC));System.out.println("Filter: " + Bytes.toString(row) + "的 " + strC + " " + Bytes.toString(value));}rs.close();table.close();
}
好像不能设置多个列族。。。
1.2 过滤值的大小范围
public void filterBySingleColumnValueFilter(String tablename, String strF, String strC,String dayu,String xiaoyu) throws Exception
{HTable table = new HTable(conf, tablename);Scan s = new Scan();FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);//组成区间过滤SingleColumnValueFilter filter1 =new SingleColumnValueFilter(Bytes.toBytes(strF), Bytes.toBytes(strC), CompareOp.LESS_OR_EQUAL,Bytes.toBytes(xiaoyu));SingleColumnValueFilter filter2 =new SingleColumnValueFilter(Bytes.toBytes(strF), Bytes.toBytes(strC), CompareOp.GREATER_OR_EQUAL,Bytes.toBytes(dayu));filterList.addFilter(filter1);filterList.addFilter(filter2);s.setFilter(filterList);ResultScanner rs = table.getScanner(s);for (Result r : rs) {byte[] row = r.getRow();byte[] value = r.getValue(Bytes.toBytes(strF), Bytes.toBytes(strC));System.out.println("Filter: " + Bytes.toString(row) + "的 " + strC + " " + Bytes.toString(value));}rs.close();table.close();
}
关键代码
SingleColumnValueFilter filter1 =
new SingleColumnValueFilter(Bytes.toBytes(strF), Bytes.toBytes(strC), CompareOp.LESS_OR_EQUAL,Bytes.toBytes(xiaoyu));
SingleColumnValueFilter filter2 =
new SingleColumnValueFilter(Bytes.toBytes(strF), Bytes.toBytes(strC), CompareOp.GREATER_OR_EQUAL,Bytes.toBytes(dayu));
可设置rowkey的范围以及返回的条数
Filter filter = new PageFilter(2); // Hbase获取前2条记录,只能放到后面,在追加的列主前面无效s.setFilter(filter);s.setStartRow(Bytes.toBytes("0"));s.setStopRow(Bytes.toBytes("3"));
注意:追加列族,如果不写,就默认追加全部,如果写了,就默认查出来的数据就是你追加的数据限定,不会出现其他的
s.addColumn(Bytes.toBytes("info"), Bytes.toBytes(strC));//追加列族和列s.addColumn(Bytes.toBytes("info"), Bytes.toBytes("sex"));//追加列族和列
下面我举个例子:
这个是我的表,如果我只追加scoreinfo,
我想查询sex这一列就查不到
设置过滤器"scoreinfo:60-70的数据,RegexStringComparator匹配正则表达式
RegexStringComparator
RegexStringComparator comp =new RegexStringComparator("^[67][0-9]$");SingleColumnValueFilter filter_zz = new SingleColumnValueFilter(Bytes.toBytes(strF), Bytes.toBytes(strC), CompareOp.EQUAL, comp);s.setFilter(filter_zz);
找出来scoreinfo中包含6的数字
SubstringComparator
SubstringComparator comp_con = new SubstringComparator("6");//包含6SingleColumnValueFilter filter_con = new SingleColumnValueFilter(Bytes.toBytes(strF), Bytes.toBytes(strC), CompareOp.EQUAL, comp_con);s.setFilter(filter_con);
BinaryPrefixComparator
匹配开头,前缀二进制比较器,找出6开头的数据
BinaryPrefixComparator comp_pre =new BinaryPrefixComparator("8".getBytes());//以6开头的
SingleColumnValueFilter filter_pre = new SingleColumnValueFilter(Bytes.toBytes(strF), Bytes.toBytes(strC), CompareOp.EQUAL, comp_pre);
// s.setFilter(filter_pre);
我想过滤60到70之间的数据,可以怎么写呢?
方法一:
直接在得到的结果里面加一个判断函数:
ResultScanner rs = table.getScanner(s);for (Result r : rs) {byte[] row = r.getRow();byte[] value = r.getValue(Bytes.toBytes(strF), Bytes.toBytes(strC));if(Integer.valueOf(Bytes.toString(value))>60&&Integer.valueOf(Bytes.toString(value))<70) {System.out.println("Filter: " + Bytes.toString(row) + "的 " +"列族是"+strF+" cq是"+ strC + " 值是 " + Integer.valueOf(Bytes.toString(value)));}
方法二,用正则表达式,过滤找出60到70的数据
RegexStringComparator comp =new RegexStringComparator("^[67][0-9]$");SingleColumnValueFilter filter_zz = new SingleColumnValueFilter(Bytes.toBytes(strF), Bytes.toBytes(strC), CompareOp.EQUAL, comp);s.setFilter(filter_zz);
方法三,用比较器,但是有些许问题呢,比如我有个数据是666,它字典序。。。
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);//组成区间过滤SingleColumnValueFilter filter1 =new SingleColumnValueFilter(Bytes.toBytes(strF), Bytes.toBytes(strC), CompareOp.LESS_OR_EQUAL,Bytes.toBytes("70"));SingleColumnValueFilter filter2 =new SingleColumnValueFilter(Bytes.toBytes(strF), Bytes.toBytes(strC), CompareOp.GREATER_OR_EQUAL,Bytes.toBytes("60"));filterList.addFilter(filter1);filterList.addFilter(filter2);s.setFilter(filterList);
Hbase的表查询操作相关推荐
- Mybatis入门:4(多表查询操作)
多表查询操作 Mybatis的多表操作 表之间的关系有几种:一对多.一对一.多对一.多对多 举例: 用户和订单就是一对多--一个用户可以下多个订单 订单和用户就是多对一--多个订单属于同一个用户 人和 ...
- 简单的单表和连表查询操作
现在是做项目的时候,之前大部分都是跟着老师的视频和代码对着敲去完成老师的作业,做出了效果后就不管了,然后一段时间之后就不知道代码是怎样来的了,而且也不认识大部分的代码意思,现在做项目了,要自己去敲出来 ...
- yii mysql join_Yii框架连表查询操作示例
本文实例讲述了Yii框架连表查询操作.分享给大家供大家参考,具体如下: Join //表连接 //查询出学生.班级.校区.记录表的所有数据 $data=Jf_record::find() ->j ...
- 达梦数据库实验三:DMDBMS表查询操作
目录标题 实验三 DMDBMS表查询操作 一.实验目的: 二.实验要求: 三.实验重点和难点: 四.实验内容: 五.实验步骤与结果: 1. 表创建 2. 数据填充 3. 数据查询 3.1查出选修了20 ...
- C# 基于AE的GIS二次开发 要素查询操作,属性表查询操作及其属性表修改操作
直接上代码: 要素查询 并高亮显示 模糊查询我的根据自己的表设计的,自己用基本语句是 属性表字段 LIKE '*内容*' *号为 SQL like语句里的% like '%%',在ArcGIS里是*开 ...
- 数据库的多表查询操作-查询只选修了1门课程的学生,显示学号、姓名、课程名。
文章目录 前言 一.建立数据库和表 二.数据库展示 2.查询只选修了1门课程的学生,显示学号.姓名.课程名. 总结 前言 在我看来数据库真的是一个神奇的东西,不但里面的只是点很深刻,而且对于我们学习起 ...
- mysql教程多表查询_mysql重点,表查询操作和多表查询
表单查询 1. 完整的查询语句语法 select distinct(* or 字段名 or 四则运算 )from 表名 where 条件 group by 条件 having 条件 order by ...
- python count函数用法 comm_python3:MySQL 8.0学习笔记(第五部分:单表查询操作)
在讲解单表查询时,首先创建一个emp的员工表,表中字段包括:empno(员工编号).ename(员工姓名).job(员工职位).mgr(员工领导).hiredate(员工入职日期).sal(员工月薪) ...
- 10月25日学习内容整理:数据操作:增加更新删除,单表查询操作
>>\G是按行显示,必须是大写 >>插入数据:补充另一种插入记录的方法 -->insert into 表名1(字段1,字段2,...) select 字段1,字段2,.. ...
最新文章
- C库函数-perror()
- 皮一皮:学霸和学渣的区别
- 使用SG_IO发送SCSI 指令测试底层驱动的scatter-gather 功能
- 织梦网站翻页php,dedecms织梦网站列表页和内容页分页样式
- Taro+react开发(65):h5全局重置背景样式
- Foxmail新建自动标签功能在哪 如何给Foxmail收件人邮件设置自动标签
- 父类对象由子类实例化【转载】
- 苹果2019新款iPhone售价惊曝:咬牙仍坚持高价位?
- compose RxJava笔记
- linux cpu监控方案,Linux性能优化和监控系列(二)分析CPU性能
- php常用数组,php常用数组函数
- 自己动手来做一寸或两寸照片(使用工具)
- C51单片机实验——矩阵按键
- 毕业5年决定你的一生
- Android中的短信收不到问题,华为的安卓(Android)系统手机收不到短信问题解决方法...
- [洛谷P3527] [POI2011]MET-Meteors
- html5 自动分享到朋友圈,html5手机端分享微信朋友圈代码
- 微信小程序登陆,后端接口实现 - springboot
- 2023.1. Stimulsoft 报告和仪表板的新版本:Crack
- (三)改掉这些坏习惯,还怕写不出优雅的代码?