前言:上篇文章HBase Filter 过滤器概述对HBase过滤器的组成及其家谱进行简单介绍,本篇文章主要对HBase过滤器之比较器作一个补充介绍,也算是HBase Filter学习的必备低阶魂技吧。本篇文中源码基于HBase 1.1.2.2.6.5.0-292 HDP版本。

HBase所有的比较器实现类都继承于父类ByteArrayComparable,而ByteArrayComparable又实现了Comparable接口;不同功能的比较器差别在于对父类compareTo()方法的重写逻辑不同。

下面分别对HBase Filter默认实现的七大比较器一一进行介绍。

1. BinaryComparator

介绍:二进制比较器,用于按字典顺序比较指定字节数组。

先看一个小例子:

public class BinaryComparatorDemo {

    public static void main(String[] args) {

        BinaryComparator bc = new BinaryComparator(Bytes.toBytes("bbb"));

        int code1 = bc.compareTo(Bytes.toBytes("bbb"), 0, 3);
System.out.println(code1); // 0
int code2 = bc.compareTo(Bytes.toBytes("aaa"), 0, 3);
System.out.println(code2); // 1
int code3 = bc.compareTo(Bytes.toBytes("ccc"), 0, 3);
System.out.println(code3); // -1
int code4 = bc.compareTo(Bytes.toBytes("bbf"), 0, 3);
System.out.println(code4); // -4
int code5 = bc.compareTo(Bytes.toBytes("bbbedf"), 0, 6);
System.out.println(code5); // -3
}
}

不难看出,该比较器的比较规则如下:

  • 两个字符串首字母不同,则该方法返回首字母的asc码的差值
  • 参与比较的两个字符串如果首字符相同,则比较下一个字符,直到有不同的为止,返回该不同的字符的asc码差值
  • 两个字符串不一样长,可以参与比较的字符又完全一样,则返回两个字符串的长度差值

看一下以上规则对应其compareTo()方法的源码实现:

实现一:

static enum UnsafeComparer implements Bytes.Comparer<byte[]> {
INSTANCE;
....
public int compareTo(byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2, int length2) {
if (buffer1 == buffer2 && offset1 == offset2 && length1 == length2) {
return 0;
} else {
int minLength = Math.min(length1, length2);
int minWords = minLength / 8;
long offset1Adj = (long)(offset1 + BYTE_ARRAY_BASE_OFFSET);
long offset2Adj = (long)(offset2 + BYTE_ARRAY_BASE_OFFSET);
int j = minWords << 3; int offset;
for(offset = 0; offset < j; offset += 8) {
long lw = theUnsafe.getLong(buffer1, offset1Adj + (long)offset);
long rw = theUnsafe.getLong(buffer2, offset2Adj + (long)offset);
long diff = lw ^ rw;
if (diff != 0L) {
return lessThanUnsignedLong(lw, rw) ? -1 : 1;
}
} offset = j;
int b;
int a;
if (minLength - j >= 4) {
a = theUnsafe.getInt(buffer1, offset1Adj + (long)j);
b = theUnsafe.getInt(buffer2, offset2Adj + (long)j);
if (a != b) {
return lessThanUnsignedInt(a, b) ? -1 : 1;
} offset = j + 4;
} if (minLength - offset >= 2) {
short sl = theUnsafe.getShort(buffer1, offset1Adj + (long)offset);
short sr = theUnsafe.getShort(buffer2, offset2Adj + (long)offset);
if (sl != sr) {
return lessThanUnsignedShort(sl, sr) ? -1 : 1;
} offset += 2;
} if (minLength - offset == 1) {
a = buffer1[offset1 + offset] & 255;
b = buffer2[offset2 + offset] & 255;
if (a != b) {
return a - b;
}
} return length1 - length2;
}
}

实现二:

static enum PureJavaComparer implements Bytes.Comparer<byte[]> {
INSTANCE; private PureJavaComparer() {
} public int compareTo(byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2, int length2) {
if (buffer1 == buffer2 && offset1 == offset2 && length1 == length2) {
return 0;
} else {
int end1 = offset1 + length1;
int end2 = offset2 + length2;
int i = offset1; for(int j = offset2; i < end1 && j < end2; ++j) {
int a = buffer1[i] & 255;
int b = buffer2[j] & 255;
if (a != b) {
return a - b;
} ++i;
} return length1 - length2;
}
}
}

实现一是对实现二的一个优化,都引自Bytes类,HBase优先执行实现一方案,如果有异常再执行实现二方案。如下:

public static int compareTo(byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2, int length2) {
return Bytes.LexicographicalComparerHolder.BEST_COMPARER.compareTo(buffer1, offset1, length1, buffer2, offset2, length2);
}
...
... static final String UNSAFE_COMPARER_NAME = Bytes.LexicographicalComparerHolder.class.getName() + "$UnsafeComparer";
static final Bytes.Comparer<byte[]> BEST_COMPARER = getBestComparer();
static Bytes.Comparer<byte[]> getBestComparer() {
try {
Class<?> theClass = Class.forName(UNSAFE_COMPARER_NAME);
Bytes.Comparer<byte[]> comparer = (Bytes.Comparer)theClass.getEnumConstants()[0];
return comparer;
} catch (Throwable var2) {
return Bytes.lexicographicalComparerJavaImpl();
}
}

2. BinaryPrefixComparator

介绍:二进制比较器,只比较前缀是否与指定字节数组相同。

先看一个小例子:

public class BinaryPrefixComparatorDemo {

