1 下载气象数据集部分数据(下载地址:http://www1.ncdc.noaa.gov/pub/data/noaa/2001/),求每年的最低温度,部署并运行之,抓图过程
2 在linux或win下安装eclipse,并且连接到Hadoop集群(关键步骤是编译插件),运行习题1的map-reduce程序作为测试,抓图整个过程
第3-4题为2选1(请在课程资源下载第4周作业素材和视频素材),如能2题均完成为最佳。
3 传递参数问题
请阅读Exercise_1.java,编译并且运行。该程序从Test_1改编而来,其主要差别在于能够让用户在结果文件中的每一行前面添加一个用户自定义的字符串,而这个字符串将由参数传递到程序中。例如,运行
$hadoop jar Exercise_1.jar input_path output_path hadoop
之后,第三个参数“hadoop”将会在结果文件中显示,例如附件“result_1”所显示的。
问题:着重考虑Exercise_1.java里面”需要注意的部分“,改写Test_2程序,得到的结果必须跟附件"resule_2"一致,并且其中hadoop必须由参数传递。
4 Setup函数的作用
阅读Exercise_2.java,这是一个与Exercise_1.java有相同效果的程序,自行搜索关于setup函数的资料,回答上述两个程序的不同。
1、
设置依赖的环境变量
[keon@h1 code]$ vim ~/.bash_profile
export HADOOP_HOME=/usr/hadoop-2.7.2
export JAVA_HOME=/usr/jdk1.8.0_101
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$HADOOP_HOME/s
hare/hadoop/common/hadoop-common-2.7.2.jar:$HADOOP_HOME/share/hadoop/hdfs/hadoop
-hdfs-2.7.2.jar:$HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-client-core
-2.7.2.jar:$HADOOP_HOME/share/hadoop/tools/lib/commons-cli-1.2.jar
编译代码
[keon@h1 wether]$ javac code/*
[keon@h1 wether]$ cd code/
[keon@h1 code]$ ls
MaxTemperature.class MaxTemperatureMapper.class MaxTemperatureReducer.class
MaxTemperature.java MaxTemperatureMapper.java MaxTemperatureReducer.java
打包class文件
[keon@h1 code]$ jar cvf ./MaxTemperature.jar ./*.class
added manifest
adding: MaxTemperature.class(in = 2665) (out= 1304)(deflated 51%)
adding: MaxTemperatureMapper.class(in = 1893) (out= 818)(deflated 56%)
adding: MaxTemperatureReducer.class(in = 1704) (out= 728)(deflated 57%)
准备数据文件
[keon@h1 data]$ cat * >> wetherdata.txt
[keon@h1 data]$ ls
007026-99999-2016 008415-99999-2016 010014-99999-2001 wetherdata.txt
008411-99999-2016 010000-99999-2001 010015-99999-2001
008414-99999-2016 010010-99999-2001 010017-99999-2001
[keon@h1 hadoop-2.7.2]$ ./bin/hadoop fs -mkdir /input
[keon@h1 hadoop-2.7.2]$ ./bin/hadoop fs -put ~/hadoop/wether/data/wetherdata.txt /input
运行程序
./bin/hadoop jar ~/hadoop/wether/code/Weather.jar /input /output


2、
为项目添加hadoopjar包

添加运行参数

运行获得结果

查看结果

3、
运行exercise_1

处理后的数据

添加reduce

结果

4、

Mapper和reducer的四个方法是setup,map(reduce),cleanup和run。其中,setup和cleanup用于管理生命周期中的资源,setup在完成构造,即将开始执行动作前调用,cleanup则在所有的动作完成后被调用。方法map(reduce)用于对一次输入的key/value对进行map(reduce)动作。run方法执行了上面描述的过程,它调用setup,让后迭代所有的key/value对,进行map(reduce),最后调用cleanup。
exercise_1获取名字是在map中进行,每次迭代都会进行获取,改为使用setup后,只会获取一次,提高效率。
网友评论