MapReduce运行wordcount程序

3 篇文章 0 订阅
1 篇文章 0 订阅
本文介绍了如何在Eclipse的Maven项目下编写MapReduce的WordCount程序,详细讲解了主程序的代码结构,并提供了Maven打包成jar的教程链接。随后,文章说明了如何将jar包上传到虚拟机,并通过命令行执行,最终查看运行结果。
摘要由CSDN通过智能技术生成

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.csdn.net/Wxp_csdn/article/details/90444949

3、将jar包上传到虚拟机根目录下后执行命令:
java -jar jar包名
执行完成即可查看运行结果。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值