Quantcast
Channel: CodeSection,代码区,数据库(综合) - CodeSec
Viewing all articles
Browse latest Browse all 6262

hbasefilter自定义Comparator

$
0
0
HBase之过滤器
filter ==> SQL 中的Where

filter的执行流程:

过滤器在客户端创建,然后通过RPC发送到服务器上,由服务器执行

基础过滤器:

比较器:
Comparator

Description

LongComparator

Assumes the given value array is a Java Long number and uses Bytes.toLong() to convert it.

BinaryComparator

Uses Bytes.compareTo() to compare 当前值与阀值

BinaryPrefixComparator

Bytes.compareTo() 进行匹配,但是从左端开始前缀匹配

NullComparator

判断当前值是否为null

BitComparator

Performs a bitwise comparison, providing a BitwiseOp enumeration with AND, OR, and XOR operators.

RegexStringComparator

正则表达式匹配

SubstringComparator

子字符串比对

RowFilter 行键过滤器:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

/**
* 基于行键上的过滤器
*/
public class FilterInHbase {
public static void main(String[] args) throws IOException{
Configuration configuration = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(configuration);
//建立user表的连接
Table table =connection.getTable(TableName.valueOf(“user”));
Scan scan=new Scan();
//扫描列族info 列age
scan.addColumn(Bytes.toBytes(“info”),Bytes.toBytes(“age”));
System.out.println("行过滤器");
//比较过滤器
//这儿是指找出行小于或者等于"510824118261011172"的所有行
Filter filter1 = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes("813782218261011172")));
//添加过滤器到扫描器中
scan.setFilter(filter1);
ResultScanner scanner1 = table.getScanner(scan);
for(Result res:scanner1){
System.out.println(res);
}
scanner1.close();
System.out.println("正则过滤器");
//正则过滤器
//过滤行键以2结束的
Filter filter2 = new RowFilter(CompareFilter.CompareOp.EQUAL,
new RegexStringComparator(".*2$")
);
scan.setFilter(filter2);
ResultScanner scanner2 = table.getScanner(scan);
for (Result res:scanner2){
System.out.println(res);
}
scanner2.close();
//子串过滤器
//过滤行键中包含了"61826"这个字符串
System.out.println("子串过滤器");
Scan scan3=new Scan();
//扫描列族info 列age
scan3.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age"));
Filter filter3=new RowFilter(CompareFilter.CompareOp.EQUAL,
new SubstringComparator("61826")
);
scan3.setFilter(filter3);
ResultScanner scanner3=table.getScanner(scan3);
for(Result res:scanner3){
System.out.println(res);
}
scanner3.close();
table.close();
connection.close();
}

}

/**
Result:
行过滤器 < 813782218261011172
keyvalues={224382618261914241/info:age/1472196211169/Put/vlen=2/seqid=0}
keyvalues={510824118261011172/info:age/1472196213020/Put/vlen=2/seqid=0}
keyvalues={524382618264914241/info:age/1472196193913/Put/vlen=2/seqid=0}
keyvalues={673782618261019142/info:age/1472196211733/Put/vlen=2/seqid=0}
keyvalues={813782218261011172/info:age/1472196212550/Put/vlen=2/seqid=0}
正则过滤器 已2结尾的行键
keyvalues={510824118261011172/info:age/1472196213020/Put/vlen=2/seqid=0}
keyvalues={673782618261019142/info:age/1472196211733/Put/vlen=2/seqid=0}
keyvalues={813782218261011172/info:age/1472196212550/Put/vlen=2/seqid=0}
子串过滤器 包含了”61826”的行键
keyvalues={224382618261914241/info:age/1472196211169/Put/vlen=2/seqid=0}
keyvalues={524382618264914241/info:age/1472196193913/Put/vlen=2/seqid=0}
keyvalues={673782618261019142/info:age/1472196211733/Put/vlen=2/seqid=0}
**/

