MapReduce API 基本概念

來源:互聯網
上載者:User

標籤:

在正式分析新舊 API 之前, 先要介紹幾個基本概念。 這些概念貫穿於所有 API 之中,因此, 有必要單獨講解。

1.序列化

序列化是指將結構化對象轉為位元組流以便於通過網路進行傳輸或寫入持久儲存的過程。還原序列化指的是將位元組流轉為結構化對象的過程。 在 Hadoop MapReduce 中, 序列化的主
要作用有兩個: 永久儲存和處理序間通訊。為了能夠讀取或者儲存 Java 對象, MapReduce 編程模型要求使用者輸入和輸出資料中的 key 和 value 必須是可序列化的。 在 Hadoop MapReduce 中 , 使一個 Java 對象可序列化的方法是讓其對應的類實現 Writable 介面 。 但對於 key 而言,由於它是資料排序的關鍵字, 因此還需要提供比較兩個 key 對象的方法。 為此,key對應類需實現WritableComparable 介面 , 它的類:

在package org.apache.hadoop.io 中的WritableComparable.java檔案中定義:

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

再來看看Writable介面的定義:

public interface Writable {      /**        * Serialize the fields of this object to <code>out</code>.       *        * @param out <code>DataOuput</code> to serialize this object into.       * @throws IOException       */      void write(DataOutput out) throws IOException;      /**        * Deserialize the fields of this object from <code>in</code>.         *        * <p>For efficiency, implementations should attempt to re-use storage in the        * existing object where possible.</p>       *        * @param in <code>DataInput</code> to deseriablize this object from.       * @throws IOException       */      void readFields(DataInput in) throws IOException;    }

可以很明顯的看出,write(DataOutput out)方法的作用是將指定對象的域序列化為out相同的類型;readFields(DataInput in)方法的作用是將in對象中的域還原序列化,考慮效率因素,實現介面的時候應該使用已經存在的Object Storage Service。

DataInput介面定義原始碼如下:

publicinterface DataInput {   void readFully(byte b[]) throws IOException;   void readFully(byte b[], int off, int len) throws IOException;   int skipBytes(int n) throws IOException;   boolean readBoolean() throws IOException;   byte readByte() throws IOException;   int readUnsignedByte() throws IOException;   short readShort() throws IOException;      int readUnsignedShort() throws IOException;    char readChar() throws IOException;   int readInt() throws IOException;      long readLong() throws IOException;      float readFloat() throws IOException;      double readDouble() throws IOException;      String readLine() throws IOException;   String readUTF() throws IOException;}

每個方法的含義差不多,具體可參見java jdk源碼

DataOutput介面定義原始碼如下:

publicinterface DataOutput {      void write(int b) throws IOException;      void write(byte b[]) throws IOException;    void write(byte b[], int off, int len) throws IOException;       void writeBoolean(boolean v) throws IOException;    void writeByte(int v) throws IOException;    void writeShort(int v) throws IOException;    void writeChar(int v) throws IOException;    void writeInt(int v) throws IOException;    void writeLong(long v) throws IOException;    void writeFloat(float v) throws IOException;    void writeDouble(double v) throws IOException;    void writeBytes(String s) throws IOException;        void writeChars(String s) throws IOException;    void writeUTF(String s) throws IOException;}

WritableComparable可以用來比較,通常通過Comparator . 在hadoop的Map-Reduce架構中任何被用作key的類型都要實現這個介面。

看一個例子:

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 &lt; thatValue ? -1 : (thisValue==thatValue ? 0 : 1));       }}
2.Reporter 參數

Reporter 是 MapReduce 提供給應用程式的工具。 ,應用程式可使用Reporter 中的方法報告完成進度(progress)、設定狀態訊息(setStatus 以及更新計數器( incrCounter)。

Reporter 是一個基礎參數。 MapReduce 對外提供的大部分組件, 包括 InputFormat、Mapper 和 Reducer 等,均在其主要方法中添加了該參數。

3.回調機制

回調機制是一種常見的設計模式。它將工作流程內的某個功能按照約定的介面暴露給外部使用者, 為外部使用者提供資料,或要求外部使用者提供資料。
Hadoop MapReduce 對外提供的 5 個組件( InputFormat、 Mapper、 Partitioner、 Reducer 和 OutputFormat) 實際上全部屬於回調介面 。 當使用者按照約定實現這幾個介面後, MapReduce運行時環境會自 動調用它們。,MapReduce 給使用者暴露了介面 Mapper, 當使用者按照自己的應用程式邏輯實現自己的 MyMapper 後,Hadoop MapReduce 運行時環境會將輸入資料解析成 key/value 對, 並調用 map() 函數迭代處理。

參考資料

《Hadoop技術內幕 深入理解MapReduce架構設計與實現原理》

MapReduce API 基本概念

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.