hadoop-classcastexception:longwriteable不能转换为long

abithluo  于 2021-05-27  发布在  Hadoop
关注(0)|答案(0)|浏览(293)

我得到了以下信息 ClassCastException 但不确定代码有什么问题:

java.lang.Exception: java.lang.ClassCastException: org.apache.hadoop.io.LongWritable cannot be cast to java.lang.Long
    at org.apache.hadoop.mapred.LocalJobRunner$Job.runTasks(LocalJobRunner.java:492)
    at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:552)
Caused by: java.lang.ClassCastException: org.apache.hadoop.io.LongWritable cannot be cast to java.lang.Long
    at uk.ac.man.cs.comp38211.exercise.PositionalTFTerm.<init>(BasicInvertedIndex.java:264)
    at uk.ac.man.cs.comp38211.exercise.PositionalTFTerm.of(BasicInvertedIndex.java:301)
    at uk.ac.man.cs.comp38211.exercise.PositionalTFTerm.merge(BasicInvertedIndex.java:276)
    at java.util.stream.Collectors$1OptionalBox.accept(Collectors.java:706)
    at java.util.stream.Collectors.lambda$groupingBy$45(Collectors.java:909)
    at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
    at com.google.common.collect.Streams$2.tryAdvance(Streams.java:366)
    at java.util.Spliterator.forEachRemaining(Spliterator.java:326)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
    at uk.ac.man.cs.comp38211.exercise.BasicInvertedIndex$MyMapper.map(BasicInvertedIndex.java:103)

这是抛出异常的一行,在构造函数中,我试图转换 List<Long> positionsArrayListWritable<LongWritable> positions :

以下是实施 ArrayListWritable :

/*
 * Cloud9: A MapReduce Library for Hadoop Licensed 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 uk.ac.man.cs.comp38211.io.array;

import org.apache.hadoop.io.Writable;

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

/**
 * <p>
 * Writable extension of a Java ArrayList. Elements in the list must be
 * homogeneous and must implement Hadoop's Writable interface.
 * </p>
 * 
 * @param <E>
 *            type of list element
 * 
 * @author Jimmy Lin
 * @author Tamer Elsayed
 */

public class ArrayListWritable<E extends Writable> extends ArrayList<E> implements Writable
{
    private static final long serialVersionUID = 4911321393319821791L;

    /**
     * Creates an ArrayListWritable object.
     */
    public ArrayListWritable()
    {
        super();
    }

    /**
     * Creates an ArrayListWritable object from an ArrayList.
     */
    public ArrayListWritable(ArrayList<E> array)
    {
        super(array);
    }

    /**
     * Deserializes the array.
     * 
     * @param in
     *            source for raw byte representation
     */
    @SuppressWarnings("unchecked")
    public void readFields(DataInput in) throws IOException
    {
        this.clear();

        int numFields = in.readInt();
        if (numFields == 0) return;
        String className = in.readUTF();
        E obj;
        try
        {
            Class<E> c = (Class<E>) Class.forName(className);
            for (int i = 0; i < numFields; i++)
            {
                obj = (E) c.newInstance();
                obj.readFields(in);
                this.add(obj);
            }

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * Serializes this array.
     * 
     * @param out
     *            where to write the raw byte representation
     */
    public void write(DataOutput out) throws IOException
    {
        out.writeInt(this.size());
        if (size() == 0) return;
        E obj = get(0);

        out.writeUTF(obj.getClass().getCanonicalName());

        for (int i = 0; i < size(); i++)
        {
            obj = get(i);
            if (obj == null)
            {
                throw new IOException("Cannot serialize null fields!");
            }
            obj.write(out);
        }
    }

    /**
     * Generates human-readable String representation of this ArrayList.
     * 
     * @return human-readable String representation of this ArrayList
     */
    public String toString()
    {
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        for (int i = 0; i < this.size(); i++)
        {
            if (i != 0) sb.append(", ");
            sb.append(this.get(i));
        }
        sb.append("]");

        return sb.toString();
    }
}

我能知道怎么修吗?谢谢。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题