作者:谢绝沟通 | 来源:互联网 | 2023-08-12 12:36
1.首先建一个WCMapper类:packagecom.zhichao.wan.mr.wordcount1;importjava.io.IOException;import
1.首先建一个WCMapper 类:
package com.zhichao.wan.mr.wordcount1;
import java.io.IOException;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class WCMapper extends Mapper<LongWritable, Text, Text, LongWritable>{
@Override
protected void map(LongWritable key, Text value,Context context)
throws IOException, InterruptedException {
String sting = value.toString();
String[] words = StringUtils.split(sting, " ");
for (String word : words) {
context.write(new Text(word), new LongWritable(1));
}
}
}
2.然后建一个WCReducer 类:
package com.zhichao.wan.mr.wordcount1;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class WCReducer extends Reducer<Text, LongWritable, Text, LongWritable>{
@Override
protected void reduce(Text key, Iterable values,Context context)
throws IOException, InterruptedException {
long count=0;
for (LongWritable value : values) {
count+=value.get();
}
context.write(key, new LongWritable(count));
}
}
3.最后
package com.zhichao.wan.mr.wordcount1
import java.io.IOException
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.lib.input.FileInputFormat
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat
public class WCRunner {
public static void main(String[] args) throws Exception {
Job job=Job.getInstance()
job.setJarByClass(WCRunner.class)
job.setMapperClass(WCMapper.class)
job.setReducerClass(WCReducer.class)
job.setMapOutputKeyClass(Text.class)
job.setMapOutputValueClass(LongWritable.class)
job.setOutputKeyClass(Text.class)
job.setOutputValueClass(LongWritable.class)
FileInputFormat.setInputPaths(job, new Path("/wc/srcdata/"))
FileOutputFormat.setOutputPath(job, new Path("/wc/output"))
job.waitForCompletion(true)
}
}
5.将程序打成jar包,并拷贝到linux中。
6.建一个测试文件
7.运行jar包,指定主程序的地址
8.运行结果: