20,844
社区成员
发帖
与我相关
我的任务
分享
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class wzl189_distinct {
public static class MyMapper extends
Mapper<Object, Text, Text, NullWritable> {
Text outKey = new Text();
@Override
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
String tmp[] = value.toString().split(" ");
if (tmp.length != 2)
return;
outKey.set(tmp[0]);
context.write(outKey, NullWritable.get());
}
}
public static class MyReducer extends
Reducer<Text, NullWritable, LongWritable, NullWritable> {
long myCount = 0l;
@Override
public void reduce(Text key, Iterable<NullWritable> values,
Context context) throws IOException, InterruptedException {
++myCount;
}
@Override
public void cleanup(Context context) throws IOException,
InterruptedException {
context.write(new LongWritable(myCount), NullWritable.get());
};
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
if (args.length != 2) {
System.err.println("Usage: <in> <out>");
System.exit(2);
}
conf.set("mapred.child.java.opts", "-Xmx350m -Xmx1024m");
@SuppressWarnings("deprecation")
Job job = new Job(conf, "wzl189_distinct");
job.setNumReduceTasks(1);
job.setInputFormatClass(TextInputFormat.class);
job.setJarByClass(wzl189_distinct.class);
job.setMapperClass(MyMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(NullWritable.class);
job.setReducerClass(MyReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
reduce阶段只用一个计数器就行了