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

标签 java mongodb

我正在尝试设置 mongo-hadoop我为 python 插件编写的 hadoop 流作业的驱动程序扩展 dumbo .

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, 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;

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

关于java.lang.ClassCastException 即使我将它转换为扩展类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6560565/

相关文章:

java - 如何删除 Java ArrayList 中具有偶数索引的所有元素

java - BufferedWriter 写 sumbols 而不是数字是有原因的吗?

java - 如何在 Mule 3.4 ESB 中使用 Datamapper 映射数据库中的 Excel 文件

python - 如何将异步插入到 mongodb 中的日志中?

javascript - 带有 $or 条件的 Mongoose findOne 方法会引发错误

java - android 计划的作业是否在应用程序升级后仍然存在?

java - 使用Java在Sql中存储 '\n'

mongodb - 如何在mgo中进行文本搜索?

linux - 如何将linux命令日志捕获到文件中?

node.js - 即使使用 `credentials: ' 也无法使用 cookie 获取包含'`