【版本】
当前版本号v20250326
版本 | 修改说明 |
---|
v20250326 | 修正部分命令错误 |
v20250213 | 初始化版本 |
任务3.1 - 编写 MapReduce wordcount 程序
【任务目的】
【任务环境】
- Windows 7 以上64位操作系统
- JDK8
- IDEA
- Hadoop 3
- Maven 3
【任务资源】
【任务内容】
- 编写 MapReduce wordcount 程序
【任务步骤】
打开 Part 4 的 hadoopexp 项目。
在项目hadoop-exp\src\main\java
下创建一个名为hadoop+你学号后4位.mapreduce.wc
的包。注意替换为你的学号后4位。
在包下面新建一个 WordCountMapper
的类。代码如下,注意替换为你的学号后4位。
| package hadoop你的学号后4位.mapreduce.wc; |
| |
| 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 WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{ |
| @Override |
| protected void map(LongWritable key1,Text v1,Context context) |
| throws IOException,InterruptedException{ |
| String data=v1.toString(); |
| |
| String[] words=data.split(" "); |
| |
| for(String w:words){ |
| context.write(new Text(w),new IntWritable(1)); |
| } |
| } |
| } |
复制
- 在包下面新建一个
WordCountReducer
的类。代码如下,注意替换为你的学号后4位。
| package hadoop你的学号后4位.mapreduce.wc; |
| |
| import org.apache.hadoop.io.IntWritable; |
| import org.apache.hadoop.io.Text; |
| import org.apache.hadoop.mapreduce.Reducer; |
| import java.io.IOException; |
| |
| public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> { |
| @Override |
| protected void reduce(Text k3, Iterable<IntWritable> v3,Context context) throws IOException, InterruptedException { |
| |
| |
| int total = 0; |
| for(IntWritable v:v3){ |
| total += v.get(); |
| } |
| |
| context.write(k3, new IntWritable(total)); |
| } |
| } |
复制
- 在包下面新建一个
WordCountMain
的类。代码如下,注意替换为你的学号后4位。
| package hadoop你的学号后4位.mapreduce.wc; |
| |
| 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; |
| import org.apache.hadoop.util.GenericOptionsParser; |
| |
| import java.io.IOException; |
| |
| public class WordCountMain { |
| |
| public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { |
| Configuration conf = new Configuration(); |
| String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); |
| int argLength=otherArgs.length; |
| if (otherArgs.length < 2) { |
| System.err.println("Usage: wordcount <in> [<in>...] <out>"); |
| System.exit(2); |
| } |
| |
| Job job=Job.getInstance(conf,"Word Count"); |
| |
| job.setJarByClass(WordCountMain.class); |
| |
| job.setMapperClass(WordCountMapper.class); |
| |
| job.setReducerClass(WordCountReducer.class); |
| |
| job.setCombinerClass(WordCountReducer.class); |
| |
| job.setOutputKeyClass(Text.class); |
| |
| job.setOutputValueClass(IntWritable.class); |
| |
| for(int i=0;i<argLength-1;i++) { |
| FileInputFormat.addInputPath(job, new Path(otherArgs[i])); |
| } |
| |
| FileOutputFormat.setOutputPath(job,new Path(otherArgs[argLength-1])); |
| |
| System.exit(job.waitForCompletion(true) ? 0 : 1); |
| } |
| } |
| |
复制
- 修改项目的
pom.xml
。其中<mainClass>
标签内的类修改为WordCountMain
类的包内路径。参考代码如下,注意替换为你的学号后4位。
| <build> |
| <plugins> |
| <plugin> |
| <groupId>org.apache.maven.plugins</groupId> |
| <artifactId>maven-jar-plugin</artifactId> |
| <version>3.2.0</version> |
| <configuration> |
| <archive> |
| |
| <manifest> |
| |
| <mainClass>hadoop你的学号后4位.mapreduce.wc.WordCountMain</mainClass> |
| </manifest> |
| </archive> |
| </configuration> |
| </plugin> |
| </plugins> |
| </build> |
复制
- 在 IDEA 的左下角找到 Terminal,输入以下命令,使用 Maven 对代码进行打包。

