:-:
由上图可归纳出WordCount的MapReduceb编程模型,需要注意的如下:
# Hadoop数据类型和Java数据类型转换
String --> Text
int --> IntWritable
long --> LongWritable
null --> NullWritable
[warning] 开发预热工作
- 使用IDEA工具开发java代码,需要具备Java SE 和IDEA的基础
- (1)在IDEA中引入所需的jar包,IDEA支持文件夹方式引入
- (2)代码编写(项目名称以各位同学名称命名)
- 1.编写Mapper函数
- 2.编写Reducer函数
- 3.编写Main函数主入口
- (3)代码打包
- (4)程序调试
- 1.伪分布式调试(仿真)
- 2.本地主机调试(便捷)
[info] (1)编程需要依赖的Jar包,把所有包放在一个文件夹,在IDEA中引入
//在Hadoop的对应包下面能找到jar包
hadoop-2.7.3/share/hadoop/common/lib
hadoop-2.7.3/share/hadoop/common/
hadoop-2.7.3/share/hadoop/mapreduce/lib
hadoop-2.7.3/share/hadoop/mapreduce/
:-:
[info] (2)编写Mapper代码
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class WordCountMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
/*
* key: 输入的key
* value: 数据 I love Beijing
* context: Map上下文
*/
String data= value.toString();
//分词
String[] words = data.split(" ");
//输出每个单词
for(String w:words){
context.write(new Text(w), new LongWritable(1));
}
}
}
:-:
[info] (3)编写Reducer代码
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class WordCountReducer extends Reducer<Text, LongWritable, Text, LongWritable>{
@Override
protected void reduce(Text k3, Iterable<LongWritable> v3,Context context) throws IOException, InterruptedException {
//v3: 是一个集合,每个元素就是v2
long total = 0;
for(LongWritable l:v3){
total = total + l.get();
}
//输出
context.write(k3, new LongWritable(total));
}
}
:-:
[info] (4)编写Main函数主入口
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class WordCountMain {
public static void main(String[] args) throws Exception {
//创建一个job = map + reduce
Configuration conf = new Configuration();
//创建一个Job
Job job = Job.getInstance(conf);
//指定任务的入口
job.setJarByClass(WordCountMain.class);
//指定job的mapper
job.setMapperClass(WordCountMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class);
//指定job的reducer
job.setReducerClass(WordCountReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
//指定任务的输入和输出
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
//提交任务
job.waitForCompletion(true);
}
}
:-:
:-:
[info] (5)代码打包
:-:
:-:
:-:
:-:
:-:
[info] (6)把生成WordCount的Java程序传上去伪分布式环境运行(上传software目录)
:-:
[info] (7)程序调试
# 1.新建一个文件,添加内容以统计字数,上传HDFS(若前面的实验有文件则不需要再上传)
# 2.执行jar命令执行程序,注意output目录不能存在,若存在请先删除
hadoop jar hadoop.jar /huatec/word.txt /huatec/output
:-:
:-:
[info] IDEA本地目录调试
内容来源于网络,如有侵权,请联系作者删除!