You are viewing a plain text version of this content. The canonical link for it is here.
Posted to mapreduce-dev@hadoop.apache.org by ch huang <ju...@gmail.com> on 2014/11/25 10:08:40 UTC

issue when use MultipleOutputs

hi,maillist:

             i am a newbie for MR job coding, i want to ask a question
about using MultipleOutputs, i plan to change my workcount code and output
the odd record to a file and even record to another file

if result is
tom 2
alex 1

i want write "tom 2" into file which name is even  ,and write "alex 1" to
file which name is odd,i hope it's clear for understand

and here is my code

*map code:*

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class MyMapper extends Mapper<Object, Text, Text, IntWritable> {

private final static IntWritable one = new IntWritable(1);
private Text word = new Text();

public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {

StringTokenizer tokenizer = new StringTokenizer(value.toString());
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
context.write(word, one);
}
}
}

*reduce code:*

import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;

public class MyReducer extends Reducer<Text, IntWritable, Text,
IntWritable> {
 private MultipleOutputs<Text, IntWritable> _output;
@Override
public void setup(Context context)throws IOException, InterruptedException{
_output = new MultipleOutputs<Text, IntWritable>(context);

}
@Override
public void cleanup(Context context)throws IOException,
InterruptedException{
_output.close();

}
 public void reduce(Text key, Iterator<IntWritable> values, Context context)
throws IOException, InterruptedException {
int sum = 0;
while (values.hasNext()) {
sum += values.next().get();
}
if(sum%2 == 0){
_output.write("even", key, new IntWritable(sum),"testeven");
}else{
_output.write("odd", key, new IntWritable(sum),"testodd");
}
 // context.write(key, new IntWritable(sum));
}

}

and my driver code


*import org.apache.hadoop.conf.Configuration;*
*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.lib.input.FileInputFormat;*
*import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;*
*import org.apache.hadoop.mapreduce.lib.output.LazyOutputFormat;*
*import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;*
*import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;*
*import org.apache.hadoop.mapreduce.lib.reduce.IntSumReducer;*
*import org.apache.hadoop.util.GenericOptionsParser;*


public class MyFirstMRjob {

public static void main(String[] args) throws Exception
 {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args)
.getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println("Usage: wordcount <in> <out>");
System.exit(2);
}
Job job = Job.getInstance(conf,"my demo MR job");
 job.setJarByClass(MyFirstMRjob.class);
 job.setMapperClass(MyMapper.class);
job.setReducerClass(MyReducer.class);
 job.setNumReduceTasks(1);
conf.set("mapreduce.output.textoutputformat.separator", "U");
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
 MultipleOutputs.addNamedOutput(job, "even",TextOutputFormat.class,
Text.class,
            IntWritable.class);
MultipleOutputs.addNamedOutput(job, "odd",TextOutputFormat.class,
Text.class,
            IntWritable.class);
LazyOutputFormat.setOutputFormatClass(job, TextOutputFormat.class);
System.exit(job.waitForCompletion(true) ? 0 : 1);
}

}

my datafile is

alex
tom
alex
tom
tim

but my output still like
# hadoop fs -ls alex_out
Found 2 items
-rw-r--r--   3 root supergroup          0 2014-11-25 16:50 alex_out/_SUCCESS
-rw-r--r--   3 root supergroup         19 2014-11-25 16:50
alex_out/part-r-00000

# hadoop fs -cat alex_out/part-r-00000
alex    2
tim     1
tom     2

why ??  thanks a lot