    public static void main(String[] args) {

        BinaryPrefixComparator bc = new BinaryPrefixComparator(Bytes.toBytes("b"));

        int code1 = bc.compareTo(Bytes.toBytes("bbb"), 0, 3);
System.out.println(code1); // 0
int code2 = bc.compareTo(Bytes.toBytes("aaa"), 0, 3);
System.out.println(code2); // 1
int code3 = bc.compareTo(Bytes.toBytes("ccc"), 0, 3);
System.out.println(code3); // -1
int code4 = bc.compareTo(Bytes.toBytes("bbf"), 0, 3);
System.out.println(code4); // 0
int code5 = bc.compareTo(Bytes.toBytes("bbbedf"), 0, 6);
System.out.println(code5); // 0
int code6 = bc.compareTo(Bytes.toBytes("ebbedf"), 0, 6);
System.out.println(code6); // -3
}
}

该比较器只是基于BinaryComparator比较器稍作更改而已,以下代码一目了然:

public int compareTo(byte[] value, int offset, int length) {
return Bytes.compareTo(this.value, 0, this.value.length, value, offset, this.value.length <= length ? this.value.length : length);
}

看一下同BinaryComparator方法的异同:

public int compareTo(byte[] value, int offset, int length) {
return Bytes.compareTo(this.value, 0, this.value.length, value, offset, length);
}

区别只在于最后一个传参,即length=min(this.value.length,value.length),取小。这样在后面的字节逐位比较时,即只需比较min length次。

3. BitComparator

介绍:位比价器,通过BitwiseOp提供的AND(与)、OR(或)、NOT(非)进行比较。返回结果要么为1要么为0,仅支持 EQUAL 和非 EQUAL。

先看一个小例子:

public class BitComparatorDemo {

