在看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. vue proxyTable 接口跨域请求调试(五)

    在不同域之间访问是比较常见,在本地调试访问远程服务器....这就是有域问题. VUE解决通过proxyTable: 在 config/index.js 配置文件中 dev: { env: requir ...

  2. Django orm查询操作

    基于双下划线查询切记!!!!正向查询按字段,反向查询按表名的小写 正向:在Book表里设置关联Obj表,Book------>Obj就是正向查询 反向:在Book表里设置关联Obj表,Obj-- ...

  3. Linux的page cache使用情况/命中率查看和操控

    转载自宋宝华:https://blog.csdn.net/21cnbao/article/details/80458173 这里总结几个Linux文件缓存(page cache)使用情况.命中率查看的 ...

  4. linux上的常用命令

    删除目录及文件 (删除tem目录和所有.xml文件) rm -rf tem/ *.xml 复制文件 cp zoo_sample.cfg zoo.cfg 两台机的目录相互拷贝 scp -r apps/ ...

  5. Kure讲HTML_如何学习HTML

    HTML即是超文本标记语言,它主要是用来构建网页的轮廓的.HTML自身包含了众多的API(应用程序接口:即HTML暴露给Web前端开发者的语言特性,当然作为开发者就应该更多的关注这个.)话不多说,直接 ...

  6. css实现高度垂直居中

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

  7. 【计算机网络】一步一步学习IP路由流程

    TCP/IP协议簇是目前互联网应用最广的协议栈,谈到TCP/IP协议栈就不能不讲一讲IP路由的问题,因为在我们使用的网络通信中几乎每时每刻都在发生着IP路由的事件…….当你在网络世界中还是一位新手的时 ...

  8. pta5-9 Huffman Codes (30分)

    5-9 Huffman Codes   (30分) In 1953, David A. Huffman published his paper "A Method for the Const ...

  9. Hibernate课程 初探一对多映射3-1 单向多对一简介

    多对一的关系和关系数据库中的外键参照关系最匹配,即在己方的表中的一个外键参照另一个表中的主键! 通过在多方持有一方的引用来实现,需要在多的一方使用<many-to-one>来配置

  10. spring笔记4-事务管理

    一.xml配置文件形式 通过转账案例,学习事务管理 1.建立数据库 2.编写entity package huguangqin.com.cnblogs.entity; public class Use ...