在上一遍博文中介绍了创建创建预分区,并实现添加数据操作,本篇博文,介绍不同角度的查询
1.按范围查询
/**
* scan 设置时间范围查询
* @param hTable
* @throws IOException
*/
private static void scanByRangeQuery(Table hTable,String startRow,String endRow) throws IOException {
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes(startRow));//范围查询
scan.setStopRow(Bytes.toBytes(endRow));
ResultScanner scanner = hTable.getScanner(scan);
for (Result result : scanner) {
System.out.println("rowKey:"+new String(result.getRow()));
for (Cell cell : result.rawCells()) {
// System.out.println("列族:"+new String(CellUtil.cloneFamily(cell))+ " 列:"+new String(CellUtil.cloneQualifier(cell))+ " 值:"+new String(CellUtil.cloneValue(cell)));
}
}
}
public static void main(String agrs[]){
String startRow="70-u000";
String endRow="90-u100";
String tableNameIndex= "hb_yonghu_index";
Table tableIndex=HbaseConnectionUtils.getInstance().getTable(tableNameIndex);
scanByRangeQuery(tableIndex,startRow,endRow);//范围查询
}
2.按正则查询
/**
* scan 正则过滤器,根据用户id查询用户
* 提取rowkey以01结尾数据
* Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator(".*01$"));
* 提取rowkey以包含201407的数据
* Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new SubstringComparator("201407"));
* 提取rowkey以123开头的数据
* Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new BinaryPrefixComparator("123".getBytes()));
* @param hTable
* @throws IOException
*/
private static void scanByRegexFilter(Table hTable,String userId) throws IOException {
Scan scan = new Scan();
Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator(".*-"+userId));//查询uid
scan.setFilter(filter);
ResultScanner scanner = hTable.getScanner(scan);
for (Result result : scanner) {
System.out.println("rowKey:"+new String(result.getRow()));
for (Cell cell : result.rawCells()) {
// System.out.println("列族:"+new String(CellUtil.cloneFamily(cell))+ " 列:"+new String(CellUtil.cloneQualifier(cell))+ " 值:"+new String(CellUtil.cloneValue(cell)));
}
}
}
public static void main(String agrs[]){
String tableNameIndex= "hb_yonghu_index";
Table tableIndex=HbaseConnectionUtils.getInstance().getTable(tableNameIndex);
scanByRegexFilter(tableIndex, "u073"); //正则查询
}
3.按ROWKEY
/**
* scan 通过rowkey过滤器进行查看
* @param hTable
* @throws IOException
*/
private static void scanByRowKeyFilter(Table hTable,String rowKey) throws IOException {
Scan scan = new Scan();
Filter filter=new RowFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL,new BinaryComparator(Bytes.toBytes(rowKey)));//OK筛选出匹配的所有的行
scan.setFilter(filter);
ResultScanner scanner = hTable.getScanner(scan);
for (Result result : scanner) {
System.out.println("rowKey:"+new String(result.getRow()));
for (Cell cell : result.rawCells()) {
// System.out.println("列族:"+new String(CellUtil.cloneFamily(cell))+ " 列:"+new String(CellUtil.cloneQualifier(cell))+ " 值:"+new String(CellUtil.cloneValue(cell)));
}
}
}
public static void main(String agrs[]){
String tableNameIndex= "hb_yonghu_index";
Table tableIndex=HbaseConnectionUtils.getInstance().getTable(tableNameIndex);
scanByRowKeyFilter(tableIndex,rowKey);//rowkey查询
}
4.按前缀过滤
/**
* scan 前缀过滤器
* @param hTable
* @throws IOException
*/
private static void scanByPrefixQuery(Table hTable,String prefix) throws IOException {
Scan scan = new Scan();
Filter filter = new PrefixFilter(Bytes.toBytes(prefix));
scan.setFilter(filter);
ResultScanner scanner = hTable.getScanner(scan);
for (Result result : scanner) {
System.out.println("rowKey:"+new String(result.getRow()));
for (Cell cell : result.rawCells()) {
// System.out.println("列族:"+new String(CellUtil.cloneFamily(cell))+ " 列:"+new String(CellUtil.cloneQualifier(cell))+ " 值:"+new String(CellUtil.cloneValue(cell)));
}
}
}
public static void main(String agrs[]){
Table table=HbaseConnectionUtils.getInstance().getTable("hb_yonghu");
scanByPrefixQuery(table,"70");
}
5.按包含字符串
/**
* scan 包含过滤器
* @param hTable
* @throws IOException
*/
private static void scanByContainsFilter(Table hTable,String startRow,String param) throws IOException {
Scan scan = new Scan();
Filter filter = new RowFilter(CompareFilter.CompareOp.NO_OP,new SubstringComparator(param));
scan.setFilter(filter);
ResultScanner scanner = hTable.getScanner(scan);
for (Result result : scanner) {
System.out.println("rowKey:"+new String(result.getRow()));
for (Cell cell : result.rawCells()) {
// System.out.println("列族:"+new String(CellUtil.cloneFamily(cell))+ " 列:"+new String(CellUtil.cloneQualifier(cell))+ " 值:"+new String(CellUtil.cloneValue(cell)));
}
}
}
public static void main(String agrs[]){
Table table=HbaseConnectionUtils.getInstance().getTable("hb_yonghu");
scanByContainsFilter(table,"","1564647622836");//时间查询
}
6.按时间段
/**
* scan 设置时间范围查询
* @param hTable
* @throws IOException
*/
private static void scanByRangeTimeQuery(Table hTable,String startRow,String endRow) throws IOException {
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes(startRow));//范围查询
scan.setStopRow(Bytes.toBytes(endRow));
ResultScanner scanner = hTable.getScanner(scan);
for (Result result : scanner) {
System.out.println("rowKey:"+new String(result.getRow()));
for (Cell cell : result.rawCells()) {
// System.out.println("列族:"+new String(CellUtil.cloneFamily(cell))+ " 列:"+new String(CellUtil.cloneQualifier(cell))+ " 值:"+new String(CellUtil.cloneValue(cell)));
}
}
}
public static void main(String agrs[]){
Table table=HbaseConnectionUtils.getInstance().getTable("hb_yonghu");
String beginStr="70-1564647622838-u000000";
String endStr="80-1564647622938-u1000000";
scanByRangeTimeQuery(table,beginStr,endStr);//时间范围查询;80-0000000-XXX到80-1564647622938的数据也能查询到
// 80分区比70分区大,即80分区的所有数据均能查询出来。
//要求要查询:时间段:1564647622838 到1564647622938 这个时间段的数据
// startRow="70-1564647622838-u000000",endRow="80-1564647622938-u1000000";不太满足要求,80分区中时间段小于1564647622838数据也能查询出来
// 解决办法:分区查询再累加;70区域这个时间段的数据: startRow="70-1564647622838-u000000",endRow="70-1564647622938-u1000000";
// 80区域这个时间段的数据: startRow="80-1564647622838-u000000",endRow="80-1564647622938-u1000000";
// 最后将结果累加起来
}