    public static void main(String[] args) {

        // 长度相同按位或比较:由低位起逐位比较,每一位按位或比较都为0,则返回1,否则返回0。
BitComparator bc1 = new BitComparator(new byte[]{0,0,0,0}, BitComparator.BitwiseOp.OR);
int i = bc1.compareTo(new byte[]{0,0,0,0}, 0, 4);
System.out.println(i); // 1
// 长度相同按位与比较:由低位起逐位比较,每一位按位与比较都为0,则返回1,否则返回0。
BitComparator bc2 = new BitComparator(new byte[]{1,0,1,0}, BitComparator.BitwiseOp.AND);
int j = bc2.compareTo(new byte[]{0,1,0,1}, 0, 4);
System.out.println(j); // 1
// 长度相同按位异或比较:由低位起逐位比较,每一位按位异或比较都为0,则返回1,否则返回0。
BitComparator bc3 = new BitComparator(new byte[]{1,0,1,0}, BitComparator.BitwiseOp.XOR);
int x = bc3.compareTo(new byte[]{1,0,1,0}, 0, 4);
System.out.println(x); // 1
// 长度不同,返回1,否则按位比较
BitComparator bc4 = new BitComparator(new byte[]{1,0,1,0}, BitComparator.BitwiseOp.XOR);
int y = bc4.compareTo(new byte[]{1,0,1}, 0, 3);
System.out.println(y); // 1
}
}

上述注释阐述的规则,对应以下代码:

···

public int compareTo(byte[] value, int offset, int length) {

if (length != this.value.length) {

return 1;

} else {

int b = 0;

	for(int i = length - 1; i >= 0 && b == 0; --i) {
switch(this.bitOperator) {
case AND:
b = this.value[i] & value[i + offset] & 255;
break;
case OR:
b = (this.value[i] | value[i + offset]) & 255;
break;
case XOR:
b = (this.value[i] ^ value[i + offset]) & 255;
}
} return b == 0 ? 1 : 0;
}

}

···

核心思想就是:由低位起逐位比较,直到b!=0退出循环。

4. LongComparator

介绍:Long 型专用比较器,返回值:0 -1 1。上篇概述没有提到,这里补上。

先看一个小例子:

public class LongComparatorDemo {

    public static void main(String[] args) {
LongComparator longComparator = new LongComparator(1000L);
int i = longComparator.compareTo(Bytes.toBytes(1000L), 0, 8);
System.out.println(i); // 0
int i2 = longComparator.compareTo(Bytes.toBytes(1001L), 0, 8);
System.out.println(i2); // -1
int i3 = longComparator.compareTo(Bytes.toBytes(998L), 0, 8);
System.out.println(i3); // 1
}
}

这个比较器实现相当简单,不多说了,如下:

public int compareTo(byte[] value, int offset, int length) {
Long that = Bytes.toLong(value, offset, length);
return this.longValue.compareTo(that);
}

5. NullComparatorDemo

介绍:控制比较式,判断当前值是不是为null。是null返回0,不是null返回1,仅支持 EQUAL 和非 EQUAL。

先看一个小例子:

public class NullComparatorDemo {

    public static void main(String[] args) {
NullComparator nc = new NullComparator();
int i1 = nc.compareTo(Bytes.toBytes("abc"));
int i2 = nc.compareTo(Bytes.toBytes(""));
int i3 = nc.compareTo(null);
System.out.println(i1); // 1
System.out.println(i2); // 1
System.out.println(i3); // 0
}
}

这个比较器实现相当简单,不多说了,如下:

public int compareTo(byte[] value) {
return value != null ? 1 : 0;
}

6. RegexStringComparator

介绍:提供一个正则的比较器,支持正则表达式的值比较,仅支持 EQUAL 和非 EQUAL。匹配成功返回0,匹配失败返回1。

先看一个小例子:

public class RegexStringComparatorDemo {

