需要导入的依赖如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qjl</groupId>
<artifactId>mapreduce</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hadoop.version>2.7.3</hadoop.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>${hadoop.version}</version>
</dependency>
</dependencies>
</project>
Mapper
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
/**
* @author 曲健磊
* @date 2018-11-27 16:15:49
* @description 词频统计的Mapper
*/
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
/**
* context 表示Mapper的上下文
* 上文:HDFS
* 下文:Mapper
*/
// 数据: I love Beijing
String data = value.toString();
// 分词
String[] words = data.split(" ");
// 输出 k2 v2
for (String word : words) {
context.write(new Text(word), new IntWritable(1));
}
}
}
Reducer
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
/**
* @author 曲健磊
* @date 2018-11-27 16:20:38
* @description 词频统计的Reducer
*/
public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
/**
* context 是reducer上下文
* 上文:Mapper
* 下文:HDFS
*/
// 对v3求和
int total = 0;
for (IntWritable value : values) {
total += value.get();
}
context.write(key, new IntWritable(total));
}
}
Job
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
/**
* @author 曲健磊
* @date 2018-11-27 16:24:58
* @description 词频统计任务的执行的入口
*/
public class WordCountMain {
public static void main(String[] args) throws Exception {
// 1.创建一个job指定任务入口
Job job = Job.getInstance(new Configuration());
job.setJarByClass(WordCountMain.class);
// 2.指定job的mapper和输出类型
job.setMapperClass(WordCountMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
// 3.指定job的reducer和输出类型
job.setReducerClass(WordCountReducer.class);
job.setOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
// 4.指定job的输入和输出
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
// 5.执行job,true表示打印日志
job.waitForCompletion(true);
}
}
执行 Job 的流程基本是固定的:
创建一个job指定任务入口
指定job的mapper和输出类型
指定job的reducer和输出类型
指定job的输入和输出
执行job
之后将项目打成 jar 包,上传到 Linux 服务器,提交到 Yarn 上执行:hadoop jar xxx.jar /data.txt /output,如果是本地模式后面跟的两个路径是本地 Linux 的路径,伪分布模式或者全分布模式则表示 HDFS 的路径,第一个表示数据的输入路径,第二个表示数据的输出位置。
---------------------
作者:曲健磊
来源:CSDN
原文:https://blog.csdn.net/a909301740/article/details/84571653
版权声明:本文为博主原创文章,转载请附上博文链接!
|
|