package com.sky.mr.wordcount;
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 org.junit.Test;
import java.io.IOException;
public class WordcountMapper extends Mapper<LongWritable,Text, Text, IntWritable> {
//由于每读一行文本数据,就要调用一次map方法,为了避免多次创建对象,浪费内存资源,将Text,IntWritable对象创建在
//map方法之外
Text k = new Text();
IntWritable v = new IntWritable(1);
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//获取每一行的文本内容
String line = value.toString();
//按空格分割
String[] words = line.split(" ");
//转换数据格式,输出
for ( String word: words) {
k.set(word);
context.write(k, v);
}
}
}
package com.sky.mr.wordcount;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class WordcountReducer extends Reducer<Text, IntWritable, Text,IntWritable> {
IntWritable v = new IntWritable();
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
//求每组相同key的总个数
int sum = 0;
for ( IntWritable count:values) {
sum += count.get();
}
//输出
v.set(sum);
context.write(key, v);
}
}
package com.sky.mr.wordcount;
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;
import java.io.IOException;
public class WordcountDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
//1、获取配置信息以及job对象
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
//2、设置jar包路径
job.setJarByClass(WordcountDriver.class);
//3、关联自定义mapper和reducer类
job.setMapperClass(WordcountMapper.class);
job.setReducerClass(WordcountReducer.class);
//4、设置Map输出key和value类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
//5、设置最终结果key,value类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
//6、设置文件输入输出路径
FileInputFormat.setInputPaths(job,new Path(args[0]));
FileOutputFormat.setOutputPath(job,new Path(args[1]));
//7、将封装了MapReduce程序运行参数的job对象,提交到Yarn集群
boolean result = job.waitForCompletion(true);
System.exit(result?0:1);
}
}
import org apache hadoop io
import org apache hadoop io
import org apache hadoop
import java io IOException
IOException 1
apache 3
hadoop 3
import 4
io 3
java 1
org 3
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。