集群包含三个节点:1个namenode、2个datanode,其中节点之间能够相互ping通。节点IP地址和主机名分布以下:java
序号 | IP地址 | 机器名 | 类型 | 用户名 | 运行进程 |
---|---|---|---|---|---|
1 | 192.168.33.200 | Master | 名称节点 | haha | NN、SNN、JobTracer |
2 | 192.168.33.201 | Slave1 | 数据节点 | haha | DN、TaskTracer |
3 | 192.168.33.202 | Slave2 | 数据节点 | haha | DN、TaskTracer |
4 | 192.168.33.203 | Slave3 | 数据节点 | haha | DN、TaskTracer |
全部节点均是CentOS6.5 64bit系统,防火墙均禁用,全部节点上均建立了一个haha用户,用户主目录是/home/haha。node
下载气象数据集部分数据,写一个Map-Reduce做业,求每一年的最低温度,部署并运行之.
apache
分析Map-Reduce过程
编程
Map-Reduce编程模型
bash
Mapper网络
Mapper负责“分”,即把复杂的任务分解为若干个“简单的任务”执行
“简单的任务”有几个含义:app
Reducerjsp
对map阶段的结果进行汇总ide
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; publicclass MinTemperature { public staticvoid main(String[] args) throws Exception { if(args.length != 2) { System.err.println("Usage: MinTemperature<input path> <output path>"); System.exit(-1); } Job job = new Job(); job.setJarByClass(MinTemperature.class); job.setJobName("Min temperature"); //new Path(args[0])控制台的第一个参数--输入路径 FileInputFormat.addInputPath(job, new Path(args[0])); //new Path(args[1])控制台的第二个参数--输出路径 FileOutputFormat.setOutputPath(job, new Path(args[1])); //指定Mapper是哪一个类 job.setMapperClass(MinTemperatureMapper.class); //指定Reducer是哪一个类 job.setReducerClass(MinTemperatureReducer.class); //指定输出的key和value是什么 job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
import java.io.IOException; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; public class MinTemperatureMapper extends Mapper<LongWritable, Text, Text, IntWritable>{ private static final int MISSING = 9999; @Override public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); String year = line.substring(15, 19); int airTemperature; if(line.charAt(87) == '+') { airTemperature = Integer.parseInt(line.substring(88, 92)); } else { airTemperature = Integer.parseInt(line.substring(87, 92)); } String quality = line.substring(92, 93); if(airTemperature != MISSING && quality.matches("[01459]")) { context.write(new Text(year), new IntWritable(airTemperature)); } } }
import java.io.IOException; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; public class MinTemperatureReducer extends Reducer<Text, IntWritable, Text, IntWritable> { @Override public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int minValue = Integer.MAX_VALUE; for(IntWritable value : values) { minValue = Math.min(minValue, value.get()); } context.write(key, new IntWritable(minValue)); } }
进入/home/haha/hadoop-1.1.2/myclass目录,在该目录中创建MinTemperature.Java
、MinTemperatureMapper.java
和MinTemperatureReducer.java
代码文件,代码内容为2.2所示,执行命令以下:工具
[haha@Master ~]$cd /home/haha/hadoop-1.1.2/myclass/ [haha@Master myclass]$vi MinTemperature.java [haha@Master myclass]$vi MinTemperatureMapper.java [haha@Master myclass]$vi MinTemperatureReducer.java
MinTemperature.java
MinTemperatureMapper.java
MinTemperatureReducer.java
在/home/haha/hadoop-1.1.2/myclass目录中,使用以下命令对java代码进行编译,为保证编译成功,加入classpath变量,引入hadoop-core-1.1.2.jar包:
[haha@Master myclass]$javac -classpath ../hadoop-core-1.1.2.jar *.java [haha@Master myclass]$ls [haha@Master myclass]$mv *.jar [haha@Master myclass]$rm *.class
进入/home/haha/hadoop-1.1.2/bin目录,在HDFS中建立气象数据存放路径/user/haha/in,执行命令以下:
cd /home/haha/hadoop-1.1.2/bin hadoop fs -mkdir /user/haha/in hadoop fs -ls /user/haha
使用SSH工具或者scp命令把从NCDC下载的气象数据上传到上步骤建立的目录/user/haha/in中。
使用zcat命令把这些数据文件解压并合并到一个sample.txt文件中,合并后把这个文件上传到HDFS文件系统的/usr/hadoop/in目录中:
cd /user/haha/hadoop-1.1.2/in zcat *.gz > sample.txt hadoop fs -copyFromLocal sample.txt /user/haha/in
气象数据具体的下载地址为 ftp://ftp3.ncdc.noaa.gov/pub/data/noaa/ ,该数据包括1900年到如今全部年份的气象数据,大小大概有70多个G。为了测试简单,咱们这里选取一部分的数据进行测试
以jar的方式启动MapReduce任务,执行输出目录为/user/haha/outputFile:
cd /home/haha/hadoop-1.1.2 hadoop jar MinTemperature.jar MinTemperature /usr/hadoop/in/sample.txt outputFile
执行成功后,查看/user/haha/outputFile目录中是否存在运行结果,使用cat查看结果:
[haha@Master ~]$ hadoop fs -ls /user/haha/outputFile [haha@Master ~]$ hadoop fs -cat /user/haha/outputFile/part-r-00000 [haha@Master ~]$ hadoop fs -cat /user/haha/outputFile/part-r-00000 1972 11
http://master:50030/jobtracker.jsp
已经完成的做业任务:
任务的详细信息:
http://master:50070/dfshealth.jsp
分别查看HDFS文件系统和日志
Q:若是求温度的平均值,能使用combiner吗?有没有变通的方法.
A:不能使用,由于求平均值和前面求最值存在差别,各局部最值的最值仍是等于总体的最值的,可是对于平均值而言,各局部平均值的平均值将再也不是总体的平均值了,因此不能用combiner。能够经过变通的办法使用combiner来计算平均值,即在combiner的键值对中不直接存储最后的平均值,而是存储全部值的和个数,最后在reducer输出时再用和除以个数获得平均值。
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; public class AvgTemperature { public static void main(String[] args) throws Exception { if(args.length != 2) { System.out.println("Usage: AvgTemperatrue <input path><output path>"); System.exit(-1); } Job job = new Job(); job.setJarByClass(AvgTemperature.class); job.setJobName("Avg Temperature"); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.setMapperClass(AvgTemperatureMapper.class); job.setCombinerClass(AvgTemperatureCombiner.class); job.setReducerClass(AvgTemperatureReducer.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(Text.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
import java.io.IOException; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; publicclass AvgTemperatureMapper extends Mapper<LongWritable, Text, Text, Text> { private static final int MISSING = 9999; @Override public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{ String line = value.toString(); String year = line.substring(15, 19); int airTemperature; if(line.charAt(87) == '+') { airTemperature = Integer.parseInt(line.substring(88, 92)); } else { airTemperature = Integer.parseInt(line.substring(87, 92)); } String quality = line.substring(92, 93); if(airTemperature != MISSING && !quality.matches("[01459]")) { context.write(new Text(year), new Text(String.valueOf(airTemperature))); } } }
import java.io.IOException; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; public class AvgTemperatureCombiner extends Reducer<Text, Text, Text, Text>{ @Override public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { double sumValue = 0; long numValue = 0; for(Text value : values) { sumValue += Double.parseDouble(value.toString()); numValue ++; } context.write(key, new Text(String.valueOf(sumValue) + ',' + String.valueOf(numValue))); } }
import java.io.IOException; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; public class AvgTemperatureReducer extends Reducer<Text, Text, Text, IntWritable>{ @Override public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { double sumValue = 0; long numValue = 0; int avgValue = 0; for(Text value : values) { String[] valueAll = value.toString().split(","); sumValue += Double.parseDouble(valueAll[0]); numValue += Integer.parseInt(valueAll[1]); } avgValue = (int)(sumValue/numValue); context.write(key, new IntWritable(avgValue)); } }
进入/home/haha/hadoop-1.1.2/myclass目录,在该目录中创建AvgTemperature.java、AvgTemperatureMapper.java、AvgTemperatureCombiner.java和AvgTemperatureReducer.java代码文件,执行命令以下:
cd /usr/local/hadoop-1.1.2/myclass/ vi AvgTemperature.java vi AvgTemperatureMapper.java vi AvgTemperatureCombiner.java vi AvgTemperatureReducer.java
在/home/user/hadoop-1.1.2/myclass目录中,使用以下命令对java代码进行编译,为保证编译成功,加入classpath变量,引入hadoop-core-1.1.2.jar包:
javac -classpath ../hadoop-core-1.1.2.jar *.java ls
把编译好class文件打包,不然在执行过程会发生错误。把打好的包移动到上级目录并删除编译好的class文件:
jar cvf ./AvgTemperature.jar ./*.class ls mv *.jar .. rm *.class
数据使用求每一年最低温度的气象数据,数据在HDFS位置为/user/haha/in/sample.txt,以jar的方式启动MapReduce任务,执行输出目录为/user/haha/out1:
cd /home/haha/hadoop-1.1.2 hadoop jar AvgTemperature.jar AvgTemperature /user/haha/in/sample.txt /user/haha/out1
执行成功后,查看/user/haha/out1目录中是否存在运行结果,使用cat查看结果:
hadoop fs -ls /user/haha/out1 hadoop fs -cat /user/haha/out1/part-r-00000