`
eimhee
  • 浏览: 2112544 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

hadoop中的Writable分析

    博客分类:
  • JAVA
阅读更多

 

hadoop 要使一个类能序例化, 要实现Writable接口, Writable 调用DataInput和DataOutput实现序例化。 

DataOutput是JDK中IO包下的一个类, 提供了writeBoolean, writeByte, writeShort。等方法了。

这样让用户决定哪一个字段序例化, 怎么反序例化。

在org.apache.hadoop.io包下包含了大量的可序列化的组件,它们都实现了Writable接口,Writable接口提供了两个方法,write和readFields,分别用来序列化和反序列化。

 

 

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.hadoop.io;

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

import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;

/**
 * A serializable object which implements a simple, efficient, serialization 
 * protocol, based on {@link DataInput} and {@link DataOutput}.
 *
 * <p>Any <code>key</code> or <code>value</code> type in the Hadoop Map-Reduce
 * 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>
 */
@InterfaceAudience.Public
@InterfaceStability.Stable
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;
}
 

 

1
1
分享到:
评论

相关推荐

    Hadoop权威指南 第二版(中文版)

     本书是Hadoop权威参考,程序员可从中探索如何分析海量数据集,管理员可以从中了解如何安装与运行Hadoop集群。 目录 第1章 初识Hadoop  数据!数据!  数据存储与分析  与其他系统相比  关系型数据库管理系统...

    Hadoop权威指南(中文版)2015上传.rar

    使用Hadoop分析数据 map阶段和reduce阶段 横向扩展 合并函数 运行一个分布式的MapReduce作业 Hadoop的Streaming Ruby版本 Python版本 Hadoop Pipes 编译运行 第3章 Hadoop分布式文件系统 HDFS的设计 HDFS的概念 数据...

    hadoop 权威指南(第三版)英文版

    hadoop权威指南第三版(英文版)。 Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii Preface . . . . . . ....

    Hadoop大作业排序.zip

    Hadoop大作业排序代码 由于 MapReduce 中对 key 进行比较和排序,而 key 可以是任何实 现了 Writable 接口的类。 在 java 中,要实现类的大小比较可以实现 Comparable 接口并通 过重写 compareTo 方法来实现。 在 ...

    java-Hadoop序列化

     序列化概念 ... Hadoop的序列化格式:Writable  序列化格式特点:  紧凑:高效使用存储空间。  快速:读写数据的额外开销小  可扩展:可透明地读取老格式的数据  互操作:支持多语言的交互

    Hadoop中MapReduce基本案例及代码(二)

    当自定义一个类之后,如果想要产生的对象在hadoop中进行传输,那么需要 这个类实现Writable的接口进行序列化/反序列化 案例:统计每个人产生的总流量 数据源 自定义类序列化 import java.io.DataInput; import java...

    hadoop_the_definitive_guide_3nd_edition

    Hadoop definitive 第三版, 目录如下 1. Meet Hadoop . . . 1 Data! 1 Data Storage and Analysis 3 Comparison with Other Systems 4 RDBMS 4 Grid Computing 6 Volunteer Computing 8 A Brief History of Hadoop 9...

    Hadoop 培训课程(3)MapReduce_1

    Hadoop 培训课程(3)MapReduce_1 MapReduce原理*** MapReduce执行过程** 数据类型与格式*** Writable接口与序列化机制*** ---------------------------加深拓展---------------------- MapReduce的执行过程源码分析

    16_尚硅谷大数据之MapReduce_Hadoop序列化1

    2.1 序列化概述 2.2 常用数据序列化类型 2.3 自定义 bean 对象实现序列化接口(Writable) 2.4 序列化案例实操

    largecollections-retired:使用堆外内存扩展到数百万个元素的 HashMap 实现

    所有键/值类都需要支持 java.io.Serializable 或 org.apache.hadoop.io.Writable。 注意:不要将 Serializable 与 Writable 混用。 例如,不要使用列表。 不起作用。 在需要使用 List 的情况下使用 ArrayWritable ...

    geterdun:确保弱秩序的幂等动作“完成”的简单框架

    此外,Geterdun能够写入支持附加操作的Hadoop API中的任何文件系统,因此可以可靠地存储用于实现此目标的事务日志。用法首先,使用GeterDun.geterDun(...)工厂方法创建一个新的GeterDun实例。 有一个签名较短的方法...

Global site tag (gtag.js) - Google Analytics