Java Map减少编程错误
我的输入是许多文本文件。我希望我的map reduce程序将所有文件名和与文件名相关联的句子写入一个输出文件中,其中我只希望从映射器发出文件名(键)和相关句子(值)。reducer将收集键和所有值,并在输出中写入文件名及其相关语句 以下是我的mapper和reducer的代码:Java Map减少编程错误,java,hadoop,Java,Hadoop,我的输入是许多文本文件。我希望我的map reduce程序将所有文件名和与文件名相关联的句子写入一个输出文件中,其中我只希望从映射器发出文件名(键)和相关句子(值)。reducer将收集键和所有值,并在输出中写入文件名及其相关语句 以下是我的mapper和reducer的代码: public class WordCount { public static class Map extends MapReduceBase implements Mapper<LongWritable,
public class WordCount {
public static class Map extends MapReduceBase implements Mapper<LongWritable,
Text, Text, Text> {
public void map(LongWritable key, Text value, OutputCollector<Text,Text>
output, Reporter reporter) throws IOException {
String filename = new String();
FileSplit filesplit = (FileSplit)reporter.getInputSplit();
filename=filesplit.getPath().getName();
output.collect(new Text(filename), value);
}
}
public static class Reduce extends MapReduceBase implements Reducer<Text, Text,
Text, Text> {
public void reduce(Text key, Iterable<Text> values, OutputCollector<Text,
Text> output, Reporter reporter) throws IOException {
StringBuilder builder = new StringBuilder();
for(Text value : values) {
String str = value.toString();
builder.append(str);
}
String valueToWrite=builder.toString();
output.collect(key, new Text(valueToWrite));
}
@Override
public void reduce(Text arg0, Iterator<Text> arg1,
OutputCollector<Text, Text> arg2, Reporter arg3)
throws IOException {
}
}
public static void main(String[] args) throws Exception {
JobConf conf = new JobConf(WordCount.class);
conf.setJobName("wordcount");
conf.setMapperClass(Map.class);
conf.setReducerClass(Reduce.class);
conf.setJarByClass(WordCount.class);
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(Text.class);
conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);
conf.setNumReduceTasks(1);
FileInputFormat.setInputPaths(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));
JobClient.runJob(conf);
}
}
当我使用相同配置的inputformat(keyvaluetextinputformat.class
)运行上述映射器和reducer时,它不会在输出中写入任何内容
我应该改变什么来实现我的目标?KeyValueTextInputFormat不是适合您案例的正确输入格式。如果要使用此输入格式,输入中的每一行都应包含一个键、值对,默认情况下由用户指定的分隔符或制表符分隔。但在您的情况下,输入是“文件集”,并且您希望作业的输出是“文件名,文件内容” 实现这一点的方法之一是使用TextInputFormat作为输入格式。我已经在下面测试过了,它可以正常工作 在映射函数中获取文件名和文件内容
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException
{
String filename = new String();
FileSplit filesplit = (FileSplit)context.getInputSplit();
filename=filesplit.getPath().getName();
context.write(new Text(filename), new Text(value));
}
在reduce函数中,我们构建将成为文件内容的所有值的字符串
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException
{
StringBuilder builder= new StringBuilder();
for (Text value : values)
{
String str = value.toString();
builder.append(str);
}
String valueToWrite= builder.toString();
context.write(key, new Text(valueToWrite));
}
}
当前输出是什么(输出文件的内容)?我猜没有,如作业计数器所示。Map output bytes=0,Map函数未作为输出发出,因此还原程序将无法处理任何内容。因此,先生,我应该如何修改其ISSR,isSplitable()在何处使用?先生,您是否可以编写一个实例来使用它。。其中??你可以忽略我关于issplitable()的观点,因为它也不会有帮助,只需将上面的reducer和mapper与textinputformat一起使用,它就会工作。我已经编辑了答案。先生,我仍然得到相同的结果,,我已经修改了我的代码和输出。我已经修改了我上面发布的代码和它的输出。。按照您给定的代码。。
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException
{
StringBuilder builder= new StringBuilder();
for (Text value : values)
{
String str = value.toString();
builder.append(str);
}
String valueToWrite= builder.toString();
context.write(key, new Text(valueToWrite));
}
}
job.setInputFormatClass(TextInputFormat.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setMapperClass(myMapper.class);
job.setReducerClass(myReducer.class);
job.setNumReduceTasks(1);