---恢复内容开始---

1.首先准备一个需要统计的单词文件 word.txt,我们的单词是以空格分开的,统计时按照空格分隔即可

hello hadoop
hello yarn
hello zookeeper
hdfs hadoop
select from hadoop
select from yarn
mapReduce
MapReduce

2.上传word.txt到hdfs根目录

$ bin/hdfs dfs -put test/word.txt /

3.准备工作完成后在eclipse编写代码,分别编写Map、Reduce、Driver等Java文件

WordCountMap.java

map执行我们的word.txt 文件是按行执行,每一行执行一个map

WordCountMap.java

map执行我们的word.txt 文件是按行执行,每一行执行一个map

package com.ijeffrey.mapreduce.wordcount.client;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
/**
* map 输出的键值对必须和reducer输入的键值对类型一致
* @author PXY
*
*/
public class WordCountMap extends Mapper<LongWritable, Text, Text, IntWritable> {

private Text keyout = new Text();
private IntWritable valueout = new IntWritable(1);

@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
throws IOException, InterruptedException {

String line = value.toString();
// 我的文件记录的单词是以空格记录单词,所以这里用空格来截取
String[] words = line.split(" ");

// 遍历数组,并以k v 对的形式输出
for (String word : words) {
keyout.set(word);
context.write(keyout, valueout);
}
}

}

WordCountReducer.java

package com.ijeffrey.mapreduce.wordcount.client;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

/**
* reducer 输入的键值对必须和map输出的键值对类型一致
* map <hello,1> <world,1> <hello,1> <apple,1> ....
* reduce 接收 <apple,[1]> <hello,[1,1]> <world,[1]>
* @author PXY
*
*/
public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable valueout = new IntWritable();

@Override
protected void reduce(Text key, Iterable<IntWritable> values,
Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
int count = 0; // 统计总数

// 遍历数组,累加求和
for(IntWritable value : values){

// IntWritable类型不能和int类型相加,所以需要先使用get方法转换成int类型
count += value.get();
}

// 将统计的结果转成IntWritable
valueout.set(count);

// 最后reduce要输出最终的 k v 对
context.write(key, valueout);

}
}

WordCountDriver.java

package com.ijeffrey.mapreduce.wordcount.client;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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;

/**
* 运行主函数
* @author PXY
*
*/
public class WordCountDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();

// 获得一个job对象,用来完成一个mapreduce作业
Job job = Job.getInstance(conf);

// 让程序找到主入口
job.setJarByClass(WordCountDriver.class);

// 指定输入数据的目录,指定数据计算完成后输出的目录
// sbin/yarn jar share/hadoop/xxxxxxx.jar wordcount /wordcount/input/ /wordcount/output/
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));

// 告诉我调用那个map方法和reduce方法
job.setMapperClass(WordCountMap.class);
job.setReducerClass(WordCountReducer.class);

// 指定map输出键值对的类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);

// 指定reduce输出键值对的类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);

// 提交job任务
boolean result = job.waitForCompletion(true);
System.exit(result ? 0 : 1);

}
}

}

4.将编写完成的代码打成jar包,并在集群上运行

将jar上传到到服务器,启动服务后运行我们自己编写的MapReduce,统计根目录下的word.txt并将运行结果写入output

$ bin/yarn jar test/wordCount.jar com.ijeffrey.mapreduce.wordcount.client.WordCountDriver /word.txt /output

注意:运行jar的时候要添加Driver的完全路径

运行完成后查看output结果:

$ bin/hdfs dfs -text /output12/part-r-00000

转载于:https://www.cnblogs.com/XSG-960923/p/10928306.html

