加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java.lang.ClassCastException,即使我将它强制转换为扩展类

发布时间:2020-12-15 02:26:37 所属栏目:Java 来源:网络整理
导读:我正在尝试为我为 python插件 dumbo编写的hadoop流工作设置一个 mongo-hadoop驱动程序扩展. dumbo项目需要我使用TypedBytesWritable类.所以我做了一个新的InputFormat RecordReader像这样: package com.mongodb.hadoop;public class TypedBytesTableInputFo
我正在尝试为我为 python插件 dumbo编写的hadoop流工作设置一个 mongo-hadoop驱动程序扩展.

dumbo项目需要我使用TypedBytesWritable类.所以我做了一个新的InputFormat& RecordReader像这样:

package com.mongodb.hadoop;

public class TypedBytesTableInputFormat implements InputFormat<TypedBytesWritable,TypedBytesWritable> {

@Override
public RecordReader<TypedBytesWritable,TypedBytesWritable> getRecordReader(InputSplit split,JobConf job,Reporter reporter) {

    if (!(split instanceof MongoInputSplit))
        throw new IllegalStateException("Creation of a new RecordReader requires a MongoInputSplit instance.");

    final MongoInputSplit mis = (MongoInputSplit) split;

    //**THE FOLLOWING LINE THROWS THE ERROR**
    return (RecordReader<TypedBytesWritable,TypedBytesWritable>) new TypedBytesMongoRecordReader(mis);
}

这是扩展的RecordReader:

package com.mongodb.hadoop.input;
...
...
import org.apache.hadoop.mapreduce.RecordReader;
...
...

public class TypedBytesMongoRecordReader extends RecordReader<TypedBytesWritable,TypedBytesWritable> {

public TypedBytesMongoRecordReader(MongoInputSplit mis) {
    _cursor = mis.getCursor();
}

@Override
public void close() {
    if ( _cursor != null )
        _cursor.close();
}

但是当我运行这个工作时,它会抛出这个错误.我不知道为什么,它是RecordReader的孩子.我究竟做错了什么?这是RecordReader类的API文档.我以为我正在做的一切正确:

http://hadoop.apache.org/common/docs/current/api/org/apache/hadoop/mapreduce/RecordReader.html

我确实在正在转换为RecordReader的行上发出警告,但没有错误,并且它构建了jar就好了.警告:

Type safety: Unchecked cast from TypedBytesMongoRecordReader to RecordReader<TypedBytesWritable,TypedBytesWritable>

解决方法

试试这个:

public <T extends RecordReader<TypedBytesWritable,TypedBytesWritable>> T getRecordReader(InputSplit split,Reporter reporter) {

    if (!(split instanceof MongoInputSplit))
        throw new IllegalStateException("Creation of a new RecordReader requires a MongoInputSplit instance.");

    final MongoInputSplit mis = (MongoInputSplit) split;

    return new TypedBytesMongoRecordReader(mis); // you may need a cast (T) - try it without first
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读