Hadoop基础【1.1】 Writeable
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Hadoop基础【1.1】 Writeable,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3516字,纯文字阅读大概需要6分钟。
内容图文
![Hadoop基础【1.1】 Writeable](/upload/InfoBanner/zyjiaocheng/1328/107e88ea5d97407da3634d2b2e03496d.jpg)
MapReduce由于最耗时的是落盘与通信,所以采用了自定的Writeable的序列化反序列化(在结构化对象与二进制流之间的转换以便于节点通信:压缩过,使得节点之间带宽占用较少,可以快速读写),在Mapper Reducer里头常用的比如LongWriteable Text等等。我们根据不同的需求,对其进行一个复杂的定制,主要通过继承Writeable接口来实现。
源码如下
首先看下Writeable接口
package org.apache.hadoop.io; public interface Writable { void write(java.io.DataOutput var1) throws java.io.IOException; void readFields(java.io.DataInput var1) throws java.io.IOException; }
就俩接口,write和readFields,write用来序列化,readFileds用来反序列化
看下LongWriteable怎么实现的
1 import java.io.DataInput; 2 import java.io.DataOutput; 3 import java.io.IOException; 4 5 public class LongWritable implements WritableComparable<LongWritable> { 6privatelong value; 7 8public LongWritable() { 9 } 1011public LongWritable(long value) { 12this.set(value); 13 } 1415publicvoid set(long value) { 16this.value = value; 17 } 1819publiclong get() { 20returnthis.value; 21 } 2223publicvoid readFields(DataInput in) throws IOException { 24this.value = in.readLong(); 25 } 2627publicvoid write(DataOutput out) throws IOException { 28 out.writeLong(this.value); 29 } 3031publicboolean equals(Object o) { 32if (!(o instanceof LongWritable)) { 33returnfalse; 34 } else { 35 LongWritable other = (LongWritable)o; 36returnthis.value == other.value; 37 } 38 } 3940publicint hashCode() { 41return (int)this.value; 42 } 4344publicint compareTo(LongWritable o) { 45long thisValue = this.value; 46long thatValue = o.value; 47return thisValue < thatValue ? -1 : (thisValue == thatValue ? 0 : 1); 48 } 4950public String toString() { 51return Long.toString(this.value); 52 } 5354static { 55 WritableComparator.define(LongWritable.class, new LongWritable.Comparator()); 56 } 5758publicstaticclass DecreasingComparator extends LongWritable.Comparator { 59public DecreasingComparator() { 60 } 6162publicint compare(WritableComparable a, WritableComparable b) { 63returnsuper.compare(b, a); 64 } 6566publicint compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { 67returnsuper.compare(b2, s2, l2, b1, s1, l1); 68 } 69 } 7071publicstaticclass Comparator extends WritableComparator { 72public Comparator() { 73super(LongWritable.class); 74 } 7576publicint compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { 77long thisValue = readLong(b1, s1); 78long thatValue = readLong(b2, s2); 79return thisValue < thatValue ? -1 : (thisValue == thatValue ? 0 : 1); 80 } 81 } 82 }
WriteableComparable如下
WritableComparable是Hadoop的排序方式之一,而排序是MapReduce框架中最重要的操作之一,它就是用来给数据排序的(按照Key排好),常发生在MapTask与ReduceTask的传输过程中(就是数据从map方法写到reduce方法之间,shuffle呗?)
public interface WritableComparable<T> extends Writable, Comparable<T> { }
21为止getter setter加简单的构造函数,50-52为toString,23-29实现Writable里的两个方法(DataOutput.writeLong&DataInput.readLong),44-48为Comparable的compareTo,然后Object为LongWriteable且value同则equals返回true,hashcode方法返回value
对于简单的仅在Map的输出和Reduce的输入这儿用的的地方来说,一般compareTo,toString,write,readFields写完就ok了
然后再往下看?Comparator?啥玩意?
WritableComparator(54-81行)
WritableComparator类大致类似于一个注册表,里面记录了所有Comparator类的集合。Comparators成员用一张Hash表记录Key=Class,value=WritableComprator的注册信息。(PS:工厂模式)
它继承了RawComparator,RawComparator是用来实现直接比较数据流中的记录,无需先把数据流序列化为对象,这样便避免了新建对象的额外开销。
因此54-56为static块把LongWriteable“注册了”,71-80就是LongWriteable在static块里头要注册的Comparator(我大1,我小-1,我相等就0)( API这么写的 This base implemenation uses the natural ordering. To define alternate orderings)看起来不大清楚是干嘛的。。。
尝试了下在wordcount里头,把Reduce的output变成自己定义的,没写Comparator的StupidIntWritable,但是也能正常输出。。。我这就迷惑了。。。再想想把。。。
原文:https://www.cnblogs.com/tillnight1996/p/12317072.html
内容总结
以上是互联网集市为您收集整理的Hadoop基础【1.1】 Writeable全部内容,希望文章能够帮你解决Hadoop基础【1.1】 Writeable所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。