一、提出任务
二、任务进行
(一)准备数据
- 启动hadoop服务
1、在虚拟机上创建文本文件
- 创建
calcscore
目录,在里面创建score.txt
文件
2、上传文件到HDFS指定目录
- 创建
/calcscoreinput
目录 - 命令:
hdfs dfs -mkdir -p /calcscore/input
- 将文本文件
score.txt
,上传到HDFS的/calcscore/input
目录 - 命令:
hdfs dfs -put score.txt /calcscore/input
(二)实现步骤
1、创建Maven项目
- Maven项目 :
CalcScore
2、添加相关依赖
- 在
pom.xml
文件里添加hadoop
和junit
依赖
<dependencies>
<!--hadoop客户端-->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.3.4</version>
</dependency>
<!--单元测试框架-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
3、创建日志属性文件
- 在
resources
目录里创建log4j.properties
文件
log4j.rootLogger=ERROR, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/calcscore.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
4、创建成绩映射器类
- 在
net.xxr.mr
里创建ScoreMapper
类
package net.xxr.mr;
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;
public class ScoreMapper extends Mapper <LongWritable, Text, Text, IntWritable>{
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
String[] fields = line.split(" ");
String name = fields[0].trim();
for (int i = 1; i < fields.length; i++) {
int score = Integer.parseInt(fields[i].trim());
context.write(new Text(name), new IntWritable(score));
}
}
}
5、创建成绩驱动器类
- 在
net.xxr.mr
包里创建ScoreDriver
类
package net.xxr.mr;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
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;
public class ScoreDriver {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
conf.set("dfs.client.use.datanode.hostname", "true");
Job job = Job.getInstance(conf);
job.setJarByClass(ScoreDriver.class);
job.setMapperClass(ScoreMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
String uri = "hdfs://master:9000";
Path inputPath = new Path(uri + "/calcscore/input");
Path outputPath = new Path(uri + "/calcscore/output");
FileSystem fs = FileSystem.get(new URI(uri), conf);
fs.delete(outputPath, true);
FileInputFormat.addInputPath(job, inputPath);
FileOutputFormat.setOutputPath(job, outputPath);
job.waitForCompletion(true);
System.out.println("======统计结果======");
FileStatus[] fileStatuses = fs.listStatus(outputPath);
for (int i = 1; i < fileStatuses.length; i++) {
System.out.println(fileStatuses[i].getPath());
FSDataInputStream in = fs.open(fileStatuses[i].getPath());
IOUtils.copyBytes(in, System.out, 4096, false);
}
}
}
6、启动成绩驱动器类,查看结果
7、创建成绩归并器类
- 在
net.xxr.mr
包里创建ScoreReduce
r类
package net.xxr.mr;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
import java.text.DecimalFormat;
public class ScoreReducer extends Reducer<Text, IntWritable, Text, NullWritable> {
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int count = 0;
int sum = 0;
double avg = 0;
for (IntWritable value : values) {
count++;
sum += value.get();
}
avg = sum * 1.0 / count;
DecimalFormat df = new DecimalFormat("#.#");
String scoreInfo = "(" + key + "," + sum + "," + df.format(avg) + ")";
context.write(new Text(scoreInfo), NullWritable.get());
}
}
8、修改成绩驱动器类
- 设置Reducer类及其输出键值类型
9、启动成绩驱动器列,查看结果
- 运行
ScoreDriver
类