在看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. 多个ajax请求时控制执行顺序或全部执行后的操作

    1.当确保执行顺序时 (1)请求加async: false,,这样所有的ajax就会同步执行,请求顺序就是代码顺序: (2)$.when   确保所有异步的ajax请求完毕时 $.when($.aja ...

  2. hybird app 工具选型

    目前hybird app工具众多,如何选择?哪个坑少点呢? 下面来分析一下: 1开发工具都开源.基于Eclipse的有:apicloud,WeX5 2热门指数.下面的百度的搜索结果数,代表不了什么,至 ...

  3. SCAU 算法课的题

    8594 有重复元素的排列问题(优先做) 时间限制:1000MS  内存限制:1000K提交次数:1610 通过次数:656 题型: 编程题   语言: G++;GCC;VC Description ...

  4. webview的进度条的加载,webview的使用以及handle的理解与使用

    Webview的几个关键方法要介绍一些: 谷歌官方文档是这么说的; A WebView has several customization points where you can add your ...

  5. css实现高度垂直居中

    1:单行文字垂直居中: 如果一个容器中只有一行文字的话,定义height(高度)和 line-height(行高)相等即可. 如:<div style="height:25px;lin ...

  6. Jersey初始化配置

    一 实际项目配置 公司VIP平台因为业务的特殊性,对业务数据的操作.以及前后端解耦等要求,使用到了jersey框架.同时使用到了spring框架. 二 jersey初始化 配置web项目配置文件web ...

  7. [转]png图片压缩大小但是不改变透明部分

    降低PNG图片存储大小方法,图片压缩方法,如何降低PNG图片存储大小?前提是分辨率和尺寸大小不变,图形的透明部分不变.请看如下办法,亲测可用. 1. 将PNG图片用PS打开. 2. 图像-模式-8位/ ...

  8. nopcommerce 3.6网银在线支付插件(源码)

    网银在线支付插件,下载后通过后台插件管理安装.配置即可使用. 下载:网银在线支付插件3.1.3.6版.rar (106.3KB) 源代码放在\Plugins目录下,用vs打开重新生成. 源地址:htt ...

  9. cf914D. Bash and a Tough Math Puzzle(线段树)

    题意 题目链接 Sol 直接在线段树上二分 当左右儿子中的一个不是\(x\)的倍数就继续递归 由于最多递归到一个叶子节点,所以复杂度是对的 开始时在纠结如果一段区间全是\(x\)的两倍是不是需要特判, ...

  10. .net函数

    Math.Ceiling() Math.Floor() == 向上取整,向下取整 Regex.Split(productListControl.Text, "\n", RegexO ...