No1、mapreduce,‘wordcount案例’编程思路
No1-1 :MapReduce运行步骤
input -->map -->reduce-->output
No1-1-1 : input
input阶段:将文件中每行的数据转换成一个{key,value}键值对
key:是数据在每行中的偏移量,value:是数据内容
No1-1-2 : map
map阶段:map获取input输出的value值,并将value拆分split()成单个单词
No1-1-3 : reduce
reduce阶段:获取map拆分的单词将之作为reduce的key,得到{key,value},其中value以集合的形式储存key的数量
(举例)eg:其中一行数据包括三个reduce单词,则在{key,value}中存储则为<reduce,list(1,1,1).
No1-1-4 : output
output阶段:输出reduce的内容,在默认情况下,每个{key,value}输出一行
“`
No2-1 :wordcount编程
No2-1-1:在Linux系统搭建Eclipse和Maven环境以及创建Mven project
关于在Linux搭建Eclipse和Maven环境以及创建Mven project,在之前的文章中有详细介绍,如有疑问可进入以下链接:
[在Linux机上配置Eclipse并部署Maven](http://blog.youkuaiyun.com/huoyuyan/article/details/50650084
No2-1 -2: 代码部分
package com.hao.bigdata.hadoop.mapreduce;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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 WordCountMapReduce {
/***
* maper classs
* @author hao
* public class Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT>
*/
public static class WordCountMapper extends // 繼承mapper的基類
Mapper<LongWritable, Text, Text, IntWritable> {
// set map output value
//创建map输出的Key
private Text mapOutputKey = new Text();
//创建map输出的value值
private final static IntWritable mapOutputvalue = new IntWritable(1);
@Override
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
// line value
//将Text转为字符串类型
String linevalue = value.toString();
// split
//将字符串类型的value进行分割
String[] strs = linevalue.split(" ");
// interator
//通过for循环遍历,input进来的所有数据
for (String str : strs) {
// set map out key and value
//设置map输出
mapOutputKey.set(str);
context.write(mapOutputKey, mapOutputvalue);
}
}
}
/**
* reducer class
* @author hao
* public class Reducer<KEYIN,VALUEIN,KEYOUT,VALUEOUT>
*/
public static class WordCountReduce extends
Reducer<Text, IntWritable, Text, IntWritable> {
//设置reduce value输出的类型
private IntWritable outputValue = new IntWritable();
@Override
public void reduce(Text key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
// SET SUM
int sum = 0;
// interator
//通过for循环,叠加reduce value的值
for (IntWritable value : values) {
// total
sum += value.get();
}
// set output values
outputValue.set(sum);
// output
context.write(key, outputValue);
}
}
/**
* @author hao
* driver
*/
public int run(String args[]) throws Exception {
// step 1: get Configuration
Configuration configuration = new Configuration(); // 读取配置文件
// step 2: creat Job chuanlian input-> map->reduce->output
Job job = Job.getInstance(configuration, this.getClass() //创建job任务
.getSimpleName()
);
job.setJarByClass(this.getClass()); // jar包运行的类
// step job 将个步骤联系在一起
// input ->map ->reduce ->output
// step 3.1:input
Path inPath = new Path(args[0]); // 封装上传路径
FileInputFormat.addInputPath(job, inPath); //读文件,设置路径
// step 3.2:mapper
job.setMapperClass(WordCountMapper.class) // 调用mapper类;
// set map output <key value>
job.setMapOutputKeyClass(Text.class); //设置map-Key输出类型
job.setMapOutputValueClass(IntWritable.class); //设置map-value输出类型
// step 3.3:reducer
job.setReducerClass(WordCountReduce.class); //调用reduce类
// set reduce output <key value>
job.setOutputKeyClass(Text.class); //设置reduce-Key输出类型
job.setOutputValueClass(IntWritable.class); //设置reduce-value输出类型
// step 3.4:output
Path outPath = new Path(args[1]); //封装输出路径
FileOutputFormat.setOutputPath(job, outPath);
boolean isSuccess = job.waitForCompletion(true); //提交
return isSuccess ? 0 : 1;
}
// main
public static void main(String[] args) throws Exception {
// run job
int status = new WordCountMapReduce().run(args);
System.exit(status);
}
}
No3-1 : 打包jar,并在yarn上运行
No3-1-1 :打包jar文件
第一步:右键工程类选择“export”
第二步:选择“java”目录下“jar file”
第三步:选择 存储路径后“下一步”
第四:选择归属的类 后点击”完成“
No3-2-1:在hadoop上查看jar文件
打包完成后,在hadoop上查看是否成功
No3-2 -2 :执行jar文件,wordcount3.jar
[hao@bigdata00 hadoop-2.5.0]$ bin/yarn jar jars/wc-wordcount3.jar /data/inputFiles/input02 /data/outpputFiles/output02
No3-3 : 运行完毕后查看结果
No4-1 : 执行出现的问题
No4-1-1 :出现的问题
在第一次 运行的时候,出现了如下结果:
No4-1-2 :问题原因
原因是在mapper类中少写了红线中一句,导致没有将字符串设置给map的输出对象