• Hadoop WritableComparable接口


    WritableComparable接口

    Writable接口大家可能都知道,它是一个实现了序列化协议的序列化对象。在Hadoop中定义一个结构化对象都要实现Writable接口,使得该结构化对象可以序列化为字节流,字节流也可以反序列化为结构化对象。那WritableComparable接口可序列化并且可比较的接口。MapReduce中所有的key值类型都必须实现这个接口,既然是可序列化的那就必须得实现readFiels()write()这两个序列化和反序列化函数,既然也是可比较的那就必须得实现compareTo()函数,该函数即是比较和排序规则的实现。这样MR中的key值就既能可序列化又是可比较的。下面几符图是API中对WritableComparable接口的解释及其方法,还有一个实现了该接口的对象的列子:

    public interface WritableComparable<T>
         extends 
         Writable, 
         Comparable<T>
        
    
    

    A Writable which is alsoComparable.

    WritableComparables can be compared to each other, typically via Comparators. Any type which is to be used as a key in the Hadoop Map-Reduce framework should implement this interface.

    Example:

         public class MyWritableComparable implements WritableComparable {
           // Some data
           private int counter;
           private long timestamp;
           
           public void write(DataOutput out) throws IOException {
             out.writeInt(counter);
             out.writeLong(timestamp);
           }
           
           public void readFields(DataInput in) throws IOException {
             counter = in.readInt();
             timestamp = in.readLong();
           }
           
           public int compareTo(MyWritableComparable w) {
             int thisValue = this.value;
             int thatValue = ((IntWritable)o).value;
             return (thisValue < thatValue ? -1 : (thisValue==thatValue ? 0 : 1));
           }
         }
     
  • 相关阅读:
    在内部局域网内搭建HTTPs
    如何创建自己的Nuget包
    Scratch入门
    灰度发布 & 蓝绿部署
    .net异步方法初探
    [转]批处理静默自动安装证书
    住院病案首页数据填写质量规范(暂行)
    DRGs
    【规范】电子病历系统功能规范(试行)
    Red Hat Enterprise Linux 8配置YUM源的两种方式
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3228579.html
Copyright © 2020-2023  润新知