FamilyFilter列族过滤器:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.FamilyFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
/**
* 列族过滤器
* 比较列族来返回结果
* 用户可以在列族一级筛选所需数据
*/
public class FamilyFilterInHbase {
public static void main(String[] args) throws IOException {
Configuration configuration = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(configuration);
//建立表的连接
Table table = connection.getTable(TableName.valueOf(“user”));
//比较过滤器 现在表有2个列族 info ship 当然实际有可能很多哦
//取info < "kiss" > Result of get():keyvalues=NONE [本身冲突 所以无数据]
//如果get列族info 那么==>> Result of get2():keyvalues={673782618261019142/...
get2.addFamily(Bytes.toBytes("ship"));
get2.setFilter(filter2);
Result result2 =table.get(get2);
System.out.println("Result of get2():"+result2);
scanner.close();
table.close();
connection.close();
}
}
/**
LESS “kiss”
keyvalues={224382618261914241/info:age/1472196211169/Put/vlen=2/seqid=0, 224382618261914241/info:height/1472196211234/Put/vlen=3/seqid=0, 224382618261914241/info:name/1472196211088/Put/vlen=4/seqid=0, 224382618261914241/info:phone/1472196211427/Put/vlen=11/seqid=0, 224382618261914241/info:weight/1472196211386/Put/vlen=3/seqid=0}
keyvalues={510824118261011172/info:age/1472196213020/Put/vlen=2/seqid=0, 510824118261011172/info:height/1472196213056/Put/vlen=3/seqid=0, 510824118261011172/info:name/1472196212942/Put/vlen=8/seqid=0, 510824118261011172/info:phone/1472196213237/Put/vlen=11/seqid=0, 510824118261011172/info:weight/1472196213169/Put/vlen=3/seqid=0}
keyvalues={524382618264914241/info:age/1472196193913/Put/vlen=2/seqid=0, 524382618264914241/info:height/1472196194783/Put/vlen=3/seqid=0, 524382618264914241/info:name/1472196193255/Put/vlen=8/seqid=0, 524382618264914241/info:phone/1472196195125/Put/vlen=11/seqid=0, 524382618264914241/info:weight/1472196194970/Put/vlen=3/seqid=0}
keyvalues={673782618261019142/info:age/1472196211733/Put/vlen=2/seqid=0, 673782618261019142/info:height/1472196211761/Put/vlen=3/seqid=0, 673782618261019142/info:name/1472196211678/Put/vlen=7/seqid=0, 673782618261019142/info:phone/1472196211956/Put/vlen=11/seqid=0, 673782618261019142/info:weight/1472196211841/Put/vlen=3/seqid=0}
keyvalues={813782218261011172/info:age/1472196212550/Put/vlen=2/seqid=0, 813782218261011172/info:height/1472196212605/Put/vlen=3/seqid=0, 813782218261011172/info:name/1472196212480/Put/vlen=8/seqid=0, 813782218261011172/info:phone/1472196212713/Put/vlen=11/seqid=0, 813782218261011172/info:weight/1472196212651/Put/vlen=3/seqid=0}
GREATER “kiss”
keyvalues={224382618261914241/ship:addr/1472196211487/Put/vlen=7/seqid=0, 224382618261914241/ship:email/1472196211530/Put/vlen=11/seqid=0, 224382618261914241/ship:salary/1472196211594/Put/vlen=4/seqid=0}
keyvalues={510824118261011172/ship:addr/1472196213328/Put/vlen=8/seqid=0, 510824118261011172/ship:email/1472196213422/Put/vlen=12/seqid=0, 510824118261011172/ship:salary/1472196214963/Put/vlen=5/seqid=0}
keyvalues={524382618264914241/ship:addr/1472196195270/Put/vlen=7/seqid=0, 524382618264914241/ship:email/1472196195371/Put/vlen=13/seqid=0, 524382618264914241/ship:salary/1472196195485/Put/vlen=4/seqid=0}
keyvalues={673782618261019142/ship:addr/1472196212059/Put/vlen=8/seqid=0, 673782618261019142/ship:email/1472196212176/Put/vlen=12/seqid=0, 673782618261019142/ship:salary/1472196212284/Put/vlen=4/seqid=0}
keyvalues={813782218261011172/ship:addr/1472196212762/Put/vlen=4/seqid=0, 813782218261011172/ship:email/1472196212802/Put/vlen=12/seqid=0, 813782218261011172/ship:salary/1472196212840/Put/vlen=5/seqid=0}
APPEND(LESS “kiss”)
Result of get(): keyvalues={673782618261019142/info:age/1472196211733/Put/vlen=2/seqid=0, 673782618261019142/info:height/1472196211761/Put/vlen=3/seqid=0, 673782618261019142/info:name/1472196211678/Put/vlen=7/seqid=0, 673782618261019142/info:phone/1472196211956/Put/vlen=11/seqid=0, 673782618261019142/info:weight/1472196211841/Put/vlen=3/seqid=0}
APPEND(GREATER “kiss”)
Result of get1(): keyvalues={673782618261019142/ship:addr/1472196212059/Put/vlen=8/seqid=0, 673782618261019142/ship:email/1472196212176/Put/vlen=12/seqid=0, 673782618261019142/ship:salary/1472196212284/Put/vlen=4/seqid=0}
//filter “info” get “ship”
Result of get():keyvalues=NONE
//filter “info” get “info”
Result of get2():keyvalues={673782618261019142/info:age/1472196211733/Put/vlen=2/seqid=0, 673782618261019142/info:height/1472196211761/Put/vlen=3/seqid=0, 673782618261019142/info:name/1472196211678/Put/vlen=7/seqid=0, 673782618261019142/info:phone/1472196211956/Put/vlen=11/seqid=0, 673782618261019142/info:weight/1472196211841/Put/vlen=3/seqid=0}

**/

