import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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 java.io.IOException;
public class DemoWC {
public static class WCMapper extends Mapper<LongWritable,Text,Text,LongWritable>{
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
context.write(new Text(line),new LongWritable(1));
}
}
public static class WCReduce extends Reducer<Text,LongWritable,Text,LongWritable>{
@Override
protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
long sum =0l;
for (LongWritable value : values) {
sum = sum+value.get();
}
context.write(key,new LongWritable(sum));
}
}
public static void main(String[] args) throws Exception {
Job job = Job.getInstance();
job.setJobName("第一个mr");
job.setJarByClass(DemoWC.class);
//设置map类
job.setMapperClass(WCMapper.class);
//设置map输出类
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class);
//设置reduce类
job.setReducerClass(WCReduce.class);
//设置reduce输出类
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
Path in = new Path("/word");
FileInputFormat.addInputPath(job,in);
Path out = new Path("/output");
FileSystem fs = FileSystem.get(new Configuration());
if (fs.exists(out)){
fs.delete(out,true);
}
FileOutputFormat.setOutputPath(job,out);
//启动任务
job.waitForCompletion(true);
}
}
MapReduce之wordcount代码
最新推荐文章于 2022-07-12 14:53:59 发布