在看hadoop  的二次排序的时候,改写了下, 加了第三个参数,  本来以为是在

   public int compareTo(IntPair o) {
System.out.println("-----------compareTo");
if (first != o.first) {
return first < o.first ? -1 : 1;
} else if (second != o.second) {
return second < o.second ? -1 : 1;
}else if (third != o.third) {
return third < o.third ? -1 : 1;} return 0;
}

本来以为排序在这里面进行, 后来发现不是,把比较第3个字段的代码去掉, 发现还是有序的。

后来通过打印得知在compare函数中,稍微改写了下

      public int compare(byte[] b1, int s1, int l1,
byte[] b2, int s2, int l2) {
// 二进制数组读取
int intvalue = readInt(b1, s1);
System.out.println("s1 = " + b1.length);

    // 验证b1中存储的数据
int third = ;
for(int i =s1 + ; i<= s1+ ; i++){
third += (b1[i]&0xff) << (-*i);
}
System.out.println("third = " + third); return compareBytes(b1, s1, l1, b2, s2, l2);
}
}

有3个整形值, s1为开始位置, l1为长度12, 这样我们就可以读出我们的值

return compareBytes(b1, s1, l1, b2, s2, l2);调用 return FastByteComparisons.compareTo(b1, s1, l1, b2, s2, l2);

    public int compareTo(byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2, int length2)
{
if ((buffer1 == buffer2) && (offset1 == offset2) && (length1 == length2))
{
return 0;
} int end1 = offset1 + length1;
int end2 = offset2 + length2;
int i = offset1; for (int j = offset2; (i < end1) && (j < end2); ++j) {
int a = buffer1[i] & 0xFF;
int b = buffer2[j] & 0xFF;
if (a != b)
return (a - b);
++i;
} return (length1 - length2);
}
}
}

从代码中就知道了,通过字节数组比较三个值, 这样就出来的结果就是有序的了

结论, 理论上N个字段这样出来的结果的都是有序的,只是比较的长度有所变化

测试又加了一个字段, 输出结果都是有序的。

测试代码

  public static class IntPair
implements WritableComparable<IntPair> {
private int first = 0;
private int second = 0;
private int third = 0;
private int fourth = 0; /**
* Set the left and right values.
*/
public void set(int left, int right, int third, int fourth) {
first = left;
second = right;
this.third = third;
this.fourth = fourth;
} public int getFirst() {
return first;
} public int getSecond() {
return second;
} public int getThird() {
return third;
} public int getFourth() {
return fourth;
} @Override
public String toString() {
System.out.println("third = " + third);
return first + "\t" + second + "\t" + third + "\t" + fourth;
} /**
* Read the two integers.
* Encoded as: MIN_VALUE -> 0, 0 -> -MIN_VALUE, MAX_VALUE-> -1
*/
@Override
public void readFields(DataInput in) throws IOException {
first = in.readInt();// + Integer.MIN_VALUE;
second = in.readInt();// + Integer.MIN_VALUE;
third = in.readInt();// + Integer.MIN_VALUE;
fourth = in.readInt();
}
@Override
public void write(DataOutput out) throws IOException {
/*
out.writeInt(first - Integer.MIN_VALUE);
out.writeInt(second - Integer.MIN_VALUE);
out.writeInt(third - Integer.MIN_VALUE);
*/
out.writeInt(first );
out.writeInt(second );
out.writeInt(third );
out.writeInt(fourth);
}
@Override
public int hashCode() {
return first * 157 + second*10 + third;
} @Override
public boolean equals(Object right) {
if (right instanceof IntPair) {
IntPair r = (IntPair) right;
return r.first == first && r.second == second && r.third == third && r.fourth == fourth;
} else {
return false;
}
} /** A Comparator that compares serialized IntPair. */
public static class Comparator extends WritableComparator {
public Comparator() {
super(IntPair.class);
} // 排序比较器,数据全部存在byte数组
public int compare(byte[] b1, int s1, int l1,
byte[] b2, int s2, int l2) {
// 二进制数组读取
int intvalue = readInt(b1, s1);
System.out.println("s1 = " + b1.length); int third = 0;
for(int i =s1 + 9; i<= s1+ 12; i++){
third += (b1[i]&0xff) << (24-8*i);
}
System.out.println("third = " + third); return compareBytes(b1, s1, l1, b2, s2, l2);
}
} static { // register this comparator
WritableComparator.define(IntPair.class, new Comparator());
} // 好像没用上
@Override
public int compareTo(IntPair o) {
System.out.println("-----------compareTo");
if (first != o.first) {
return first < o.first ? -1 : 1;
} else if (second != o.second) {
return second < o.second ? -1 : 1;
}// else if (third != o.third) {
// return third < o.third ? -1 : 1;} return 0;
}
}
  public static class StrPair
