MapReduce运行wordcount程序
1、Eclipase里面在Maven项目下编写两个java程序代码:
①主程序代码:
public class WordCount {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
Job job = new Job(conf,"word count");
job.setJarByClass(WordCount.class);
//指定Mapper类
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
//指定Reducer类
job.setReducerClass(IntSumReducer.class);
//设置Reduce函数输出key的类型
job.setOutputKeyClass(Text.class);
//设置Reduce函数输出value的类型
job.setOutputValueClass(IntWritable.class);
//指定输入路径
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
//指定输出路径
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.out.println("OK");
//提交任务
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
②主程序引用到的代码:
public class TokenizerMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while(itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
}
}
}
2、把主程序打成jar包放到虚拟机上运行
此处附上Maven打成jar包的方法连接:
https://blog.youkuaiyun.com/Wxp_csdn/article/details/90444949
3、将jar包上传到虚拟机根目录下后执行命令:
java -jar jar包名
执行完成即可查看运行结果。