MapReduce编程详解

MapReduce编程模型

将作业拆分为Map和Reduce两个阶段

  • 准备map处理的输入数据
  • Mapper处理
  • Shuffle
  • Reduce处理
  • 结果输出
    在这里插入图片描述

入门:编写MapReduce程序

编写wordcount程序

  1. 编辑pom.xml,添加jar包
1
2
3
4
5
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
  1. 编写Mapper类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.biezhi.bigdata.hadoop.hdfs.mapreduce;

import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;

import java.io.IOException;
public class WordCountMapper extends Mapper<LongWritable,Text,Text,IntWritable>{


@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

// 把value对应的行数据按照指定的分隔符拆开
String[] words = value.toString().split("\t");


for(String word : words) {
// (hello,1) (world,1)
context.write(new Text(word.toLowerCase()), new IntWritable(1));
}
}
}
  1. 编写Reducer类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.biezhi.bigdata.hadoop.hdfs.mapreduce;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Iterator;

public class WordCountReduce extends Reducer<Text,IntWritable, Text,IntWritable>{

@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

int count = 0;

Iterator<IntWritable> iterator = values.iterator();

//<1,1,1>
while (iterator.hasNext()) {
IntWritable value = iterator.next();
count += value.get();
}

context.write(key, new IntWritable(count));
}
}
  1. 编写主类,提交MR作业
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.biezhi.bigdata.hadoop.hdfs.mapreduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.net.URI;


/**
* 使用MR统计HDFS上的文件对应的词频
*
* Driver: 配置Mapper,Reduce的相关属性
*
*/
public class WordCountApp {


public static void main(String[] args) throws Exception{

System.setProperty("HADOOP_USER_NAME", "hadoop");

Configuration configuration = new Configuration();
configuration.set("fs.defaultFS","hdfs://192.168.2.131:8020");


// 创建一个Job
Job job = Job.getInstance(configuration);

// 设置Job对应的参数: 主类
job.setJarByClass(WordCountApp.class);

// 设置Job对应的参数: 设置自定义的Mapper和Reducer处理类
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReduce.class);

// 设置Job对应的参数: Mapper输出key和value的类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);

// 设置Job对应的参数: Reduce输出key和value的类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);

// 如果输出目录已经存在,则先删除
FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.2.131:8020"),configuration, "hadoop");
Path outputPath = new Path("/wordcount/output");
if(fileSystem.exists(outputPath)) {
fileSystem.delete(outputPath,true);
}

// 设置Job对应的参数: Mapper输出key和value的类型:作业输入和输出的路径
FileInputFormat.setInputPaths(job, new Path("/wordcount/input"));
FileOutputFormat.setOutputPath(job, outputPath);

// 提交job
boolean result = job.waitForCompletion(true);

System.exit(result ? 0 : -1);

}
}

进阶:实战词频统计

数据下载及源码下载

Powered by Hexo and Hexo-theme-hiker

Copyright © 2013 - 2021 Inner peace All Rights Reserved.

UV : | PV :