    public static void main(String[] args) {
RegexStringComparator rsc = new RegexStringComparator("abc");
int abc = rsc.compareTo(Bytes.toBytes("abcd"), 0, 3);
System.out.println(abc); // 0
int bcd = rsc.compareTo(Bytes.toBytes("bcd"), 0, 3);
System.out.println(bcd); // 1 String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
RegexStringComparator rsc2 = new RegexStringComparator(check);
int code = rsc2.compareTo(Bytes.toBytes("zpb@163.com"), 0, "zpb@163.com".length());
System.out.println(code); // 0
int code2 = rsc2.compareTo(Bytes.toBytes("zpb#163.com"), 0, "zpb#163.com".length());
System.out.println(code2); // 1
}
}

其compareTo()方法有两种引擎实现,对应两套正则匹配规则,分别是JAVA版和JONI版(面向JRuby),默认为RegexStringComparator.EngineType.JAVA。如下:

public int compareTo(byte[] value, int offset, int length) {
return this.engine.compareTo(value, offset, length);
} public static enum EngineType {
JAVA,
JONI; private EngineType() {
}
}

具体实现都很简单,都是调用正则语法匹配。以下是JAVA EngineType 实现:

public int compareTo(byte[] value, int offset, int length) {
String tmp;
if (length < value.length / 2) {
tmp = new String(Arrays.copyOfRange(value, offset, offset + length), this.charset);
} else {
tmp = new String(value, offset, length, this.charset);
} return this.pattern.matcher(tmp).find() ? 0 : 1;
}

JONI EngineType 实现:

public int compareTo(byte[] value, int offset, int length) {
Matcher m = this.pattern.matcher(value);
return m.search(offset, length, this.pattern.getOptions()) < 0 ? 1 : 0;
}

都很容易理解,不多说了。

7. SubstringComparator

介绍:判断提供的子串是否出现在value中,并且不区分大小写。包含字串返回0,不包含返回1,仅支持 EQUAL 和非 EQUAL。

先看一个小例子:

public class SubstringComparatorDemo {

    public static void main(String[] args) {
String value = "aslfjllkabcxxljsl";
SubstringComparator sc = new SubstringComparator("abc");
int i = sc.compareTo(Bytes.toBytes(value), 0, value.length());
System.out.println(i); // 0 SubstringComparator sc2 = new SubstringComparator("abd");
int i2 = sc2.compareTo(Bytes.toBytes(value), 0, value.length());
System.out.println(i2); // 1 SubstringComparator sc3 = new SubstringComparator("ABC");
int i3 = sc3.compareTo(Bytes.toBytes(value), 0, value.length());
System.out.println(i3); // 0
}
}

这个比较器实现也相当简单,不多说了,如下:

public int compareTo(byte[] value, int offset, int length) {
return Bytes.toString(value, offset, length).toLowerCase().contains(this.substr) ? 0 : 1;
}

到此,七种比较器就介绍完了。如果对源码不敢兴趣,也建议一定要看看文中的小例子,熟悉下每种比较器的构造函数及结果输出。后续在使用HBase过滤器的过程中,会经常用到。当然除了这七种比较器,大家也可以自定义比较器。

转载请注明出处!欢迎关注本人微信公众号【HBase工作笔记】

HBase Filter 过滤器之 Comparator 原理及源码学习的更多相关文章

  1. HBase Filter 过滤器之RowFilter详解

    前言:本文详细介绍了HBase RowFilter过滤器Java&Shell API的使用,并贴出了相关示例代码以供参考.RowFilter 基于行键进行过滤,在工作中涉及到需要通过HBase ...

  2. HBase Filter 过滤器之FamilyFilter详解

    前言:本文详细介绍了 HBase FamilyFilter 过滤器 Java&Shell API 的使用,并贴出了相关示例代码以供参考.FamilyFilter 基于列族进行过滤,在工作中涉及 ...

  3. HBase Filter 过滤器之QualifierFilter详解

    前言:本文详细介绍了 HBase QualifierFilter 过滤器 Java&Shell API 的使用,并贴出了相关示例代码以供参考.QualifierFilter 基于列名进行过滤, ...

  4. HBase Filter 过滤器之 ValueFilter 详解

    前言:本文详细介绍了 HBase ValueFilter 过滤器 Java&Shell API 的使用,并贴出了相关示例代码以供参考.ValueFilter 基于列值进行过滤,在工作中涉及到需 ...

  5. Spring中AOP原理,源码学习笔记

    一.AOP(面向切面编程):通过预编译和运行期动态代理的方式在不改变代码的情况下给程序动态的添加一些功能.利用AOP可以对应用程序的各个部分进行隔离,在Spring中AOP主要用来分离业务逻辑和系统级 ...

