前言:今天刚开始看到map和reduce类里面的内容时,说实话一片迷茫,who are you?,最后实在没办法,上B站看别人的解说视频,再加上自己去网上查java的包的解释,终于把WordCount例子看懂,准备后面自己写一遍!实话说,现在实在肝不动了,每天只有晚上有点时间来学习,代码贴上来,睡觉!
正文:实在不想写太多,解释都在代码的注释里面,饶了我吧!
贴一个讲的比较好的网址:https://www.cnblogs.com/houji/p/7161468.html
代码如下:
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hadoop; import java.io.IOException;
import java.util.StringTokenizer; 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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser; public class WordCount {//WordCount是类名,要用public class进行修饰,java程序由类(class)组成,一个源文件可以包含多个类 public static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable>{
/***
Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT>
行偏移量 输入值 输出key 输出值
***/ private final static IntWritable one = new IntWritable(1);
private Text word = new Text(); public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());//value.toString()获取输入值,并用StringTokenizer进行分隔(默认空格)
while (itr.hasMoreTokens()) { //判断itr是否还有字符串,返回true或false
word.set(itr.nextToken()); //set方法给word赋值,nextToken()返回下一个标记
context.write(word, one);//输出<'word',1>
}
}
} public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values, //values 里面存储着map输出数据,格式为 'word list<1,1,1,1,1>'
Context context
) throws IOException, InterruptedException { int sum = 0;//自定义一个计数器
for (IntWritable val : values) { //循环list里面的值
sum += val.get();//求和
}
result.set(sum);//赋值给result
context.write(key, result);
}
} public static void main(String[] args) throws Exception { //1、Java程序的入口,public static void main(String[] args){}是固定用法,public static void都是关键字。2、throws:声明一个异常可能被抛出
/***
Create a new Job
***/
Configuration conf = new Configuration(); //实例化Configuration,读取Hadoop配置信息
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); //读取Hadoop的argument填入地址信息
if (otherArgs.length < 2) {//若填入的地址小于2,报错并输出"Usage: wordcount <in> [<in>...] <out>"
System.err.println("Usage: wordcount <in> [<in>...] <out>");
System.exit(2);
}
Job job = Job.getInstance(conf, "word count");//单例模式getInstance(),在主函数开始时调用,返回一个实例化对象,此对象是static的,在内存中保留着它的引用
job.setJarByClass(WordCount.class);
//设置Job处理的Map(拆分)、Combiner(中间结果合并)以及Reduce(合并)的相关处理类
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
//设置job输出结果<key,value>的中key和value数据类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
/***
调用addInputPath()和setOutputPath()设置输入输出路径置
InputFormat()方法是用来生成可供map处理的<key,value>对的
***/
for (int i = 0; i < otherArgs.length - 1; ++i) {
FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
}
FileOutputFormat.setOutputPath(job,
new Path(otherArgs[otherArgs.length - 1]));
System.exit(job.waitForCompletion(true) ? 0 : 1); //运行job
}
}