前言: 所谓的过滤器和比较器,说白了就是WHERE后语句 比较器:
//1、正则比较器= like zs% 能用索引
RegexStringComparator rsc = new RegexStringComparator("^zs");
//2、substring子串比较器= like %zs% 不能用索引
SubstringComparator sc = new SubstringComparator("4");
//3、binaryPrefix比较器 前缀比较 能用索引 一般用的比较多
BinaryPrefixComparator bpc = new BinaryPrefixComparator(Bytes.toBytes("zs4"));
//4、二进制比较器 =的意思
BinaryComparator bc = new BinaryComparator(Bytes.toBytes("zs40"));
比较运算符CompareOp
/** less than */
LESS, 小于
/** less than or equal to */
LESS_OR_EQUAL, 小于等于
/** equals */
EQUAL, 等于
/** not equal */
NOT_EQUAL, 不等于
/** greater than or equal to */
GREATER_OR_EQUAL, 大于等于
/** greater than */
GREATER, 大于
/** no operation */
NO_OP
过滤器:
行键过滤器 RowFilter
//前缀比较器
BinaryPrefixComparator bpc =new BinaryPrefixComparator(Bytes.toBytes("91110111L21454469M100190021128031679"));
//2、RowFilter过滤器
RowFilter rf = new RowFilter(CompareFilter.CompareOp.EQUAL,bpc);
HbaseUtil.testScan(rf);
列族过滤器FamilyFilter
//二进制前缀比较器
BinaryPrefixComparator bpc = new BinaryPrefixComparator(Bytes.toBytes("ex"));
//2、构造一个列簇过滤器
FamilyFilter ff = new FamilyFilter(CompareFilter.CompareOp.EQUAL,rc);
HbaseUtil.testScan(ff);
列过滤器QualifierFilter
//binaryPrefix比较器 前缀比较
BinaryPrefixComparator bpc = new BinaryPrefixComparator(Bytes.toBytes("zs4"));
//1、构造SingleColumValueFilter对象
SingleColumnValueFilter sf = new SingleColumnValueFilter(Bytes.toBytes("extra_info"),
Bytes.toBytes("name"),
CompareFilter.CompareOp.EQUAL,
bc);
sf.setFilterIfMissing(true);
//2、调用测试方法
HbaseUtil.testScan(sf);
值过滤器ValueFilter
//二进制前缀比较器
BinaryPrefixComparator bpc = new BinaryPrefixComparator(Bytes.toBytes("ad"));
//BinaryComparator
BinaryComparator bc = new BinaryComparator(Bytes.toBytes("addr"));
//构造一个列名过滤器
QualifierFilter qf = new QualifierFilter(CompareFilter.CompareOp.EQUAL,bc);
HbaseUtil.testScan(crf);
多列名过滤器
//多列名过滤器
byte[][] prefixs = new byte[][]{Bytes.toBytes("ad"),Bytes.toBytes("se")};
MultipleColumnPrefixFilter mcpf = new MultipleColumnPrefixFilter(prefixs);
HbaseUtil.testScan(mcpf);
列名范围过滤器
//列名范围过滤器
ColumnRangeFilter crf = new ColumnRangeFilter(Bytes.toBytes("addr"),
true,
Bytes.toBytes("sex"),
false);
HbaseUtil.testScan(crf);
HbaseUtil
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.log4j.Logger;
import java.io.IOException;
import java.util.Iterator;
/**
* @ClassName HbaseUtil
* @Description TODO
* @Author
* @Date 2018/7/11 0011 10:12
* @Version 1.0
*/
public class HbaseUtil {
private static Logger logger = Logger.getLogger(HbaseUtil.class);
private static final String CONNECTION_KEY = "hbase.zookeeper.quorum";
private static final String CONNECTION_VALUE = "hadoop01:2181,hadoop02:2181,hadoop03:2181";
private static Connection conn = null;
static{
//1、创建configuration对象
Configuration conf = HBaseConfiguration.create();
//2、设置连接hbase的参数
conf.set(CONNECTION_KEY,CONNECTION_VALUE);
//3、获取一个Connection对象
try {
conn = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
logger.error("创建hbase的连接失败!",e);
}
}
public static Admin getAdmin(){
Admin admin = null;
try {
admin = conn.getAdmin();
} catch (IOException e) {
logger.error("获取admin对象失败!!!",e);
}
return admin;
}
public static Table getTable(){
return getTable("ns1:t_user_info");
}
public static Table getTable(String tableName){
TableName tb = TableName.valueOf(tableName);
Table table = null;
try {
table = conn.getTable(tb);
} catch (IOException e) {
logger.error("获取表管理器失败!",e);
}
return table;
}
public static void printRS(Result result){
CellScanner cellScanner = result.cellScanner();
System.out.print(Bytes.toString(result.getRow()));
try {
while (cellScanner.advance()){
Cell cell = cellScanner.current();
System.out.print("\t" + new String(CellUtil.cloneFamily(cell),"utf-8") + " : " + new String(CellUtil.cloneQualifier(cell),"utf-8") + " = " + new String(CellUtil.cloneValue(cell),"utf-8"));
}
System.out.println("");
} catch (IOException e) {
logger.error("判断是否有下一个单元格失败!",e);
}
}
public static void printRS(ResultScanner resultScanner){
Iterator resultIterator = resultScanner.iterator();
while (resultIterator.hasNext()){
Result result = resultIterator.next();
HbaseUtil.printRS(result);
}
}
public static void closeAdmin(Admin admin){
//1、判断admin是否为空
if(admin == null){
return;
}
try {
admin.close();
} catch (IOException e) {
//do nothing
}
}
public static void testScan(Filter filter){
testScan("invoice:ods-invoice",filter);
}
public static void testScan(String tabName,Filter filter){
//4、获取表管理器
Table table = HbaseUtil.getTable(tabName);
//5、创建一个scan对象
Scan scan = new Scan();
//6、将过滤条件加入到scan对象
scan.setFilter(filter);
//7、扫描表数据
ResultScanner resultScanner = null;
try {
resultScanner = table.getScanner(scan);
//8、打印结果
HbaseUtil.printRS((resultScanner));
} catch (IOException e) {
e.printStackTrace();
}finally {
//9、关闭资源
HbaseUtil.closeTable(table);
resultScanner.close();
}
}
public static void closeTable(Table table){
if (table == null) {
return;
}
try {
table.close();
} catch (IOException e) {
//do nothing
}
}
}
UTF-8
2.7.1
com.sun
tools
1.8.0
system
${env.JAVA_HOME}/lib/tools.jar
org.apache.hadoop
hadoop-common
${hadoop.version}
org.apache.hadoop
hadoop-client
${hadoop.version}
org.apache.hadoop
hadoop-hdfs
${hadoop.version}
org.apache.zookeeper
zookeeper
3.4.10
pom
org.apache.hbase
hbase-client
1.2.1
org.apache.hbase
hbase-common
1.2.1
org.apache.hbase
hbase-server
1.2.1
junit
junit
4.12