package org.apache.hadoop.io;

import java.io.DataOutput;
import java.io.DataInput;
import java.io.IOException;

/**
* A serializable object which implements a simple, efficient,

一个序列化的对象,这个家伙实现了一个简单、高效、序列化的协议,它是基于DataInput和DataOutput这两个IO对象的
* protocol, based on {@link DataInput} and {@link DataOutput}.
*
* <p>Any <code>key</code> or <code>value</code> type in the Hadoop Map-Reduce

在hadoop Map-Reduce框架中任何基于key-value类型的的数据都是实现的这个接口
* framework implements this interface.</p>
*
* <p>Implementations typically implement a static <code>read(DataInput)</code>
* method which constructs a new instance, calls {@link #readFields(DataInput)}
* and returns the instance.</p>
*
* <p>Example:</p>
* <p><blockquote><pre>
*     public class MyWritable implements Writable {
*       // 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 static MyWritable read(DataInput in) throws IOException {
*         MyWritable w = new MyWritable();
*         w.readFields(in);
*         return w;
*       }
*     }
* </pre></blockquote></p>
*/
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;
}

-----------------------------------------------------------------------------------------------------------------------------------

int
readInt()
          读取四个输入字节并返回一个 int 值。

long
readLong()
          读取八个输入字节并返回一个 long 值。

-----------------------------------------------------------------------------------------------------------------------------------

void
writeInt(int v)
          将一个 int 值写入输出流,该值由四个字节组成。

void
writeLong(long v)
          将一个 long 值写入输出流,该值由八个字节组成。

-----------------------------------------------------------------------------------------------------------------------------------------

/** A polymorphic Writable that writes an instance with it's class name.
* Handles arrays, strings and primitive types without a Writable wrapper.
*/
public class ObjectWritable implements Writable, Configurable {

private Class declaredClass;
  private Object instance;
  private Configuration conf;

-------------------------------------------------------------------------------------------------------------------------------------------

Writable writable = WritableFactories.newInstance(instanceClass, conf);
      writable.readFields(in);
      instance = writable;

踏着前人的脚印学Hadoop——序列化,Writerable的更多相关文章

  1. 踏着前人的脚印学Hadoop——RPC源码

    A simple RPC mechanism.A protocol  is a Java interface.  All parameters and return types must be one ...

  2. 踏着前人的脚印学Hadoop——结构、重点

    HDFS作为一个分布式文件系统,是所有这些项目的基础.分析好HDFS,有利于了解其他系统.由于Hadoop的HDFS和MapReduce是同一个项目,我们就把他们放在一块,进行分析. 如果把整个had ...

  3. 踏着前人的脚印学hadoop——ipc中的Server

    1.An abstract IPC service.  IPC calls take a single {@link Writable} as a parameter, and return a {@ ...

  4. 踏着前人的脚印学hadoop——ipc中的Client

    1.Client有五个内部类,分别是Call,ParallelCall,ParallelResult,Connetion,ConnectionId 其实这五个类就是去完成两件事情的,一件事情是连接,另 ...

  5. Hadoop阅读笔记(六)——洞悉Hadoop序列化机制Writable

    酒,是个好东西,前提要适量.今天参加了公司的年会,主题就是吃.喝.吹,除了那些天生话唠外,大部分人需要加点酒来作催化剂,让一个平时沉默寡言的码农也能成为一个喷子!在大家推杯换盏之际,难免一些画面浮现脑 ...

  6. Hadoop序列化

      遗留问题: Hadoop序列化可以复用对象,是在哪里复用的? 介绍Hadoop序列化机制 Hadoop序列化机制详解 Hadoop序列化的核心 Hadoop序列化的比较接口 ObjectWrita ...

  7. hadoop序列化机制与java序列化机制对比

    1.采用的方法: java序列化机制采用的ObjectOutputStream 对象上调用writeObject() 方法: Hadoop 序列化机制调用对象的write() 方法,带一个DataOu ...

  8. Hadoop序列化与Java序列化

    序列化就是把内存中的对象的状态信息转换成字节序列,以便于存储(持久化)和网络传输 反序列化就是就将收到的字节序列或者是硬盘的持久化数据,转换成内存中的对象. 1.JDK的序列化 只要实现了serial ...

  9. 学Hadoop还是Spark好?

    JS 相信看这篇文章的你们,都和我一样对Hadoop和Apache Spark的选择有一定的疑惑,今天查了不少资料,我们就来谈谈这两种 平台的比较与选择吧,看看对于工作和发展,到底哪个更好. 一.Ha ...

随机推荐

  1. 三大平衡树(Treap + Splay + SBT)总结+模板[转]

    Treap树 核心是 利用随机数的二叉排序树的各种操作复杂度平均为O(lgn) Treap模板: #include <cstdio> #include <cstring> #i ...

  2. JavaScript 同名方法的处理

    在JS中,如果存在同名同参的方法,它会先调用哪一个?先看两个例子: 例1: <html> <head> <title></title> <scri ...

  3. git tag之后如何修改

    先 git clone 整个仓库,然后 git checkout tag_name 就可以取得 tag 对应的代码了. 但是这时候 git 可能会提示你当前处于一个“detached HEAD&quo ...

  4. computer English

    算法常用术语中英对照Data Structures 基本数据结构Dictionaries 字典PriorityQueues 堆Graph Data Structures 图Set Data Struc ...

  5. 在ubuntu(linux)下安装vim,以及vim的常用命令

    介绍vim: vim是一种编辑器,自我感觉这东西好用,就是现在有些不太习惯 如何安装: 如果是使用redhat ,系统自带着vi和vim 如果是使用ubuntu,建议使用apt-get命令, 可以尝试 ...

  6. cf(#div1 B. Dreamoon and Sets)(数论)

    B. Dreamoon and Sets time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. Struts2动态结果(${})and全局结果(global-results)

    动态结果 例:根据判定动态结果区分用户进行登录 1.先在TestAction类中进行判定赋值 public class TestAction extends ActionSupport { priva ...

  8. uva 10118

    10118 - Free Candies Time limit: 30.000 seconds Little Bob is playing a game. He wants to win some c ...

  9. Aeroplane chess(HDU 4405)

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  10. 8月10日 微软MVP巡讲 Windows 开发专题活动

    1.签到 2.准备工作 3.讲师正在准备 4.讲师发言 5.本次活动进行中 合影