hadoop2.2编程: 重写comparactor
要点:
类型比较在hadoop的mapreduce中非常重要,主要用来比较keys;
hadoop中的RawComparator<T>接口继承自java的comparator, 主要用来比较序列化的objects;
- 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的更多相关文章
- hadoop2.2编程:使用MapReduce编程实例(转)
原文链接:http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html 从网上搜到的一篇hadoop的编程实例,对于初学者真是帮助太大 ...
- Hadoop2.2编程:新旧API的区别
Hadoop最新版本的MapReduce Release 0.20.0的API包括了一个全新的Mapreduce JAVA API,有时候也称为上下文对象. 新的API类型上不兼容以前的API,所以, ...
- hadoop2.2编程:自定义hadoop map/reduce输入文件切割InputFormat
hadoop会对原始输入文件进行文件切割,然后把每个split传入mapper程序中进行处理,FileInputFormat是所有以文件作为数据源的InputFormat实现的基类,FileInput ...
- hadoop2.2编程:各种API
hadoop2.2 API http://hadoop.apache.org/docs/r0.23.9/api/index.html junit API http://junit.org/javado ...
- hadoop2.2编程:DFS API 操作
1. Reading data from a hadoop URL 说明:想要让java从hadoop的dfs里读取数据,则java 必须能够识别hadoop hdfs URL schema, 因此我 ...
- hadoop2.2编程:mapreduce编程之二次排序
mr自带的例子中的源码SecondarySort,我重新写了一下,基本没变. 这个例子中定义的map和reduce如下,关键是它对输入输出类型的定义:(java泛型编程) public static ...
- hadoop2.2编程:MRUnit测试
引用地址:http://www.cnblogs.com/lucius/p/3442381.html examples: Overview This document explains how to w ...
- hadoop2.2编程: SequenceFileWritDemo
import java.io.IOException; import java.net.URI; import org.apache.hadoop.fs.FileSystem; import org. ...
- hadoop2.2编程:从default mapreduce program 来理解mapreduce
下面写一个default mapreduce 的程序: import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapr ...
随机推荐
- Android Animation 动画属性
在 Android 中, Animation 动画效果的实现可以通过两种方式进行实现: 一种是 tweened animation 渐变动画,另一种是 frame by frame animation ...
- C# 类是怎么执行的?
C# 类是怎么执行的? public class Person{ static person(){} //不写,默认也有个空的 public person(){}//不写,默认也有个空的 public ...
- SVN的那些事
1,终端报错:is too old (format 29) to work with client version '1.9.4 (r1740329)' (expects format 31). Yo ...
- 安装flash 插件scaleform出现错误:Scaleform Launch Panel.Launcher.handleDataLoaderIOError(): Loading XML Failedscaleform
经排查发现是Scaleform Launcher.swf报的错 (C:\Users\Administrator\AppData\Local\Adobe\Flash CC\zh_CN\Configura ...
- HDU 3442 Three Kingdoms(状态压缩 + BFS )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3442 题目大意:三国时期,刘备逃亡.给定一个最大为50*50的地图,刘备在地图中只能往4个方向走. 地 ...
- 窗口 namedWindow(), destroyWindow(), destroyAllWindows()[OpenCV 笔记6]
void namedWindow(const string& winname, int flags=WINDOW_AUTOSIZE); 创建一个窗口.imshow直接指定窗口名,可以省去此函数 ...
- 关于C++对汉字拼音的处理(3)
之所以汉字转拼音的博文能出到3,主要是因为没有很完美的C++的解决方案,但是写到了这里可以有一个小结了. 以前的方法都有这种那种弊端,如果出现了无法识别的汉字(简体的)就无法修改处理了,但是下面的这种 ...
- IOS 学习日志 2015-3-16
Objective--C 一 关键字 self 相当于java中的this,但是又有不同 它即可一代替对象,也可以代替类, 也就是说它既可以用在静态方法中又可以用在动态方法中. super 相当于父类 ...
- [翻译][MVC 5 + EF 6] 3:排序、过滤、分页
原文:Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application 1.添加排序: 1. ...
- ASP.NET缓存 Cache
缓存介绍 如果每次进入页面的时候都查询数据库生成页面内容的话,如果访问量非常大,则网站性能会非常差,而如果只有第一次访问的时候才查询数据库生成页面内容,以后都直接输出内容,则能提高系统性能,这样无论多 ...