ValueFilter值过滤器:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
/**
* 值过滤器
* 根据值进行筛选 可以联合RegexStringComparator 进行设计
*/
public class FilterOfValue {
public static void main(String[] args) throws IOException {
Configuration configuration = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(configuration);
//建立表的连接
Table table = connection.getTable(TableName.valueOf(“user”));
//值中包含了177的过滤器
Filter filter = new ValueFilter(CompareFilter.CompareOp.EQUAL,
new SubstringComparator("1771392142")
);
Scan scan = new Scan();
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner){
for (Cell cell:result.rawCells()){
System.out.println("Cell: "+cell+", Value: "+Bytes.toString(cell.getValueArray(),cell.getValueLength()));
}
}
scanner.close();
Get get1 = new Get(Bytes.toBytes("673782618261019142"));
get1.setFilter(filter);
Result result1=table.get(get1);
for (Cell cell : result1.rawCells()) {
System.out.println("Get1 Cell: " + cell + ", Value: " +
Bytes.toString(cell.getValueArray(), cell.getValueOffset(),
cell.getValueLength()));
}
Get get2 = new Get(Bytes.toBytes("813782218261011172"));
get2.setFilter(filter);
Result result2=table.get(get2);
for (Cell cell : result2.rawCells()) {
System.out.println("Get2 Cell: " + cell + ", Value: " +
Bytes.toString(cell.getValueArray(), cell.getValueOffset(),
cell.getValueLength()));
}
table.close();
connection.close();
}

}

/**
原数据:
673782618261019142 column=info:phone, timestamp=1472196211956, value=17713921424
813782218261011172 column=info:phone, timestamp=1472196212713, value=12713921424
*输出结果:
Cell: 673782618261019142/info:phone/1472196211956/Put/vlen=11/seqid=0, Value: 73782618261019142

Viewing all articles
Browse latest Browse all 6262

Trending Articles