MapReduce的WordCount应用实例
2018-03-19 15:44
351 查看
1、新建一个IDEA的Maven工程
2、引入依赖
3、Mapper类
package com.motoon;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
/**
* @author rjsong
*/
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
/**
* map阶段的业务逻辑就写在自定义的map()方法中
* maptask会对每一行输入数据调用一次我们自定义的map()方法
*/
protected void map(LongWritable key, Text value, Context context) throws IOException,InterruptedException{
//将maptask传过来的文本先转换成String
String line = value.toString();
//根据空格将这一行分成单词
String[] words =line.split(" ");
//将单词输出为<单词,1>
for (String word:words){
//将单词作为key,将次数1作为value,以便于后续的数据开发,可以根据单词分发,以便于相同单词会到相同的reduce task
context.write(new Text(word), new IntWritable(1));
}
}
}
4、Reducer类
package com.motoon;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
/**
* @author rjsong
*/
public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException,InterruptedException{
int count = 0;
for (IntWritable value:values){
count+=value.get();
}
context.write(key, new IntWritable(count));
}
}
5、main类
package com.motoon;
import org.apache.hadoop.conf.Configuration;
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;
/**
* @author rjsong
*/
public class WordCountDriver {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job wordCountJob = Job.getInstance(conf);
//重要:指定本job所在的jar包
wordCountJob.setJarByClass(WordCountDriver.class);
//设置wordCountJob所用的mapper逻辑类为哪个类
wordCountJob
c40c
.setMapperClass(WordCountMapper.class);
//设置wordCountJob所用的reducer逻辑类为哪个类
wordCountJob.setReducerClass(WordCountReducer.class);
//设置map阶段输出的kv数据类型
wordCountJob.setMapOutputKeyClass(Text.class);
wordCountJob.setMapOutputValueClass(IntWritable.class);
//设置最终输出的kv数据类型
wordCountJob.setOutputKeyClass(Text.class);
wordCountJob.setOutputValueClass(IntWritable.class);
//设置要处理的文本数据所存放的路径
FileInputFormat.setInputPaths(wordCountJob, "hdfs://192.168.61.138:9000/wordcount/input/");
FileOutputFormat.setOutputPath(wordCountJob, new Path("hdfs://192.168.61.138:9000/wordcount/output/"));
//提交job给hadoop
wordCountJob.waitForCompletion(true);
}
}
6、导出jar包,在工作区间目录下会产生一个out目录,里面有相应的jar文件。
7、将jar文件上传到linux服务器上
8、创建文本数据
9、将文本数据上传到hdfs上
10、运行jar文件
11、显示结果
2、引入依赖
3、Mapper类
package com.motoon;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
/**
* @author rjsong
*/
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
/**
* map阶段的业务逻辑就写在自定义的map()方法中
* maptask会对每一行输入数据调用一次我们自定义的map()方法
*/
protected void map(LongWritable key, Text value, Context context) throws IOException,InterruptedException{
//将maptask传过来的文本先转换成String
String line = value.toString();
//根据空格将这一行分成单词
String[] words =line.split(" ");
//将单词输出为<单词,1>
for (String word:words){
//将单词作为key,将次数1作为value,以便于后续的数据开发,可以根据单词分发,以便于相同单词会到相同的reduce task
context.write(new Text(word), new IntWritable(1));
}
}
}
4、Reducer类
package com.motoon;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
/**
* @author rjsong
*/
public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException,InterruptedException{
int count = 0;
for (IntWritable value:values){
count+=value.get();
}
context.write(key, new IntWritable(count));
}
}
5、main类
package com.motoon;
import org.apache.hadoop.conf.Configuration;
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;
/**
* @author rjsong
*/
public class WordCountDriver {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job wordCountJob = Job.getInstance(conf);
//重要:指定本job所在的jar包
wordCountJob.setJarByClass(WordCountDriver.class);
//设置wordCountJob所用的mapper逻辑类为哪个类
wordCountJob
c40c
.setMapperClass(WordCountMapper.class);
//设置wordCountJob所用的reducer逻辑类为哪个类
wordCountJob.setReducerClass(WordCountReducer.class);
//设置map阶段输出的kv数据类型
wordCountJob.setMapOutputKeyClass(Text.class);
wordCountJob.setMapOutputValueClass(IntWritable.class);
//设置最终输出的kv数据类型
wordCountJob.setOutputKeyClass(Text.class);
wordCountJob.setOutputValueClass(IntWritable.class);
//设置要处理的文本数据所存放的路径
FileInputFormat.setInputPaths(wordCountJob, "hdfs://192.168.61.138:9000/wordcount/input/");
FileOutputFormat.setOutputPath(wordCountJob, new Path("hdfs://192.168.61.138:9000/wordcount/output/"));
//提交job给hadoop
wordCountJob.waitForCompletion(true);
}
}
6、导出jar包,在工作区间目录下会产生一个out目录,里面有相应的jar文件。
7、将jar文件上传到linux服务器上
8、创建文本数据
9、将文本数据上传到hdfs上
10、运行jar文件
11、显示结果
相关文章推荐
- hadoop MapReduce实例解析(wordcount例子)
- Hadoop之道--MapReduce之Hello World实例wordcount
- 第一个MapReduce应用:WordCount
- 3.2 Mapreduce实例—WordCount
- 运行mapreduce实例wordcount时, 出现ClassNotFoundException: org.myorg.WordCount错误解决方法
- Hadoop之道--MapReduce之Hello World实例wordcount
- 学习Hadoop第十一课(MapReduce的实例---WordCount)
- Hive应用实例:WordCount
- Mapreduce实例---统计单词个数(wordcount)
- MapReduce WordCount编程实例
- 第一个MapReduce应用:WordCount
- hadoop MapReduce实例解析(WordCount)
- 大数据之MapReduce详解(MR的运行机制及配合WordCount实例来说明运行机制)
- MapReduce编程实例之WordCount
- Mapreduce WordCount实例
- mapreduce Wordcount输入文件在hdfs上的实例
- mapreduceword count实例
- Hadoop学习笔记之初识MapReduce以及WordCount实例分析
- Mapreduce 测试自带实例 wordcount
- hadoop集群配置方法---mapreduce应用:xml解析+wordcount详解---yarn配置项解析