mapreduce单词统计
该程序的功能:
(1)读取文件
(2)统计每个单词出现的数量
1.创建words.txt文件并上传到HDFS
创建words.txt文件,添加内容
vim words.txt
#添加单词(任意单词)
hadoop,hive,hbase
spark,flink,kafka
python,java,scala
sqoop,hello,world
sqoop,hello,world
sqoop,hello,world
sqoop,hello,world
sqoop,hello,world
上传到HDFS
hdfs dfs -put words.txt /input1
2.编写代码
package MapReduce;import org.apache.hadoop.conf.Configuration;
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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import java.io.IOException;
/*** 读取文件* 统计每个单词出现的数量*/
public class Demo01WordCount {//map/*** Mapper<IntWritable,Text,Text,IntWritable>* Mapper<输入Map的key的类型,输入Map的value的类型,Map输出的key的类型,Map输出的value的类型>* Map默认的inputformat是TextInputFormat* TextInputFormat:会将数据每一行的偏移量作为key,每一行作为value输入到Map端*/public static class MyMapper extends Mapper<LongWritable,Text,Text,LongWritable>{//实现自己的map逻辑/**** @param key:输入map的key* @param value:输入map的value* @param context:MapReduce程序的上下文环境,Map端的输出可以通过context发送到Reduce端* @throws IOException* @throws InterruptedException*/@Overrideprotected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context) throws IOException, InterruptedException {//获取一行数据String line = value.toString();String[] split = line.split(",");for (String word : split) {context.write(new Text(word),new LongWritable(1));}}}/*** Reducer<Text,IntWritable,Text,IntWritable>* Mapper<Map输出的key的类型,Map输出的value的类型,Reduce输出key的类型,Reduce输出的value的类型>*///reducepublic static class MyReducer extends Reducer<Text,LongWritable,Text,LongWritable>{/**** @param key:从Map端输出并经过分组后的key(相当于对map输出的key做一个group by)* @param values:从Map端输出并经过分组后的key,对应的value集合* @param context:MapReduce程序的上下文环境 Reduce端的输出可以通过context最终写道hdfs集群上* @throws IOException* @throws InterruptedException*/@Overrideprotected void reduce(Text key, Iterable<LongWritable> values, Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {int sum = 0;for (LongWritable value : values) {sum += value.get();}context.write(key,new LongWritable(sum));}}public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {Configuration conf = new Configuration();// 配置reduce最终输出的key value的分隔符为 #conf.set("mapred.textoutputformat.separator", "#");// 做其他的配置// 创建一个JobJob job = Job.getInstance(conf);// 设置reduce的个数job.setNumReduceTasks(2);// 设置Job的名字job.setJobName("MyWordCountMapReduceApp");// 设置MapReduce运行的类job.setJarByClass(Demo01WordCount.class);// 配置Map// 配置Map Task运行的类job.setMapperClass(MyMapper.class);// 设置Map任务输出的key的类型job.setMapOutputKeyClass(Text.class);// 设置Map任务输出的value的类型job.setMapOutputValueClass(LongWritable.class);// 配置Reduce// 配置Reduce Task运行的类job.setReducerClass(MyReducer.class);// 设置Reduce任务输出的key的类型job.setOutputKeyClass(Text.class);// 设置Reduce任务输出的value的类型job.setOutputValueClass(LongWritable.class);// 配置输入输出路径// 将输入的第一个参数作为输入路径,第二个参数作为输出的路径FileInputFormat.addInputPath(job, new Path(args[0]));FileOutputFormat.setOutputPath(job, new Path(args[1]));// 提交任务并等待运行结束job.waitForCompletion(true);}
}
3.打包,上传到master上
打包.jar
通过Xftp上传至master
4.运行mapreduce程序
hadoop jar HDFSJavaAPI-1.0-SNAPSHOT.jar MapReduce.Demo01WordCount /input1/words.txt /output2
5.查看结果
hdfs dfs -ls /output2
mapreduce单词统计相关推荐
- hadoop基础教程(二) MapReduce 单词统计
1.这是hadoop基础系列教程,适合入门者学习. 2.MapReduce是一种分布式计算模型,解决海量数据问题,由两个阶段组成,map()和reduce().本文不讲解原理,下面实际操作利用MapR ...
- 关于MapReduce单词统计的例子:
要统计的文件的文件名为hello hello中的内容如下 hello you hello me 通过MapReduce程序统计出文件中的各个单词出现了几次.(两个单词之间通过tab键进行的分割) 1 ...
- MapReduce之单词统计
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 MapReduce单词统计 一.搭建环境 二.编写WordCount程序 1.在IDEA中引入所需的jar包,IDEA支持文件夹方式引 ...
- Mapreduce程序 统计文件中每个单词出现次数
mapreduce程序 统计文件中每个单词出现次数 调用MapReduce对文件中各个单词出现次数进行统计 一.安装环境 二.准备工作 1.创建Hadoop账户 2.更新 apt 3.安装vim 4. ...
- mapreduce程序本地运行,单词统计案例
mapreduce程序本地运行单词统计案例,输入输出数据放在本地 集群模式运行:https://blog.csdn.net/weixin_43614067/article/details/108400 ...
- 运行Hadoop自带的wordcount单词统计程序
1.使用示例程序实现单词统计 (1)wordcount程序 wordcount程序在hadoop的share目录下,如下: 1 2 3 4 5 6 7 8 9 [root@leaf mapreduce ...
- 大数据阶段划分及案例单词统计
大数据阶段的重要课程划分 离线分析 : hadoop生态圈 HDFS, MapReduce(概念偏多), hive(底层是MapReduce), 离线业务分析80%都是使用hive实时分析 : spa ...
- Hadoop单词统计
1. 本地创建文本文件 [root@s166 fantj]# mkdir input [root@s166 fantj]# cd input/ [root@s166 input]# echo &quo ...
- 用spark实现单词统计
1.在本地运行 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext}/*** 用spark ...
最新文章
- 计算机基础知识第十讲,计算机文化基础(第十讲)学习笔记
- DataRow的序列化问题
- 成为 Gartner 广域网基础设施报告领导者第二位,Fortinet 做对了什么?
- 服务监控-URL接口监控
- 【每周CV论文推荐】 初学深度学习人脸关键点检测必读文章
- 数字类 default 0和 default 0_0秒入住 无感体验 悉点科技引领酒店数字化大潮
- excel函数vloopup使用方法
- Java基础---变量(三)
- 为PHP7安装Windows Server 2012 R2过程记录
- java不同环境_Spring Boot系列 – 5. 不同的环境使用不同的配置
- 中文python笔记_Python学习笔记-基础篇
- 使用 CODING 进行 Spring Boot 项目的集成
- Java笔记-使用ServerSocket构建HTTP服务器
- 通过ssh方式clone项目_参与开源项目流程
- 智慧气象 开源_来自开源的半条命11条智慧
- linux snap文件夹,SNAP 文件扩展名: 它是什么以及如何打开它?
- 大写日期转换器(大写日期转换器)
- 什么是光纤?光纤的原理是什么?你能想象没有光纤通讯的世界么?
- 关于人性的一些思考:如何提高员工工作热情与成就感,以及因材施教的心灵培训
- 浏览器产生乱码的原因