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));} }
阅读全文
0 0
- hadoop计算单词出现次数
- Hadoop实现单词出现次数排序
- [hadoop]简单的MapReduce项目,计算文件中单词出现的次数(五)
- Hadoop 统计文件中某个单词出现的次数
- 计算数组中任意一个单词出现的次数(HashMap)
- 计算文件中所有单词出现的次数
- 统计单词出现次数
- 单词的出现次数
- 读取txt文档中单词,并计算单词出现的次数(英文文档)
- 读取txt文档中单词,并计算单词出现的次数(英文文档)
- 统计单词出现的次数
- baidu单词出现的次数
- 统计单词出现的次数
- 统计单词出现的次数
- 统计单词出现的次数。
- java按单词出现次数统计单词
- 统计单词出现的次数并按单词出现的次数顺序输出单词及其次数
- String_ComputeMostFrequentlyWord , in String (在 字符串 中 计算 出现次数最多的单词 )
- 网页爬虫-用PHP的拓展库curl实现模拟登录慕课网
- 并发
- 视图控制器的生命周期方法
- Qt绘图程序
- classpath、path、JAVA_HOME的作用及JAVA环境变量配置
- hadoop计算单词出现次数
- 跟我一学linux基础(第四天)
- 剑指offer:跳台阶
- 关于代码的优化
- Oracle数据库之SQL(高级查询)详解
- CentOS常用备忘
- 20170715离线赛比赛总结
- MyBatis多对多以及一二级缓存
- AngularJs小案例_代办与已办事项