implements WritableComparable<StrPair> {
private Text first;
private Text second ;
private Text third ;
private Text fourth; // 这句很重要, 要不读的时候会出错
public StrPair(){
set(new Text(),new Text(),new Text(),new Text());
} public void set(Text left, Text right, Text third, Text fourth) {
this.first = left;
this.second = right;
this.third = third;
this.fourth = fourth;
} public Text getFirst() {
return first;
} public Text getSecond() {
return second;
} public Text getThird() {
return third;
} public Text getFourth() {
return fourth;
} @Override
public String toString() {
return first + "\t" + second + "\t" + third + "\t" + fourth;
} @Override
public void readFields(DataInput in) throws IOException {
first.readFields(in);
second.readFields(in);
third.readFields(in);
fourth.readFields(in);
}
@Override
public void write(DataOutput out) throws IOException {
System.out.println(out);
first.write(out);
second.write(out);
third.write(out);
fourth.write(out);
System.out.println("First = " + second.toString());
}
@Override
public int hashCode() {
return first.hashCode()* 157 + second.hashCode()*10 + third.hashCode();
} @Override
public boolean equals(Object right) {
if (right instanceof StrPair) {
StrPair r = (StrPair) right;
return first.equals(r.first) && second.equals(r.second) && third.equals(r.third) && fourth.equals(r.fourth);
} else {
return false;
}
} /** A Comparator that compares serialized StrPair. */
public static class Comparator extends WritableComparator {
public Comparator() {
super(StrPair.class);
} // 排序比较器,数据全部存在byte数组
public int compare(byte[] b1, int s1, int l1,
byte[] b2, int s2, int l2) {
// 二进制数组读取
int intvalue = readInt(b1, s1);
System.out.println("s1 = " + b1.length);
/*
int third = 0;
for(int i =s1 + 9; i<= s1+ 12; i++){
third += (b1[i]&0xff) << (24-8*i);
}
System.out.println("third = " + third); */
return compareBytes(b1, s1, l1, b2, s2, l2);
}
} static { // register this comparator
WritableComparator.define(StrPair.class, new Comparator());
} @Override
public int compareTo(StrPair o) {/*
if (first != o.first) {
return first < o.first ? -1 : 1;
} else if (second != o.second) {
return second < o.second ? -1 : 1;
}// else if (third != o.third) {
// return third < o.third ? -1 : 1;} return 0;
*/
return 0;
}
} /**
* Partition based on the first part of the pair.
*/
public static class FirstPartitioner extends Partitioner<StrPair,Text>{
@Override //
public int getPartition(StrPair key, Text value,
int numPartitions) {
return Math.abs(key.getFirst().hashCode() * 127) % numPartitions;
}
} /**
* Compare only the first part of the pair, so that reduce is called once
* for each value of the first part.
*/
public static class FirstGroupingComparator
implements RawComparator<StrPair> {
@Override
public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
return WritableComparator.compareBytes(b1, s1, Integer.SIZE/8,
b2, s2, Integer.SIZE/8);
} @Override
public int compare(StrPair o1, StrPair o2) {
Text l = o1.getFirst();
Text r = o2.getFirst();
return l.equals(r)?0:1;
// return l == r ? 0 : (l < r ? -1 : 1);
}
}