在eclipse使用map reduce编写word count程序生成jar包并在虚拟机运行的步骤相关推荐

  1. 当Eclipse中maven识别不了本仓jar包是的解决方法

    当Eclipse中maven识别不了本仓jar包是的解决方法: 在本地仓找到对应的jar包,复制到java安装目录 打开Eclipse.ini这个文件,把该带代码加到后面 -javaagent:G:\ ...

  2. eclipse java 编译jar_Eclipse对Java项目打Jar包

    一定要注意第五步,先编译,不然找不到入口main文件 在本Java项目中,如下图一所示,Java项目含有外部依赖Jar包 -- fastjson-1.2.29.jar  包. 在经历了多次的失败后,最 ...

  3. eclipse java混淆打包_eclipse proguard怎么混淆jar包

    展开全部 android应用程序的混淆打包32313133353236313431303231363533e59b9ee7ad9431333339663933 1 . 在工程文件project.pro ...

  4. idea下将scala编写的项目打成jar包

    如果文章里的内容有误,为避免误人子弟,请一定评论或直接私信我. 我已经很认真的将文章写的尽可能详细,希望这篇文章能给予您微不足道的帮助. 摘要 spark,中文译作星星之火,时至今日,这把由scala ...

  5. maven的eclipse找不到本地仓库的的jar包

    解决方法: 我的工程不是maven工程,把他改成maven工程就好了 命令:mvn eclipse:eclipse

  6. AS编写sdk并打成jar包供其它APP调用

    如果sdk中涉及res下面的资源注意package路径. 1. Android Studio后,结合gradle来配置生成jar 包. gradle在构建的时候,会对各个module的class文件打 ...

  7. Hadoop Map/Reduce教程

    Hadoop Map/Reduce教程 目的     先决条件     概述     输入与输出     例子:WordCount v1.0         源代码         用法        ...

  8. Hadoop MapReduce测试word count功能

    在hadoop的mapreduce目录下自带有一个hadoop-mapreduce-examples-2.7.5.jar(官方已经为我们写好了用java实现word count的jar)可以用来测试w ...

  9. android支持第三方jar包,以及Eclipse如何导入jar包

    2019独角兽企业重金招聘Python工程师标准>>> 通常我们进行android开发的时候运用到了两种格式的jar包文件. 1.在eclipse环境下,引入第三方jar包,指的是通 ...

最新文章

  1. 对时间序列分类的LSTM全卷积网络的见解
  2. input python2.7_python 中的input
  3. 记一次Java动态代理实践
  4. JS面向对象--你真的理解闭包了吗?
  5. 联想电脑如何下载matlab,lenovo utility是什么软件?
  6. C++实现折半插入排序
  7. cf 1511 D. Min Cost String
  8. RT5350原厂SDK及AP移植步骤详解
  9. linux终端贪吃蛇,分享|nSnake: 在Linux的终端上玩经典的贪食蛇游戏
  10. 研究机构:全球半导体厂商今年资本支出1081亿美元
  11. Java是先难后易吗_学软件应该“先难后易”还是“先易后难”?
  12. 常用的C#方法【Format.CS】
  13. 在SQL Server 2000中使用Transact-SQL建立数据库
  14. Linux操作系统文件链接问题
  15. echarts 关系图 参数_Echarts关系图(使用重力图)
  16. mac jenkins下载与安装
  17. 45、backtrader的一些基本概念---佣金(commission)的设置
  18. 缠中说禅:缠非缠、禅非禅,枯木龙吟照大千(整理版)
  19. k8s部署mysql
  20. 从基本组件到结构创新,67页论文解读深度卷积神经网络架构

热门文章

  1. Intellij Idea 导入多个maven项目展示在左侧栏Maven Projects
  2. HTML5上传图片,后台使用java
  3. Pokémon Go数据收集是否带来隐私问题
  4. PHP无法加载curl扩展
  5. aspx文件、aspx.cs文件、aspx.designer.cs文件之讲解
  6. 不走寻常路 IBM云计算解决方案解读
  7. java 算法笔试题_【干货】经典算法面试题代码实现-Java版
  8. python类继承实例
  9. 02-vue过滤器和键盘修饰符
  10. 十问 | 关于Service Mesh 和Kubernets的最前沿思考