我试图创建一个由两个步骤组成的mapreduce链。 第一个reduce将键值对发出为(键,值),其中value是自定义对象的列表,第二个映射器应读取第一个reducer的输出。 该列表是一个自定义ArrayWritable。以下是相关代码:
自定义对象:
public class Custom implements Writable {
private Text document;
private IntWritable count;
public Custom(){
setDocument("");
setCount(0);
}
public Custom(String document, int count) {
setDocument(document);
setCount(count);
}
@Override
public void readFields(DataInput in) throws IOException {
// TODO Auto-generated method stub
document.readFields(in);
count.readFields(in);
}
@Override
public void write(DataOutput out) throws IOException {
document.write(out);
count.write(out);
}
@Override
public String toString() {
return this.document.toString() + "\t" + this.count.toString();
}
public int getCount() {
return count.get();
}
public void setCount(int count) {
this.count = new IntWritable(count);
}
public String getDocument() {
return document.toString();
}
public void setDocument(String document) {
this.document = new Text(document);
}
}
自定义ArrayWritable:
class MyArrayWritable extends ArrayWritable {
public MyArrayWritable(Writable[] values) {
super(Custom.class, values);
}
public MyArrayWritable() {
super(Custom.class);
}
@Override
public Custom[] get() {
return (Custom[]) super.get();
}
@Override
public String toString() {
return Arrays.toString(get());
}
@Override
public void write(DataOutput arg0) throws IOException {
super.write(arg0);
}
}
第一个减速器:
public static class NGramReducer extends Reducer<Text, Text, Text, MyArrayWritable> {
public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
//other code
context.write(key, mArrayWritable);
}
}
第二张映射器:
public static class SecondMapper extends Mapper<Text, MyArrayWritable, Text, IntWritable> {
private StringBuilder docBuilder= new StringBuilder();
public void map(Text key, MyArrayWritable value, Context context) throws IOException, InterruptedException {
//whatever code
}
}
这些是主要的设置:
//...
job1.setOutputKeyClass(Text.class);
job1.setOutputValueClass(MyArrayWritable.class);
job1.setInputFormatClass(WholeFileInputFormat.class);
FileInputFormat.addInputPath(job1, new Path(args[2]));
FileOutputFormat.setOutputPath(job1, TEMP_PATH);
//...
job2.setInputFormatClass(KeyValueTextInputFormat.class);
FileInputFormat.addInputPath(job2, TEMP_PATH);
FileOutputFormat.setOutputPath(job2, new Path(args[3]));
当我运行它时,我收到此错误 错误:java.lang.ClassCastException:org.apache.hadoop.io.Text无法强制转换为Detector $ MyArrayWritable
有什么问题?我是否必须编写FileInputFormat? (job1工作正常)
答案 0 :(得分:0)
看起来这是因为你的工作2 InputFormat
。 KeyValueTextInputFormat.class
期望一个键和值都是Text
个对象。当您的工作1输出(Text,MyArrayWritable)
时,与该值存在冲突。
幸运的是,您无需编写自定义OutputFormat
来满足您的数据需求!只需将作业1数据的输出写入序列文件,即以二进制形式保存数据:
//...
job1.setOutputKeyClass(Text.class);
job1.setOutputValueClass(MyArrayWritable.class);
job1.setInputFormatClass(WholeFileInputFormat.class);
job1.setOutputFormatClass(SequenceFileOutputFormat.class);
FileInputFormat.addInputPath(job1, new Path(args[2]));
SequenceFileOutputFormat.setOutputPath(job1, TEMP_PATH);
//...
job2.setInputFormatClass(SequenceFileInputFormat.class);
SequenceFileInputFormat.addInputPath(job2, TEMP_PATH);
FileOutputFormat.setOutputPath(job2, new Path(args[3]));