现在来一部分 一部分的理解程序:
要写一个mapreduce程序,首先要实现一个map函数和reduce函数。
map的方法:
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context)
/** * KEYIN 即K1 表示每一行的起始位置(偏移量offset) * VALUEIN 即v1 表示每一行的文本内容 * KEYOUT 即k2 表示每一行中的每个单词 * VALUEOUT 即v2 表示每一行中的每个单词的出现次数,固定值1
**/
这里有三个参数,前面两个LongWritable key, Text value就是输入的key和value,第三个参数Context context这是可以记录输入的key和value,例如:
context.write(new Text(word), new LongWritable(1));
此外context还会记录map运算的状态。
对于reduce函数的方法:
protected void reduce(Text k2, Iterable<LongWritable> v2s,Reducer<Text, LongWritable, Text, LongWritable>.Context context)
/** * KEYIN 即k2 表示每一行中的每个单词 * VALUEIN 即v2 表示每一行中每个单词出现次数,固定值1 * KEYOUT 即k3 表示整个文件中的不同单词 * VALUEOUT 即v3 表示整个文件中的不同单词的出现总次数 **/
reduce函数的输入也是一个key/value的形式,不过它的value是一个迭代器的形式Iterable<IntWritable> values,也就是说reduce的输入是一个key对应一组的值的value,reduce也有context和map的context作用一致。
下面就是main函数的调用了:
Configuration conf = new Configuration();
运行mapreduce程序前都要初始化Configuration,该类主要是读取mapreduce系统配置信息,这些信息包括hdfs还有mapreduce,也就是安装hadoop时候的配置文件例如:core-site.xml、hdfs-site.xml和mapred-site.xml等等文件里的信息,我们开发mapreduce时候只是在填空,在map函数和reduce函数里编写实际进行的业务逻辑,其它的工作都是交给mapreduce框架自己操作的,但是至少我们要告诉它怎么操作啊,比如hdfs在哪里啊,mapreduce的jobstracker在哪里啊,而这些信息就在conf包下的配置文件里。
package cn.hx.test;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
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;
public class WordCountApp {
//自定义的mapper,继承org.apache.hadoop.mapreduce.Mapper
public static class MyMapper extends org.apache.hadoop.mapreduce.Mapper<LongWritable, Text, Text, LongWritable>{
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context)
throws IOException, InterruptedException {
String line = value.toString();
//split 函数是用于按指定字符(串)或正则去分割某个字符串,结果以字符串数组形式返回,这里按照"\t"来分割text文件中字符,即一个制表符,这就是为什么我在文本中用了空格分割,导致最后的结果有很大的出入。
String[] splited = line.split("\t");
//foreach 就是 for(元素类型t 元素变量x:遍历对象obj){引用x的java语句}
for (String word : splited) {
context.write(new Text(word), new LongWritable(1));
}
}
}
public static class MyReducer extends org.apache.hadoop.mapreduce.Reducer<Text, LongWritable, Text, LongWritable>{
@Override
protected void reduce(Text k2, Iterable<LongWritable> v2s,
Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {
long count = 0L;
for (LongWritable v2 : v2s) {
count += v2.get();
}
LongWritable v3 = new LongWritable(count);
context.write(k2, v3);
}
}
//客户端代码,写完交给ResourceManager框架去执行
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, WordCountApp.class.getSimpleName());
//打成jar执行
job.setJarByClass(WordCountApp.class);
//数据在哪里?
FileInputFormat.setInputPaths(job, args[0]);
//使用哪个mapper处理输入的数据?
job.setMapperClass(MyMapper.class);
//map输出的数据类型是什么?
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class);
//使用哪个reducer处理输入的数据?
job.setReducerClass(MyReducer.class);
//reduce输出的数据类型是什么?
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
//数据输出到哪里?
FileOutputFormat.setOutputPath(job, new Path(args[1]));
//交给yarn去执行,直到执行结束才退出本程序
job.waitForCompletion(true);
}
}