| mvn compile package -Dmaven.test.skip=true |
复制
生成的 jar 包会在项目的 target 目录下。名称为hadoop-exp-1.0.jar
。
把hadoop-exp-1.0.jar
和countryroad.txt
都上传到NodeA
的/home/hadoop
目录下。
在NodeA
启动Hadoop.
复制
- 把
NodeA
本地的countryroad.txt
文件上传到 HDFS 的根目录/
。
| hdfs dfs -put /home/hadoop/countryroad.txt / |
复制
- 在
NodeA
上执行以下命令,开始运行我们编写的 Wordcount 程序。
| hadoop jar /home/hadoop/hadoop-exp-1.0.jar /countryroad.txt /output6 |
复制
- 运行成功以后,查看结果。
| hdfs dfs -cat /output6/part-r-00000 |
复制
任务3.2 - 编写 MapReduce 程序统计数据
【任务目的】
【任务环境】
- Windows 7 以上64位操作系统
- JDK8
- IDEA
- Hadoop 3
- CentOS 7
- Maven 3
【任务资源】
复制
【任务内容】
【任务要求】
【任务步骤】
打开 Part 4 的 hadoopexp 项目。
在项目hadoop-exp\src\main\java
下创建一个名为hadoop+你学号后4位.mapreduce.sales
的包。注意替换为你的学号后4位。
在包下面新建一个 SalesMapper
的类。下面是部分参考代码,请完成关键的编码部分,注意替换为你的学号后4位。
| package hadoop你的学号后4位.mapreduce.sales; |
| |
| import org.apache.hadoop.io.*; |
| import org.apache.hadoop.mapreduce.Mapper; |
| |
| import java.io.IOException; |
| |
| public class SalesMapper extends Mapper<LongWritable, Text, Text, DoubleWritable>{ |
| @Override |
| protected void map(LongWritable key1,Text v1,Context context) |
| throws IOException,InterruptedException{ |
| String data=v1.toString(); |
| |
| String[] words=data.split(","); |
| |
| |
| 此处关键代码请自己完成 |
| } |
| } |
复制
- 在包下面新建一个
SalesReducer
的类。代码如下,注意替换为你的学号后4位。
| package hadoop你的学号后4位.mapreduce.sales; |
| |
| import org.apache.hadoop.io.DoubleWritable; |
| import org.apache.hadoop.io.Text; |
| import org.apache.hadoop.mapreduce.Reducer; |
| import java.io.IOException; |
| |
| public class SalesReducer extends Reducer<Text, DoubleWritable, Text, DoubleWritable> { |
| @Override |
| protected void reduce(Text k3, Iterable<DoubleWritable> v3,Context context) throws IOException, InterruptedException { |
| 此处关键代码请自己完成 |
| } |
| } |
复制
- 在包下面新建一个
SalesMain
的类。代码如下,注意替换为你的学号后4位。
| package hadoop你的学号后4位.mapreduce.sales; |
| |
| import org.apache.hadoop.conf.Configuration; |
| import org.apache.hadoop.fs.Path; |
| import org.apache.hadoop.io.DoubleWritable; |
| 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 org.apache.hadoop.util.GenericOptionsParser; |
| |
| import java.io.IOException; |
| |
| public class SalesMain { |
| |
| public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { |
| Configuration conf = new Configuration(); |
| String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); |
| int argLength=otherArgs.length; |
| if (otherArgs.length < 2) { |
| System.err.println("Usage: xxx.jar <in> [<in>...] <out>"); |
| System.exit(2); |
| } |
| |
| Job job=Job.getInstance(conf,"Sales Statistic"); |
| |
| job.setJarByClass(SalesMain.class); |
| |
| job.setMapperClass(SalesMapper.class); |
| |
| job.setReducerClass(SalesReducer.class); |
| |
| job.setCombinerClass(SalesReducer.class); |
| |
| job.setOutputKeyClass(Text.class); |
| |
| job.setOutputValueClass(DoubleWritable.class); |
| |
| for(int i=0;i<argLength-1;i++) { |
| FileInputFormat.addInputPath(job, new Path(otherArgs[i])); |
| } |
| |
| FileOutputFormat.setOutputPath(job,new Path(otherArgs[argLength-1])); |
| |
| System.exit(job.waitForCompletion(true) ? 0 : 1); |
| } |
| } |
复制
- 修改项目的
pom.xml
。其中<mainClass>
标签内的类修改为SalesMain
类的包内路径。参考代码如下,注意替换为你的学号后4位。
| <build> |
| <plugins> |
| <plugin> |
| <groupId>org.apache.maven.plugins</groupId> |
| <artifactId>maven-jar-plugin</artifactId> |
| <version>3.2.0</version> |
| <configuration> |
| <archive> |
| |
| <manifest> |
| |
| <mainClass>hadoop你的学号后4位.mapreduce.sales.SalesMain</mainClass> |
| </manifest> |
| </archive> |
| </configuration> |
| </plugin> |
| </plugins> |
| </build> |
复制
- 参考实验6.1 打包、上传包、运行和查看结果的步骤。参考结果如下:
| 1998 2.408391494998411E7 |
| 1999 2.221994766001435E7 |
| 2000 2.37655066200018E7 |
| 2001 2.8136461979984347E7 |
复制
##【常见问题】
1. IDEA在命令行运行打包命令出错。
答:这是因为 IDEA 某些新版本使用powershell 作为默认命令行,以下命令会出错。
| mvn compile package -Dmaven.test.skip=true |
复制
| mvn compile package '-Dmaven.test.skip=true' |
复制