原始数据:

Map阶段

1.每次读一行数据,

2.拆分每行数据,

3.每个单词碰到一次写个1

<0, "hello tom">

<10, "hello jerry">

<22, "hello kitty">

<34, "hello world">

<46, "hello tom">

点击查看代码
/**
* @ClassName:WordCountReduce
* @Description:TODO
* @author:Li Wei Ning
* @Date:2022/4/28 10:55
*/ import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException; /**
* Text 数据类型:字符串类型 String
* IntWritable reduce阶段的输入类型 int
* Text reduce阶段的输出数据类型 String类型
* IntWritable 输出词频个数 Int型
* @author 暖阳
*/
public class WordCountReduce extends Reducer<Text, IntWritable,Text,IntWritable> {
/**
* key 输入的 键
* value 输入的 值
* context 上下文对象,用于输出键值对
*/
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context){
try {
int sum=0;
for(IntWritable val:values){
sum+=val.get();
}
context.write(key,new IntWritable(sum));
}catch (Exception e){
e.printStackTrace();
}finally {
System.out.println("成功!!!");
}
}
}

reduce阶段

1.把单词对应的那些1

2遍历

3求和

<hello, {1,1,1,1,1}>

<jerry, {1}>

<kitty, {1}>

<tom, {1,1}>

<world, {1}>

点击查看代码
/**
* @ClassName:WordCountReduce
* @Description:TODO
* @author:Li Wei Ning
* @Date:2022/4/28 10:55
*/ import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException; /**
* Text 数据类型:字符串类型 String
* IntWritable reduce阶段的输入类型 int
* Text reduce阶段的输出数据类型 String类型
* IntWritable 输出词频个数 Int型
* @author 暖阳
*/
public class WordCountReduce extends Reducer<Text, IntWritable,Text,IntWritable> {
/**
* key 输入的 键
* value 输入的 值
* context 上下文对象,用于输出键值对
*/
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context){
try {
int sum=0;
for(IntWritable val:values){
sum+=val.get();
}
context.write(key,new IntWritable(sum));
}catch (Exception e){
e.printStackTrace();
}finally {
System.out.println("成功!!!");
}
}
}

整合合并

点击查看代码
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; /**
* @ClassName:WordCount
* @Description:TODO
* @author:Li Wei Ning
* @Date:2022/4/28 10:54
*/
public class WordCountTest {
public static void main(String[] args) { try {
/*定义配置*/
Configuration config = new Configuration();
/* config.set("fs.defaultFS", "hdfs://192.168.47.128:9000");*/ /*定义一个工作任务,用于套接map和reduce两个阶段*/
Job job = Job.getInstance(config); /* 定义工作任务用map*/
job.setMapperClass(WordCountMap.class);
/*定义map的输出key*/
job.setMapOutputKeyClass(Text.class);
/*定义map的输出value*/
job.setMapOutputValueClass(IntWritable.class); /*定义map的文件路径*/
Path srcPath = new Path("C:/Users/暖阳/Desktop/123.txt");
/*定义map的输入文件*/
FileInputFormat.setInputPaths(job,srcPath); /* 定义reduce用哪个类*/
job.setReducerClass(WordCountReduce.class);
/*指定reduce的输出key*/
job.setOutputKeyClass(Text.class);
/*指定reduce的输出value*/
job.setOutputValueClass(IntWritable.class); /* 定义主类*/
job.setJarByClass(WordCountTest.class); /*定义reduce的输出文件路径*/
Path outPath = new Path("C:/Users/暖阳/Desktop/WordCountTest");
/*输出最终结果文件路径*/
FileOutputFormat.setOutputPath(job, outPath); /*提交job并关闭程序*/
System.exit(job.waitForCompletion(true) ? 0 : 1); }catch (Exception e){
e.printStackTrace();
}finally {
System.out.println("最终的");
}
}
}

输出结果

hello 5

jerry 1

kitty 1

tom 2

world 1

最新文章

  1. (转帖)开源容器集群管理系统Kubernetes架构及组件介绍
  2. linux下常见解压缩命令
  3. mssql
  4. 20145223《Java程序程序设计》第1周学习总结
  5. 从 Eclipse 导入项目到 Android Studio
  6. tilemap坐标转换
  7. hdu 1195 Open the Lock
  8. 山寨小小军团开发笔记 之 GamePool
  9. 职业操盘手内部教材 z
  10. HTML目录
  11. HDOJ/HDU 1022 Train Problem I(模拟栈)
  12. 设计 REST 风格的 MVC 框架
  13. c++virtual inline 是否冲突
  14. 基于visual Studio2013解决面试题之0807strstr函数
  15. 7种创建线程方式,你知道几种?线程系列Thread(一)
  16. 梅安森元图地图开放平台、专业GIS地图平台
  17. FeignClient调用POST请求时查询参数被丢失的情况分析与处理
  18. android启动画面隐藏状态栏全屏显示
  19. git协同开发
  20. 斐波那契数列(python)

热门文章

  1. 上传文件到阿里云linux服务器(windows到Linux的文件上传)
  2. java-集合-realdo-集合一对多
  3. 学习tomcat(三)
  4. vue 初识(基础语法与数据驱动模型)
  5. 学习FastDfs(二)
  6. MEVN 架构(MongoDB + Express + Vue + NODEJS)搭建
  7. fetch,终于认识你
  8. ES6-11学习笔记--深拷贝与浅拷贝
  9. Unknown host mirrors.opencas.cn You may need to adjust the proxy settings in Gradle 报错及解决办法
  10. 记一次dotnet拆分包,并希望得大佬指点