hadoop中compare函数的更多相关文章

  1. Hadoop中的各种排序

    本篇博客是金子在学习hadoop过程中的笔记的整理,不论看别人写的怎么好,还是自己边学边做笔记最好了. 1:shuffle阶段的排序(部分排序) shuffle阶段的排序可以理解成两部分,一个是对sp ...

  2. Hadoop中的Partitioner浅析

    转自:http://blog.csdn.net/b1198103958/article/details/47169105 Hadoop里面的MapReduce编程模型,非常灵活,大部分环节我们都可以重 ...

  3. Oracle 中 decode 函数用法

    Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译 ...

  4. Hadoop中WritableComparable 和 comparator

    1.WritableComparable 查看HadoopAPI,如图所示: WritableComparable继承自Writable和java.lang.Comparable接口,是一个Writa ...

  5. Hadoop中两表JOIN的处理方法(转)

    1. 概述 在传统数据库(如:MYSQL)中,JOIN操作是非常常见且非常耗时的.而在HADOOP中进行JOIN操作,同样常见且耗时,由于Hadoop的独特设计思想,当进行JOIN操作时,有一些特殊的 ...

  6. 学习hash_map从而了解如何写stl里面的hash函数和equal或者compare函数

    ---恢复内容开始--- 看到同事用unordered_map了所以找个帖子学习学习 http://blog.sina.com.cn/s/blog_4c98b9600100audq.html (一)为 ...

  7. Hadoop中两表JOIN的处理方法

    Dong的这篇博客我觉得把原理写的很详细,同时介绍了一些优化办法,利用二次排序或者布隆过滤器,但在之前实践中我并没有在join中用二者来优化,因为我不是作join优化的,而是做单纯的倾斜处理,做joi ...

  8. 1 weekend110的复习 + hadoop中的序列化机制 + 流量求和mr程序开发

    以上是,weekend110的yarn的job提交流程源码分析的复习总结 下面呢,来讲weekend110的hadoop中的序列化机制 1363157985066      13726230503  ...

  9. C++中的函数模板

    我们在定义函数时,可以通过定义函数模板,来简化一些功能相同而数据类型不同的函数的定义和调用过程. C++中的函数模板 对于类的声明来说,也有同样的问题.有时,有两个或多个类,其功能是相同的,仅仅是数据 ...

随机推荐

  1. JavaSE---位运算符

    1.Java支持的位运算符有7个: &:按位与 [2个相同取相同.2个不同取0] |:按位或 [2个相同取相同.2个不同取1] ~:按位非 ^:按位异或 [2个相同取0.2个不同取1] < ...

  2. 智能时代的到来,企业APP给企业带来的好处

    智能手机的出现给大家带来了巨大的便捷,无论是,文字沟通,购物点餐,开车导航,查看信息,生活服务,上下班打卡,交付各种费用,娱乐生活等都可以在一部小小的手机上来实现.随着智能手机的不断更新,越来越多的软 ...

  3. Idea创建Maven项目没有src

    第一次创建,下载非常慢,解决方法 1.配置环境变量 第二种:创建Maven项目时加上 archetypeCatalog=internal 参数 第三种:为自己的Maven配置国内镜像源 打开自己的 M ...

  4. eclipse启动的时候报错An internal error occurred during: "Initializing Java Tooling"

    eclipse ->windows ->Perspactive -> Reset perspactive 重置视图可以解决

  5. SWIG 和 Python——c/c++与脚本交互

    C 和 C++ 被公认为(理当如此)创建高性能代码的首选平台.对开发人员的一个常见要求是向脚本语言接口公开 C/C++ 代码,这正是 Simplified Wrapper and Interface ...

  6. asp.net webForm也可以这样用Ajax -- My Ajax Framework [全屏看文]

    对于asp.net WebForm项目,进行Ajax操作大概有三种方式:web服务(.asmx文件)  ,  一般处理程序(.ashx)和  一些Ajax控件. 对于.net提供的ajax控件,暂且不 ...

  7. 浅析正则表达式模式匹配的 String 方法

    在JavaScript代码中使用正则表达式进行模式匹配经常会用到String对象和RegExp对象的一些方法,例如replace.match.search等方法,以下是对一些方法使用的总结. Stri ...

  8. 关于webform textbox Password 模式

    textbox在这个模式时,如果进行点击按钮或者其他与后台交互的操作,则状态不会保留,既密码框内容会被清空: 这个可以在前台使用 隐藏控件加js获取密码框内容赋值到隐藏控件,点击刷新后通过后台为密码框 ...

  9. BZOJ3261: 最大异或和(可持久化trie树)

    题意 题目链接 Sol 设\(sum[i]\)表示\(1 - i\)的异或和 首先把每个询问的\(x \oplus sum[n]\)就变成了询问前缀最大值 可持久化Trie树维护前缀xor,建树的时候 ...

  10. 电路中GND和GROUND、VCC,VDD,VEE,VSS

    电路解析:GND和GROUND.VCC,VDD,VEE,VSS 参考: http://www.elecfans.com/dianzichangshi/20160822432514.html 一.解释版 ...