要点

  1. 类型比较在hadoop的mapreduce中非常重要,主要用来比较keys;

  2. hadoop中的RawComparator<T>接口继承自java的comparator, 主要用来比较序列化的objects;

  3. hadoop中的WritableComparator class更全面,提供了两种主要的比较方法,一种是直接比较object,另一种是较serialized representations;

    举例来说 比较object: compare(new IntWritable(21), new IntWritable(998)); 比较serialized representations: compare(serialize(new       IntWritable(21)), serialize(new IntWritable(998))),

提示:继承关系

1.org.apache.hadoop.io
Interface RawComparator<T>
//description
public interface RawComparator<T>
extends Comparator<T>
//method
int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2)
2.org.apache.hadoop.io
Interface WritableComparable<T>
//description
public interface WritableComparable<T>
extends Writable, Comparable<T>
//method
Methods inherited from interface org.apache.hadoop.io.Writable
readFields, write
3.java.lang.Object
     |__ org.apache.hadoop.io.WritableComparator
//description
public class WritableComparator
extends Object
implements RawComparator
//methods
int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2)
int compare(Object a, Object b)
int compare(WritableComparable a, WritableComparable b)
static int compareBytes(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2)
4.java.util
Interface Comparator<T>
//description
public interface Comparator<T>
//methods
int compare(T o1, T o2)
boolean equals(Object obj)

代码:

 import java.lang.Byte;
 import java.io.DataOutputStream;
 import java.io.ByteArrayOutputStream;

 import org.apache.hadoop.io.IntWritable;
 import org.apache.hadoop.io.WritableComparator;
 import org.apache.hadoop.io.RawComparator;

 public class MyIntWritableComparactor {

   public static byte[] serialize(IntWritable writable) throws Exception {
     ByteArrayOutputStream out = new ByteArrayOutputStream();
     DataOutputStream dataOut = new DataOutputStream(out);
     writable.write(dataOut);
     dataOut.close();
     return out.toByteArray();
   }

   @SuppressWarnings("unchecked")
   public static void main(String[] args) throws Exception {
     RawComparator<IntWritable> comparator = WritableComparator.get(IntWritable.class);
     IntWritable w1 = new IntWritable(13);
     IntWritable w2 = new IntWritable(12);
     System.out.println("w1: " + w1 + " w2: " + w2);
     System.out.println("w1 compare w2 : " + comparator.compare(w1,w2));

     byte[] b1 = serialize(w1);
     byte[] b2 = serialize(w2);
     System.out.println("b1.length: " + b1.length);
     System.out.println("b2.length: " + b2.length);
     System.out.println("b1.length compare b2.length: " + comparator.compare(b1, 0, b1.length, b2, 0, b2.length));

   }
 }

编译,运行:

//注意我用的是hadoop2.2
$ source $YARN_HOME/libexec/hadoop-config.sh
$ mkdir myclass
$ javac -d myclass MyIntWritableCompare.java
$ jar -cvf  mycompare.jar -C myclass ./
$ export HADOOP_CLASSPATH=$CLASSPATH:mycompare.jar
$ yarn MyIntWritableCompare

输出:

$ yarn jar text.jar Text
w1:  w2:
w1 compare w2 :
b1.length:
b2.length:
b1.length compare b2.length: 

hadoop2.2编程: 重写comparactor的更多相关文章

  1. hadoop2.2编程:使用MapReduce编程实例(转)

    原文链接:http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html 从网上搜到的一篇hadoop的编程实例,对于初学者真是帮助太大 ...

  2. Hadoop2.2编程:新旧API的区别

    Hadoop最新版本的MapReduce Release 0.20.0的API包括了一个全新的Mapreduce JAVA API,有时候也称为上下文对象. 新的API类型上不兼容以前的API,所以, ...

  3. hadoop2.2编程:自定义hadoop map/reduce输入文件切割InputFormat

    hadoop会对原始输入文件进行文件切割,然后把每个split传入mapper程序中进行处理,FileInputFormat是所有以文件作为数据源的InputFormat实现的基类,FileInput ...

  4. hadoop2.2编程:各种API

    hadoop2.2 API http://hadoop.apache.org/docs/r0.23.9/api/index.html junit API http://junit.org/javado ...

  5. hadoop2.2编程:DFS API 操作

    1. Reading data from a hadoop URL 说明:想要让java从hadoop的dfs里读取数据,则java 必须能够识别hadoop hdfs URL schema, 因此我 ...

  6. hadoop2.2编程:mapreduce编程之二次排序

    mr自带的例子中的源码SecondarySort,我重新写了一下,基本没变. 这个例子中定义的map和reduce如下,关键是它对输入输出类型的定义:(java泛型编程) public static ...

  7. hadoop2.2编程:MRUnit测试

    引用地址:http://www.cnblogs.com/lucius/p/3442381.html examples: Overview This document explains how to w ...

  8. hadoop2.2编程: SequenceFileWritDemo

    import java.io.IOException; import java.net.URI; import org.apache.hadoop.fs.FileSystem; import org. ...

  9. hadoop2.2编程:从default mapreduce program 来理解mapreduce

    下面写一个default mapreduce 的程序: import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapr ...

随机推荐

  1. SetTimer的使用

    SetTimer函数用于创建一个计时器,KillTimer函数用于销毁一个计时器.计时器属于系统资源,使用完应及时销毁. SetTimer的函数原型如下:UINT_PTR SetTimer( HWND ...

  2. JDK+Tomcat+MyEclipse发布JSP项目——不能成功的问题

    JDK.Tomcat和MyEclipse安装完成后,在将web Project发布到Tomcat时出现金叹号,如图 原因:由于将Tomcat安装在C盘,所以需要启用管理员权限. 解决途径:用管理员身份 ...

  3. asp.net中Get请求和Post请求

    Get和Post请求的区别:Get请求因为传输的数据在URL中,因此不安全,而且多数浏览器有限制其长度,最长为2KB.通过Get请求获取数据的方式:string strName=context.Req ...

  4. ios PromiseKit

    简介: 高级开发是高度异步的,PromiseKit收集了一些帮助函数,让我们开发过程中使用的典型异步模式更加令人愉悦. 1.通过pod安装promisekit: 2. promise.h介绍 @imp ...

  5. spring boot 配置文件提示自定义配置属性

    1.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  6. asp.net:repeater嵌套(常用于新闻等在首页归类显示)

    using System;using System.Configuration;using System.Collections.Generic;using System.Linq;using Sys ...

  7. 利用ORACLE ADV 功能完成SQL TUNING 调优(顾问培训) “让DBA失业还是解脱?”

    oracle自动判断SQL性能功能. 11G的ADV,建议.SNAPSHOT,数据集合, 存储在oracle sys $_开头的表(10几条).  创建SNAPSHOT时选择天数, 默认14天. sq ...

  8. std::map的操作:插入、修改、删除和遍历

    using namespace std; std::map<int,int> m_map; 1.添加 for(int i=0;i<10;i++) { m_map.insert(mak ...

  9. foreach的一点理解

    首先什么样的数据才能实现foreach 1 实现IEnumerable这个接口 2 有GetEnumerable()这个方法 然后为啥实现这个接口或者有这个方法就可以实现foreach遍历 首先我先用 ...

  10. ubuntu上 安装 基于sphinx 的 coreseek 全文搜索

    原生sphinx不支持中文, sphinx-for-chinese匹配中文时也不返回结果 ,真纠结,  最好试了 coreseek,这个能正确返回结果了, 所以记录一下 1 http://www.co ...