  6. Spring源码学习

    Spring源码学习--ClassPathXmlApplicationContext(一) spring源码学习--FileSystemXmlApplicationContext(二) spring源 ...

  7. Spring源码学习笔记12——总结篇,IOC,Bean的生命周期,三大扩展点

    Spring源码学习笔记12--总结篇,IOC,Bean的生命周期,三大扩展点 参考了Spring 官网文档 https://docs.spring.io/spring-framework/docs/ ...

  8. Spring-Session实现Session共享实现原理以及源码解析

    知其然,还要知其所以然 ! 本篇介绍Spring-Session的整个实现的原理.以及对核心的源码进行简单的介绍! 实现原理介绍 实现原理这里简单说明描述: 就是当Web服务器接收到http请求后,当 ...

  9. MVC系列——MVC源码学习:打造自己的MVC框架(一:核心原理)

    前言:最近一段时间在学习MVC源码,说实话,研读源码真是一个痛苦的过程,好多晦涩的语法搞得人晕晕乎乎.这两天算是理解了一小部分,这里先记录下来,也给需要的园友一个参考,奈何博主技术有限,如有理解不妥之 ...

随机推荐

  1. 吴恩达最新TensorFlow专项课程开放注册,你离TF Boy只差这一步

    不需要 ML/DL 基础,不需要深奥数学背景,初学者和软件开发者也能快速掌握 TensorFlow.掌握人工智能应用的开发秘诀. 以前,吴恩达的机器学习课程和深度学习课程会介绍很多概念与知识,虽然也会 ...

  2. HDU 4497 GCD and LCM 素因子分解+ gcd 和 lcm

    题意: 给两个数,lll 和 ggg,为x , y , z,的最小公倍数和最大公约数,求出x , y , z 的值有多少种可能性 思路: 将x , y , z进行素因子分解 素因子的幂次 x a1 a ...

  3. Python——Matplotlib库入门

    1.Matplotlib库简介 优秀的可视化第三方库 Matplotlib库由各种可视化类构成,内部结构复杂,受Matlab启发 matplotlib.pyplot是绘制各类可视化图形的命令子库,相当 ...

  4. localStorage应用(写的时间缓存在本地浏览器)

    最近用了下localStorage,于是想记录加深下映象: 有关更详细的介绍,可以去看https://www.cnblogs.com/st-leslie/p/5617130.html: 我这引用了这个 ...

  5. HBase 监控 | HBase Metrics 初探(一)

    前言:对于任意一个系统而言,做好监控都是非常重要的,HBase也不例外.经常,我们会从JMX中获取相关指标来做展示.对HBase进行监控,那这些指标是怎么生成的呢?如果你想自定义自己的监控指标又该怎么 ...

  6. 【tensorflow2.0】处理文本数据

    一,准备数据 imdb数据集的目标是根据电影评论的文本内容预测评论的情感标签. 训练集有20000条电影评论文本,测试集有5000条电影评论文本,其中正面评论和负面评论都各占一半. 文本数据预处理较为 ...

  7. python编程学习路线及笔记

    话不多说,直接上图! 关于人工智能算法学习思路,欢迎浏览我的另一篇随笔:如果你想开始学习算法,不妨先了解人工智能有哪些方向? 之后博主将持续分享各大算法的学习思路和学习笔记:hello world: ...

  8. Thinkphp getLastSql函数用法

    如何判断一个更新操作是否成功: $Model = D('Blog'); $data['id'] = 10; $data['name'] = 'update name'; $result = $Mode ...

  9. php设计模式总结

    #1 使用设计模式(如建造者.外观.原型和模板模式)更快速.更有效地创建对象 #2 使用数据访问对象和代理设计模式分离体系结构 #3 使用委托.工厂和单元素设计模式改善代码流和控制 #4 在不修改对象 ...

  10. docker开机自动启动

    方法一: chkconfig docker on 方法二: 1.1是用systemctl: systemctl enable docker 1.2将Docker的docker.service服务移动到 ...