我有一个从超级(父级)类扩展的子(子)类。我想办法提供对Mapper的输入值一般类型,这样我可以提供两个孩子和家长作为有效值是这样的:如何在Hadoop的Mapper和Reducer中提供子类?
公共静态类MyMapper扩展映射< ...,MyParentClass,...,...>
我希望从MyParentClass扩展的MyChildClass也是有效的。
然而,当我运行程序,如果该值是一个子类我得到一个例外:从地图值
类型不匹配:预计MyParentClass,收到MyChildClass
如何启用输入/输出值到映射器的值是否为有效的值?
更新:
package hipi.examples.dumphib;
import hipi.image.FloatImage;
import hipi.image.ImageHeader;
import hipi.imagebundle.mapreduce.ImageBundleInputFormat;
import hipi.util.ByteUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import java.io.IOException;
import java.util.Iterator;
public class DumpHib extends Configured implements Tool {
public static class DumpHibMapper extends Mapper<ImageHeader, FloatImage, IntWritable, Text> {
@Override
public void map(ImageHeader key, FloatImage value, Context context) throws IOException, InterruptedException {
int imageWidth = value.getWidth();
int imageHeight = value.getHeight();
String outputStr = null;
if (key == null) {
outputStr = "Failed to read image header.";
} else if (value == null) {
outputStr = "Failed to decode image data.";
} else {
String camera = key.getEXIFInformation("Model");
String hexHash = ByteUtils.asHex(ByteUtils.FloatArraytoByteArray(value.getData()));
outputStr = imageWidth + "x" + imageHeight + "\t(" + hexHash + ")\t " + camera;
}
context.write(new IntWritable(1), new Text(outputStr));
}
}
public static class DumpHibReducer extends Reducer<IntWritable, Text, IntWritable, Text> {
@Override
public void reduce(IntWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
for (Text value : values) {
context.write(key, value);
}
}
}
public int run(String[] args) throws Exception {
if (args.length < 2) {
System.out.println("Usage: dumphib <input HIB> <output directory>");
System.exit(0);
}
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "dumphib");
job.setJarByClass(DumpHib.class);
job.setMapperClass(DumpHibMapper.class);
job.setReducerClass(DumpHibReducer.class);
job.setInputFormatClass(ImageBundleInputFormat.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(Text.class);
String inputPath = args[0];
String outputPath = args[1];
removeDir(outputPath, conf);
FileInputFormat.setInputPaths(job, new Path(inputPath));
FileOutputFormat.setOutputPath(job, new Path(outputPath));
job.setNumReduceTasks(1);
return job.waitForCompletion(true) ? 0 : 1;
}
private static void removeDir(String path, Configuration conf) throws IOException {
Path output_path = new Path(path);
FileSystem fs = FileSystem.get(conf);
if (fs.exists(output_path)) {
fs.delete(output_path, true);
}
}
public static void main(String[] args) throws Exception {
int res = ToolRunner.run(new DumpHib(), args);
System.exit(res);
}
}
FloatImage是超一流的,我有ChildFloatImage类,从它延伸。当从RecordReader返回ChildFloatImage时,它抛出以前的异常。
如果可以,请发布您的映射代码。 – Amit
@Amit你可以检查上面的代码。你也可以在任何使用简单类型的映射器上进行检查,例如“Text”类和扩展它的一个类,你会看到当子类返回时会抛出一个异常。 –
你可以尝试使用“?extends FloatImage”作为你的泛型类型定义。此外,我认为下面的答案将帮助您了解泛型类型及其用法。这里是另一个泛型和继承理解的资源 - https://docs.oracle.com/javase/tutorial/java/generics/inheritance.html – Amit