hadoop计算单词出现次数

来源:互联网 发布:linux 内核版本 编辑:程序博客网 时间:2024/05/21 08:42

WCJob.java

程序执行的入口,主要设置一些配置文件和指定map类和reduce类

package com.wang.wc;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FileSystem;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;public class WCJob {public static void main(String[] args) throws Exception {// TODO Auto-generated method stub//默认为src目录下的配置文件Configuration conf = new Configuration();//其他方法设置conf//conf.set("fs.defaultFS", "hdfs://namenode:9000");//conf.set("yarn.resourcemanager.hostname", "namenode");//该方法需要将项目打包成jar包,放入hadoop集群中执行//conf.set("mapred.jar","C:\\Users\\Adminstrator\\Desktop\\WorldCount.jar");Job job = Job.getInstance(conf);//在hadoop集群上运行这个作业时,要把代码打包成jar文件,//hadoop利用这个类来查找包含它的jar文件,进而找到相关的jar文件job.setJarByClass(WCJob.class);//指定map类型和reduce类型job.setMapperClass(WCMapper.class);job.setReducerClass(WCReducer.class);//setMapOutputKeyClass和setMapOutputValueClass控制map和reduce函数的输出类型job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(IntWritable.class);//使用FileInputFormat类制定输入数据和输出数据的文件地址FileInputFormat.addInputPath(job, new Path("/wc/input/wc"));Path outpath = new Path("/wc/output");FileSystem fs = FileSystem.get(conf);//-----if(fs.exists(outpath)){fs.delete(outpath, true);}FileOutputFormat.setOutputPath(job, outpath);boolean flag = job.waitForCompletion(true);if(flag){System.out.println("success!");}}}

该类指定map方法
WCMapper.java

map类是一个泛型类型,有四个形参类型,分别指定map函数的输入key类型,输入value类型,输出key类型,输出value类型。

package com.wang.wc;import java.io.IOException;import org.apache.commons.lang.StringUtils;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Mapper;//LongWritable相当于java的Long类型,Text相当于java中的String类型,这些类都在org.apache.hadoop.io包中public class WCMapper extends Mapper<LongWritable, Text, Text, IntWritable>{@Overrideprotected void map(LongWritable key,Text value,Context context) throws IOException, InterruptedException{               //将Text类型转换为String类型                String str = value.toString();String[] strs = StringUtils.split(str,' ');                Context实例用于输出内容的写入               for(String s:strs){context.write(new Text(s), new IntWritable(1));}}}

该类指定reduce方法

package com.wang.wc;import java.io.IOException;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Reducer;public class WCReducer extends Reducer<Text, IntWritable, Text, IntWritable>{@Overrideprotected void reduce(Text text, Iterable<IntWritable> iterable,Context context)throws IOException, InterruptedException {int sum = 0;for(IntWritable i :iterable){sum += i.get();}context.write(text, new IntWritable(sum));} }


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 眼角两边长痘痘怎么办 坐动车行李超重怎么办 海康威视黑屏了怎么办 备孕喝酒抽烟了怎么办 鼻炎鼻子不通气怎么办速效办法 2岁宝宝智力落后怎么办 六个月宝宝尖足怎么办 3个月宝宝尖足怎么办 2岁宝宝发育慢怎么办 2岁宝宝便秘怎么办呀 2岁宝宝老是便秘怎么办 宝宝4岁不说话怎么办 宜家柜子味道重怎么办 家里有小飞虫怎么办呀 汽车里进老鼠了怎么办 老鼠跑到车里了怎么办 我的小车有老鼠怎么办 车里面进了老鼠怎么办 街电充电宝丢了怎么办 充电宝充不了电怎么办 脚裸扭伤肿了怎么办 大货车电瓶亏电怎么办 货车电瓶被偷了怎么办 小乌龟尾巴断了怎么办 长青春痘怎么办简单小妙招 一关灯就有蚊子怎么办 狗狗误食蟑螂药怎么办 泰迪误食蟑螂药怎么办 猫咪吃了蟑螂药怎么办 狗把蟑螂药吃了怎么办 猫吃了蟑螂诱饵怎么办 猫吃了蟑螂中毒怎么办 孕妇胃烧的难受怎么办 6个月孕妇胃难受怎么办 飞机杯吸盘不上怎么办 我的车位被占用怎么办 占别人车位的车怎么办 头受凉受风了疼怎么办 看电脑时间长了眼睛疼怎么办 电脑玩久了头疼怎么办 屋里有死老鼠味怎么办