Hadoop读书笔记(五)MapReduce统计单词demo
来源:互联网 发布:笔记本触摸屏关闭软件 编辑:程序博客网 时间:2024/06/05 10:16
Hadoop读书笔记(一)Hadoop介绍:http://blog.csdn.net/caicongyang/article/details/39898629
Hadoop读书笔记(二)HDFS的shell操作:http://blog.csdn.net/caicongyang/article/details/41253927
Hadoop读书笔记(三)Java API操作HDFS:http://blog.csdn.net/caicongyang/article/details/41290955
Hadoop读书笔记(四)HDFS体系结构 :http://blog.csdn.net/caicongyang/article/details/41322649
1.demo说明
功能:统计文章中每一个单词出现的次数
数据文件格式:
helloyouhellome
步骤:
1.1读取hdfs中的文件。每一行解析成一个<k,v>。每一个键值对调用一次map函数
1.2 覆盖map(),接收1.1产生的<k,v>,进行处理,转换为新的<k,v>输出
1.3 对1.2输出的<k,v>进行分区。默认分为1个区
1.4 对不同分区中的数据进行排序(按照k)、分组。分组指的是相同key的value放到一个集合中
1.5 (可选)对分组后的数据进行规约。
2.1 多个map任务的输出,按照不同的分区,通过网络copy到不同的reduce节点上。
2.2 对多个map的输出进行合并、排序。覆盖reduce函数,接收的是分组后的数据,实现自己的业务逻辑,处理后,产生新的<k,v>输出。
2.3 对reduce输出的<k,v>写到hdfs中。
2.代码
package mapReduce;import java.io.IOException;import java.net.URI;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FileSystem;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.input.TextInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;import org.apache.hadoop.mapreduce.lib.partition.HashPartitioner;public class WordCount {private static final String INPUT_PATH="hdfs://192.168.80.100:9000/hello";private static final String OUT_PATH="hdfs://192.168.80.100:9000/out";public static void main(String[] args) throws Exception {Configuration conf = new Configuration();FileSystem fileSystem = FileSystem.get(new URI(INPUT_PATH),conf);Path outPath = new Path(OUT_PATH);if(fileSystem.exists(outPath)){fileSystem.delete(outPath,true);}//创建作业Job job = new Job(conf, WordCount.class.getSimpleName());//1.1读取指定的文件FileInputFormat.setInputPaths(job, INPUT_PATH);//输入文件格式化job.setInputFormatClass(TextInputFormat.class);//1.2指定自定义Mapper类job.setMapperClass(MyMapper.class);//指定输出类型job.setOutputKeyClass(Text.class);job.setOutputValueClass(LongWritable.class);//1.3分区job.setPartitionerClass(HashPartitioner.class);//设置reduce个数job.setNumReduceTasks(1);//1.4排序、分组//1.5归约//2.2指定Reducer类job.setReducerClass(MyReducer.class);//设定输出类型job.setOutputKeyClass(Text.class);job.setOutputValueClass(LongWritable.class);//输出地址FileOutputFormat.setOutputPath(job, new Path(OUT_PATH));//输出文件格式化类job.setOutputFormatClass(TextOutputFormat.class);//job交给JobTracker执行job.waitForCompletion(true);}static class MyReducer extends Reducer<Text, LongWritable, Text, LongWritable>{@Overrideprotected void reduce(Text key, Iterable<LongWritable> value, Context context) throws IOException, InterruptedException {long count = 0L;for(LongWritable times :value ){count += times.get();}context.write(key, new LongWritable(count));}}static class MyMapper extends Mapper<LongWritable ,Text,Text,LongWritable>{@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {String[] splited = value.toString().split("\t");for(String word:splited){context.write(new Text(word), new LongWritable(1));}}}}
欢迎大家一起讨论学习!
有用的自己收!
记录与分享,让你我共成长!欢迎查看我的其他博客;
我的个人博客:http://blog.caicongyang.com ;
我的CSDN博客地址: http://blog.csdn.net/caicongyang ;
- Hadoop读书笔记(五)MapReduce统计单词demo
- 和我一起学Hadoop(五):MapReduce的单词统计,wordcount
- Hadoop示例程序之单词统计MapReduce
- Hadoop示例程序之单词统计MapReduce
- Hadoop示例程序之单词统计MapReduce
- Hadoop示例程序之单词统计MapReduce
- Hadoop-MapReduce初步应用-统计单词个数
- Hadoop/MapReduce(单词统计--读写数据库)
- Hadoop读书笔记(六)MapReduce自定义数据类型demo
- Hadoop读书笔记(八)MapReduce 打成jar包demo
- MapReduce操作Hbase 进行单词数量统计Demo
- 【hadoop】 3002-mapreduce程序统计单词个数示例
- [hadoop]简单的MapReduce项目,计算文件中单词出现的次数(五)
- Mapreduce实例---统计单词个数(wordcount)
- MapReduce 单词统计编程
- python MapReduce单词统计
- 【MapReduce实例】单词统计
- 通过hadoop自带的demo运行单词统计
- eclipse导出可被引用的第三方包和导出可执行包的方法
- leetcode-Binary Tree Level Order Traversal 二叉树层序遍历
- poj 1019 Number Sequence 二分
- "could not create the java virtual machine" eclipse报错提示
- win系统维护和XP系统的维护
- Hadoop读书笔记(五)MapReduce统计单词demo
- c++ 虚函数的实现机制:笔记
- Smack PacketReader 监听器启动过程分析
- 循环判断之系列五
- 显示/隐藏标签
- Miller-Rabin算法
- python中read()、readline()和readlines()的区别
- UVA 10603 FILL 隐式图搜索
- IOS开发---C语言-②交换a、b的值