Hadoop中的Writable分析

2019-03-28 13:38|来源: 网络

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

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

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

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

  1. /**  
  2.  * Licensed to the Apache Software Foundation (ASF) under one  
  3.  * or more contributor license agreements.  See the NOTICE file  
  4.  * distributed with this work for additional information  
  5.  * regarding copyright ownership.  The ASF licenses this file  
  6.  * to you under the Apache License, Version 2.0 (the  
  7.  * "License"); you may not use this file except in compliance  
  8.  * with the License.  You may obtain a copy of the License at  
  9.  *  
  10.  *     http://www.apache.org/licenses/LICENSE-2.0  
  11.  *  
  12.  * Unless required by applicable law or agreed to in writing, software  
  13.  * distributed under the License is distributed on an "AS IS" BASIS,  
  14.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  15.  * See the License for the specific language governing permissions and  
  16.  * limitations under the License.  
  17.  */  
  18.   
  19. package org.apache.hadoop.io;   
  20.   
  21. import java.io.DataOutput;   
  22. import java.io.DataInput;   
  23. import java.io.IOException;   
  24.   
  25. import org.apache.hadoop.classification.InterfaceAudience;   
  26. import org.apache.hadoop.classification.InterfaceStability;   
  27.   
  28. /**  
  29.  * A serializable object which implements a simple, efficient, serialization   
  30.  * protocol, based on {@link DataInput} and {@link DataOutput}.  
  31.  *  
  32.  * <p>Any <code>key</code> or <code>value</code> type in the Hadoop Map-Reduce  
  33.  * framework implements this interface.</p>  
  34.  *   
  35.  * <p>Implementations typically implement a static <code>read(DataInput)</code>  
  36.  * method which constructs a new instance, calls {@link #readFields(DataInput)}   
  37.  * and returns the instance.</p>  
  38.  *   
  39.  * <p>Example:</p>  
  40.  * <p><blockquote><pre>  
  41.  *     public class MyWritable implements Writable {  
  42.  *       // Some data       
  43.  *       private int counter;  
  44.  *       private long timestamp;  
  45.  *         
  46.  *       public void write(DataOutput out) throws IOException {  
  47.  *         out.writeInt(counter);  
  48.  *         out.writeLong(timestamp);  
  49.  *       }  
  50.  *         
  51.  *       public void readFields(DataInput in) throws IOException {  
  52.  *         counter = in.readInt();  
  53.  *         timestamp = in.readLong();  
  54.  *       }  
  55.  *         
  56.  *       public static MyWritable read(DataInput in) throws IOException {  
  57.  *         MyWritable w = new MyWritable();  
  58.  *         w.readFields(in);  
  59.  *         return w;  
  60.  *       }  
  61.  *     }  
  62.  * </pre></blockquote></p>  
  63.  */  
  64. @InterfaceAudience.Public   
  65. @InterfaceStability.Stable   
  66. public interface Writable {   
  67.   /**   
  68.    * Serialize the fields of this object to <code>out</code>.  
  69.    *   
  70.    * @param out <code>DataOuput</code> to serialize this object into.  
  71.    * @throws IOException  
  72.    */  
  73.   void write(DataOutput out) throws IOException;   
  74.   
  75.   /**   
  76.    * Deserialize the fields of this object from <code>in</code>.    
  77.    *   
  78.    * <p>For efficiency, implementations should attempt to re-use storage in the   
  79.    * existing object where possible.</p>  
  80.    *   
  81.    * @param in <code>DataInput</code> to deseriablize this object from.  
  82.    * @throws IOException  
  83.    */  
  84.   void readFields(DataInput in) throws IOException;   
  85. }  

相关问答

更多
  • 做运维 ,又不需要太好的java ,最基础的Linux 环境配置,
  • 基本上我在这种情况下无法理解如何构造Test对象。 是的,你错过了这一点。 如果你需要构造一个Test实例并填充atoms ,那么你需要在Test添加一个构造函数: public Test(ArrayList atoms) { this.atoms = atoms; } 或者您需要使用默认构造函数并添加一个方法或一个setter,它允许您向atoms添加项或设置atoms的值。 后者实际上在Hadoop框架中很常见,有一个默认的构造函数和一个set方法。 参见,例 ...
  • TLDR :你只需要彻底移除你的WikiComparator ,而不是完全调用job.setGroupingComparatorClass 。 说明 :组比较器用于比较地图输出键,而不是地图输出值。 您的地图输出键是Text对象,值是WikiWritable对象。 这意味着传递给比较器进行反序列化的字节代表序列化的Text对象。 但是, WikiComparator使用反射来创建WikiWritable对象(如其构造函数中所述),然后尝试使用WikiWritable.readFields方法反序列化Text ...
  • Hadoop有asm 3.2而我使用的是ASM 5.在ASM5中,ClassVisitor是一个超类,而在3.2中它是一个接口。 出于某种原因,错误是Throwable(信任Shevek),catch块只捕获异常。 任何hadoop日志都没有捕获throwable错误。 因此,调试非常困难。 使用jar jar链接修复asm版本问题,现在一切正常。 如果你正在使用Hadoop并且某些东西不起作用并且没有日志显示任何错误,那么请尝试抓住Throwable。 阿伦 Hadoop had asm 3.2 and ...
  • 好的,在我的代码中找到了错误。 首先,最小的工作示例在类DatumObject没有实现equals方法: @Override public boolean equals(Object obj) { if(obj == null) return false; if(!(obj instanceof DatumObject)) return false; DatumObject other = (DatumObject) obj; re ...
  • LongWritable minDay = new LongWritable()将最小日期变量初始化为1970。 更确切地说:除非给定特定值, LongWritable根据java语言规范将其内部long初始化为0。 当它被输入java.util.Date ,它将被解释为Unix时代的0毫秒: January 1, 1970, 00:00:00 UTC 。 我的猜测是1970年是数据集中所有日期值的下限。 这将写入每个密钥。 我注意到你使用int min = Integer.MAX_VALUE初始化关闭值。 ...
  • 在Hadoop中没有可写日历/日期。 考虑到您可以从Calendar对象获取timeInMillis作为long,您可以使用LongWritable来序列化日历对象,当且仅当您的应用程序始终使用默认的UTC时区时(即它对时区“不可知”,它始终假定那个timeInMillis代表一个UTC时间)。 如果您使用其他时区或者您的应用程序需要能够针对不同的时区解释timeInMillis,则必须从头开始编写默认的可写实现。 There is no Writable for a Calendar/Date in Ha ...
  • 看起来像write(DataOutput)方法中的错误: @Override public void write(DataOutput arg0) throws IOException { //write the size first // arg0.write(aggValues.size()); // here you're writing an int as a byte // try this instead: arg0.writeInt(aggValues.size()); // ...
  • 您可以找到更简单的方法来测试您的可写,但手动执行序列化/反序列化也可以。 例如: MyUtils.java: ... import org.apache.commons.io.IOUtils; ... public static byte[] serialize(Writable writable) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); DataOutputS ...