gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:15:59 24 4
gpt4 key购买 nike

我正在尝试设置 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com