我想在Hadoop中创建一个新的数据类型但是我的自定义inputformat类出现以下错误这是我的代码:
错误 - 无法将WholeFileRecordReader强制转换为org.apache.hadoop.mapred.RecordReader
代码 -
import java.io.IOException;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileSplit;
import org.apache.hadoop.mapred.InputSplit;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.RecordReader;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.TaskAttemptContext;
public class wholeFileInputFormat extends FileInputFormat<Text, apriori>{
public RecordReader<Text, apriori> getRecordReader(
InputSplit input, JobConf job, Reporter reporter)
throws IOException {
reporter.setStatus(input.toString());
return (RecordReader<Text, apriori>) new WholeFileRecordReader(job,FileSplit)input);
}
}
我的自定义记录阅读器如下
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileSplit;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
class WholeFileRecordReader extends RecordReader<Text, apriori> {
private FileSplit fileSplit;
private Configuration conf;
private InputStream in;
private Text key = new Text("");
private apriori value = new apriori();
private boolean processed = false;
public void initialize( JobConf job, FileSplit split)
throws IOException {
this.fileSplit = split;
this.conf = job;
final Path file = fileSplit.getPath();
String StringPath = new String(fileSplit.getPath().toString());
String StringPath2 = new String();
StringPath2 = StringPath.substring(5);
System.out.println(StringPath2);
in = new FileInputStream(StringPath2);
FileSystem fs = file.getFileSystem(conf);
in = fs.open(file);
}
public boolean nextKeyValue() throws IOException, InterruptedException {
if (!processed) {
byte[] contents = new byte[(int) fileSplit.getLength()];
Path file = fileSplit.getPath();
key.set(file.getName());
try {
IOUtils.readFully(in, contents, 0, contents.length);
value.set(contents, 0, contents.length);
} finally {
IOUtils.closeStream(in);
}
processed = true;
return true;
}
return false;
}
@Override
public Text getCurrentKey() throws IOException, InterruptedException {
return key;
}
@Override
public apriori getCurrentValue() throws IOException, InterruptedException {
return value;
}
@Override
public float getProgress() throws IOException {
return processed ? 1.0f : 0.0f;
}
@Override
public void close() throws IOException {
// Do nothing
}
@Override
public void initialize(InputSplit arg0, TaskAttemptContext arg1)
throws IOException, InterruptedException {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
WholeFileRecordReader
类是org.apache.hadoop.mapreduce.RecordReader
类的子类。此类无法强制转换为org.apache.hadoop.mapred.RecordReader
类。可以尝试在两个类中使用相同的API
根据Java编程语言的规则,只能将相同类型层次结构中的类或接口(统称为Type)转换或相互转换。如果您尝试转换两个不共享相同类型层次结构的对象,即它们之间没有父子关系,则会出现编译时错误。您可以参考此link
答案 1 :(得分:0)
由于此错误即将发生包裹不匹配。
在您的代码中,您合并了MRv1和MRv2,因为您收到错误。
包org.apache.hadoop.mapred
是Mrv1。 (Map Reduce版本1)
包org.apache.hadoop.mapreduce
是Mrv2。 (Map Reduce version 2)
在您的代码中,您结合了MRv1和MRv2:
import org.apache.hadoop.mapred.FileSplit;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
将所有导入包用作org.apache.hadoop.mapred
(MRv1)或org.apache.hadoop.mapreduce
(MRv2)。
希望这有帮助。