知识点
相关文章
更多最近更新
更多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,分别用来序列化和反序列化。
- /**
- * 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;
- }
相关问答
更多-
做运维 ,又不需要太好的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(Profiling Hadoop)[2022-02-05]
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可写日期/日历(Hadoop Writable for Date/Calendar)[2023-01-07]
在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()); // ...
-
首先, Reducer您可以找到更简单的方法来测试您的可写,但手动执行序列化/反序列化也可以。 例如: MyUtils.java: ... import org.apache.commons.io.IOUtils; ... public static byte[] serialize(Writable writable) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); DataOutputS ...