八、手把手教MapReduce 单词统计案例编程
来源:互联网 发布:删除表数据的sql语句 编辑:程序博客网 时间:2024/05/17 07:33
1、在Linux 系统中搭建Eclipse 和Maven 环境,创建Maven Project
2、安装jdk,并配置环境变量。
3、配置maven ,配置环境变量,用root用户身份。
4.配置Maven仓库
5.解压eclipse
6.以普通用户打开eclipse,配置maven
修改pom.xml
配置输出路径
二、基于八古文格式编写WordCount 程序
一个简单的MapReduce程序只需要指定map()、reduce()、input和output,剩下的事由框架完成。
package org.apache.hadoop.mr01;import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.conf.Configured;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IntWritable;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 org.apache.hadoop.util.Tool;import org.apache.hadoop.util.ToolRunner;public class WordCount extends Configured implements Tool {//四个泛型,前两个是输入类型,后两个是输出类型。 public static class WordCountMapper extends Mapper<LongWritable,Text,Text,IntWritable> { private final static IntWritable mapOutputValue=new IntWritable(1); private Text mapOutputKey=new Text(); //每次读一行数据就调用一次该方法 @Override public void map(LongWritable key,Text value,Context context) throws IOException, InterruptedException{ String line=value.toString(); String[] words=line.split(" "); for(String word:words){ mapOutputKey.set(word); context.write(mapOutputKey, mapOutputValue); } } } public static class WordCountReducer extends Reducer<Text,IntWritable,Text,IntWritable>{ private IntWritable outputValue = new IntWritable() ; @Override public void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException { int sum = 0 ; //遍历value的list,累加求和 for(IntWritable value: values){ sum += value.get() ; } outputValue.set(sum); //输出一个单词的统计结果 context.write(key, outputValue); }} public int run(String[] args) throws Exception { //获取configuration Configuration configuration = super.getConf() ; // 创建job Job job = Job.getInstance(configuration,this.getClass().getSimpleName()); job.setJarByClass(this.getClass()); //input Path inPath = new Path(args[0]) ; FileInputFormat.addInputPath(job, inPath); // mapper job.setMapperClass(WordCountMapper.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(IntWritable.class); // reducer job.setReducerClass(WordCountReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); //output Path outPath = new Path(args[1]); FileOutputFormat.setOutputPath(job, outPath); //提交job boolean isSuccess = job.waitForCompletion(true); return isSuccess ? 0 : 1 ; } public static void main(String[] args) throws Exception { Configuration configuration = new Configuration(); int status = ToolRunner.run(configuration,new WordCount(), args) ; System.exit(status); }}
打包JAR,在YARN 运行测试
三、以WordCount 程序为例,理解MapReduce 如何并行分析数
默认一个分片split进行一个map处理,多个map经过中间的数据转换进入一个或多个reduce处理。
1、input读入数据,按行读取,转换格式为<key,value>为map的输入文件,key是这行数据在文件中的偏移量。Value是这行数据的内容。
2、在map中对这行数据进行split分割,形成map的输出文件类似为<word,1>。
3、经过中间过程的转换,进入reduce中,reduce的输入格式<key,values>,类似为<hadoop,list(1,1,1,1)>。Reduce对value值进行累加。
4、output输入数据,默认情况下每个<Key,Value>输出一行数据。
key和value之间分隔符为制表符。
0 0
- 八、手把手教MapReduce 单词统计案例编程
- MapReduce 单词统计编程
- 模拟MapReduce编程的程序案例(用于统计文本中单词出现频率)
- python MapReduce单词统计
- 【MapReduce实例】单词统计
- 单词统计案例
- Hadoop系列-MapReduce编程入门案例(八)
- Akka MapReduce 去停词单词统计
- 利用mapreduce统计单词个数
- 使用Mapreduce案例编写用于统计文本中单词出现的次数的案例、mapreduce本地运行等,Combiner使用及其相关的知识,流量统计案例和流量总和以及流量排序案例,自定义Partitioner
- Hadoop示例程序之单词统计MapReduce
- Hadoop示例程序之单词统计MapReduce
- Hadoop示例程序之单词统计MapReduce
- Hadoop示例程序之单词统计MapReduce
- Hadoop-MapReduce初步应用-统计单词个数
- Hadoop/MapReduce(单词统计--读写数据库)
- Mapreduce实例---统计单词个数(wordcount)
- MapReduce 开篇练习之 统计单词
- Redis命令初学记录
- 模块化
- redis集群安装脚本
- 按着北京-时间/手机-时间 每10分钟开始一次倒计时
- visual studio 单元测试
- 八、手把手教MapReduce 单词统计案例编程
- HDU1411-校庆神秘建筑(四面体体积)
- NYOJ 599 奋斗的蜗牛
- 自定义评星控件
- OpenJudge 简单的整数划分问题(递归)
- AMD与CMD区别
- 装船问题
- C++局部变量和全局变量
- Codeforces 669C Little Artem and Matrix【思维+暴力】