wordcount例子的程序,附带说明
来源:互联网 发布:婚礼布置设计软件 编辑:程序博客网 时间:2024/05/17 08:36
package mapreduce;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.output.FileOutputFormat;/** * helloyou * hellome * */public class WordCountApp {private static final String INPUT_PATH = "hdfs://chaoren1:9000/files";private static final String OUT_PATH = "hdfs://chaoren1:9000/out";public static void main(String[] args) throws Exception{Configuration conf = new Configuration();final FileSystem filesystem = FileSystem.get(new URI(OUT_PATH), conf);filesystem.delete(new Path(OUT_PATH), true);final Job job = new Job(conf , WordCountApp.class.getSimpleName());job.setJarByClass(WordCountApp.class);FileInputFormat.setInputPaths(job, INPUT_PATH);job.setMapperClass(MyMapper.class);job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(LongWritable.class);job.setReducerClass(MyReducer.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(LongWritable.class);FileOutputFormat.setOutputPath(job, new Path(OUT_PATH));job.waitForCompletion(true);}public static class MyMapper extends Mapper<LongWritable, Text, Text, LongWritable>{//解析源文件会产生2个键值对,分别是<0,hello you><10,hello me>;所以map函数会被调用2次protected void map(LongWritable key, Text value, org.apache.hadoop.mapreduce.Mapper<LongWritable,Text,Text,LongWritable>.Context context) throws java.io.IOException ,InterruptedException {//为什么要把hadoop类型转换为java类型?final String line = value.toString();final String[] splited = line.split("\t");//产生的<k,v>对少了for (String word : splited) {//在for循环体内,临时变量word的出现次数是常量1context.write(new Text(word), new LongWritable(1));}};}//map函数执行结束后,map输出的<k,v>一共有4个,分别是<hello,1><you,1><hello,1><me,1>//分区,默认只有一个区//排序后的结果:<hello,1><hello,1><me,1><you,1>//分组后的结果:<hello,{1,1}> <me,{1}> <you,{1}>//归约(可选)//map产生的<k,v>分发到reduce的过程称作shufflepublic static class MyReducer extends Reducer<Text, LongWritable, Text, LongWritable>{//每一组调用一次reduce函数,一共调用了3次//分组的数量与reduce函数的调用次数有什么关系?//reduce函数的调用次数与输出的<k,v>的数量有什么关系?protected void reduce(Text key, java.lang.Iterable<LongWritable> values, org.apache.hadoop.mapreduce.Reducer<Text,LongWritable,Text,LongWritable>.Context context) throws java.io.IOException ,InterruptedException {//count表示单词key在整个文件中的出现次数long count = 0L;for (LongWritable times : values) {count += times.get();}context.write(key, new LongWritable(count));};}}
0 0
- wordcount例子的程序,附带说明
- wordcount例子程序
- Hadoop wordcount程序说明
- hadoop的WordCount例子
- String和StringBuffer的比较(附带程序说明)
- 云计算学习笔记006---运行hadoop的例子程序:统计字符--wordcount例子程序
- 关于linux系统中命令的使用详解1(附带有例子说明)
- 关于linux系统中命令的使用详解2(附带有例子说明)
- Android 资源名获取资源ID的两种实现方式-附带例子说明
- hadooop的wordcount程序
- 分析hadoop的WordCount例子
- HI3531例子程序说明
- Hadoop2.2.0下通过编译单词统计例子WordCount.java熟悉如何编译自己的程序
- gdal的附带工具程序
- gdal的附带工具程序
- 改写Hadoop的wordcount程序
- WordCount程序的简单解释
- 入门Hadoop的WordCount程序
- 形状特征——Sift特征
- LinuxMint笔记:eclipse安装使用
- 关于Java中System.exit(n)的理解
- Android 动画框架详解,第 1 部分
- 神奇的fans
- wordcount例子的程序,附带说明
- SQL范式
- 10684 我要摇出一个妹子[C]
- Perl--包
- JM8.6中帧内帧间模式选择
- 从java虚拟机底层理解java语言运行机制 001
- AVL树的建立完整C代码
- vim
- php毫秒时间截