• 我们使用之前搭建好的Hadoop环境,可参见:
《【Hadoop环境搭建】Centos6.8搭建hadoop伪分布模式》http://www.cnblogs.com/ssslinppp/p/5923793.html   
  • 示例程序为《Hadoop权威指南3》中的获取最高温度的示例程序;

数据准备

输入数据为:sample.txt


  1. 0067011990999991950051507004+68750+023550FM-12+038299999V0203301N00671220001CN9999999N9+00001+99999999999
  2. 0043011990999991950051512004+68750+023550FM-12+038299999V0203201N00671220001CN9999999N9+00221+99999999999
  3. 0043011990999991950051518004+68750+023550FM-12+038299999V0203201N00261220001CN9999999N9-00111+99999999999
  4. 0043012650999991949032412004+62300+010750FM-12+048599999V0202701N00461220001CN0500001N9+01111+99999999999
  5. 0043012650999991949032418004+62300+010750FM-12+048599999V0202701N00461220001CN0500001N9+00781+99999999999

将samle.txt上传至HDFS


  1. hadoop fs -put /home/hadoop/ncdcData/sample.txt input


项目结构


MaxTemperatureMapper类

  1. package com.ll.maxTemperature;
  2. import java.io.IOException;
  3. import org.apache.hadoop.io.IntWritable;
  4. import org.apache.hadoop.io.LongWritable;
  5. import org.apache.hadoop.io.Text;
  6. import org.apache.hadoop.mapreduce.Mapper;
  7. public class MaxTemperatureMapper extends
  8. Mapper<LongWritable, Text, Text, IntWritable> {
  9. private static final int MISSING = 9999;
  10. @Override
  11. public void map(LongWritable key, Text value, Context context)
  12. throws IOException, InterruptedException {
  13. String line = value.toString();
  14. String year = line.substring(15, 19);
  15. int airTemperature;
  16. if (line.charAt(87) == '+') { // parseInt doesn't like leading plus
  17. // signs
  18. airTemperature = Integer.parseInt(line.substring(88, 92));
  19. } else {
  20. airTemperature = Integer.parseInt(line.substring(87, 92));
  21. }
  22. String quality = line.substring(92, 93);
  23. if (airTemperature != MISSING && quality.matches("[01459]")) {
  24. context.write(new Text(year), new IntWritable(airTemperature));
  25. }
  26. }
  27. }
  28. // ^^ MaxTemperatureMapper

MaxTemperatureReducer类

  1. package com.ll.maxTemperature;
  2. import java.io.IOException;
  3. import org.apache.hadoop.io.IntWritable;
  4. import org.apache.hadoop.io.Text;
  5. import org.apache.hadoop.mapreduce.Reducer;
  6. public class MaxTemperatureReducer extends
  7. Reducer<Text, IntWritable, Text, IntWritable> {
  8. @Override
  9. public void reduce(Text key, Iterable<IntWritable> values, Context context)
  10. throws IOException, InterruptedException {
  11. int maxValue = Integer.MIN_VALUE;
  12. for (IntWritable value : values) {
  13. maxValue = Math.max(maxValue, value.get());
  14. }
  15. context.write(key, new IntWritable(maxValue));
  16. }
  17. }
  18. // ^^ MaxTemperatureReducer

MaxTemperature类(主函数)

  1. package com.ll.maxTemperature;
  2. import org.apache.hadoop.fs.Path;
  3. import org.apache.hadoop.io.IntWritable;
  4. import org.apache.hadoop.io.Text;
  5. import org.apache.hadoop.mapreduce.Job;
  6. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  7. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  8. public class MaxTemperature {
  9. public static void main(String[] args) throws Exception {
  10. if (args.length != 2) {
  11. args = new String[] {
  12. "hdfs://localhost:9000/user/hadoop/input/sample.txt",
  13. "hdfs://localhost:9000/user/hadoop/out2" };
  14. }
  15. Job job = new Job(); // 指定作业执行规范
  16. job.setJarByClass(MaxTemperature.class);
  17. job.setJobName("Max temperature");
  18. FileInputFormat.addInputPath(job, new Path(args[0]));
  19. FileOutputFormat.setOutputPath(job, new Path(args[1])); // Reduce函数输出文件的写入路径
  20. job.setMapperClass(MaxTemperatureMapper.class);
  21. job.setCombinerClass(MaxTemperatureReducer.class);
  22. job.setReducerClass(MaxTemperatureReducer.class);
  23. job.setOutputKeyClass(Text.class);
  24. job.setOutputValueClass(IntWritable.class);
  25. System.exit(job.waitForCompletion(true) ? 0 : 1);
  26. }
  27. }
  28. // ^^ MaxTemperature
解释说明:
输入路径为:hdfs://localhost:9000/user/hadoop/input/sample.txt
这部分由两部分组成:
  1. hdfs://localhost:9000/;
  2. /user/hadoop/input/sample.txt
其中hdfs://localhost:9000/由文件core-size.xml进行设置:

其中/user/hadoop/input/sample.txt就是上面准备数据时sample.txt存放的路径:

输出路径为:hdfs://localhost:9000/user/hadoop/out2
需要注意的是,在执行MapReduce时,这个输出路径一定不要存在,否则会出错。

pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.ll</groupId>
  5. <artifactId>MapReduceTest</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>MapReduceTest</name>
  9. <url>http://maven.apache.org</url>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. <hadoopVersion>1.2.1</hadoopVersion>
  13. <junit.version>3.8.1</junit.version>
  14. </properties>
  15. <dependencies>
  16. <dependency>
  17. <groupId>junit</groupId>
  18. <artifactId>junit</artifactId>
  19. <version>${junit.version}</version>
  20. <scope>test</scope>
  21. </dependency>
  22. <!-- Hadoop -->
  23. <dependency>
  24. <groupId>org.apache.hadoop</groupId>
  25. <artifactId>hadoop-core</artifactId>
  26. <version>${hadoopVersion}</version>
  27. <!-- Hadoop -->
  28. </dependency>
  29. </dependencies>
  30. </project>

程序测试

Hadoop环境准备

我们使用之前搭建好的Hadoop环境,可参见:
《【Hadoop环境搭建】Centos6.8搭建hadoop伪分布模式》http://www.cnblogs.com/ssslinppp/p/5923793.html 

生成jar包

下面是生成jar包过程




上传服务器并运行测试


使用默认的输入输出路径:

  1. hadoop jar mc.jar


指定输入输出路径:

  1. hadoop jar /home/hadoop/jars/mc.jar hdfs://localhost:9000/user/hadoop/input/sample.txt hdfs://localhost:9000/user/hadoop/out5


【Hadoop测试程序】编写MapReduce测试Hadoop环境的更多相关文章

  1. hive--构建于hadoop之上、让你像写SQL一样编写MapReduce程序

    hive介绍 什么是hive? hive:由Facebook开源用于解决海量结构化日志的数据统计 hive是基于hadoop的一个数据仓库工具,可以将结构化的数据映射为数据库的一张表,并提供类SQL查 ...

  2. Hadoop实战5:MapReduce编程-WordCount统计单词个数-eclipse-java-windows环境

    Hadoop研发在java环境的拓展 一 背景 由于一直使用hadoop streaming形式编写mapreduce程序,所以目前的hadoop程序局限于python语言.下面为了拓展java语言研 ...

  3. Hadoop实战3:MapReduce编程-WordCount统计单词个数-eclipse-java-ubuntu环境

    之前习惯用hadoop streaming环境编写python程序,下面总结编辑java的eclipse环境配置总结,及一个WordCount例子运行. 一 下载eclipse安装包及hadoop插件 ...

  4. Hadoop:使用Mrjob框架编写MapReduce

    Mrjob简介 Mrjob是一个编写MapReduce任务的开源Python框架,它实际上对Hadoop Streaming的命令行进行了封装,因此接粗不到Hadoop的数据流命令行,使我们可以更轻松 ...

  5. Hadoop学习笔记(4) ——搭建开发环境及编写Hello World

    Hadoop学习笔记(4) ——搭建开发环境及编写Hello World 整个Hadoop是基于Java开发的,所以要开发Hadoop相应的程序就得用JAVA.在linux下开发JAVA还数eclip ...

  6. [Hadoop in Action] 第4章 编写MapReduce基础程序

    基于hadoop的专利数据处理示例 MapReduce程序框架 用于计数统计的MapReduce基础程序 支持用脚本语言编写MapReduce程序的hadoop流式API 用于提升性能的Combine ...

  7. Hadoop学习笔记:使用Mrjob框架编写MapReduce

    1.mrjob介绍 一个通过mapreduce编程接口(streamming)扩展出来的Python编程框架. 2.安装方法 pip install mrjob,略.初学,叙述的可能不是很细致,可以加 ...

  8. Hadoop通过HCatalog编写Mapreduce任务访问hive库中schema数据

    1.dirver package com.kangaroo.hadoop.drive; import java.util.Map; import java.util.Properties; impor ...

  9. hadoop研究:mapreduce研究前的准备工作

    继续研究hadoop,有童鞋问我,为啥不接着写hive的文章了,原因主要是时间不够,我对hive的研究基本结束,现在主要是hdfs和mapreduce,能写文章的时间也不多,只有周末才有时间写文章,所 ...

随机推荐

  1. VMDK镜像迁移到KVM

    The vmware system consists of two disks in raw format: the old boot disk and the second one. It is W ...

  2. Core Java Volume I — 4.5. Method Parameters

    4.5. Method ParametersLet us review the computer science terms that describe how parameters can be p ...

  3. mac中open用法

    sage: open [-e] [-t] [-f] [-W] [-R] [-n] [-g] [-h] [-b <bundle identifier>] [-a <applicatio ...

  4. 【BZOJ1005】【HNOI2008】明明的烦恼

    又是看黄学长的代码写的,估计我的整个BZOJ平推计划都要看黄学长的代码写 原题: 自从明明学了树的结构,就对奇怪的树产生了兴趣......给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间连 ...

  5. JQ插件jquery.fn.extend与jquery.extend

    jQuery为开发插件提拱了两个方法,分别是: JavaScript代码 jQuery.fn.extend(object); jQuery.extend(object); jQuery.extend( ...

  6. matlab:对一个向量进行排序,返回每一个数据的rank 序号 。。。

    %% Rank the entropy_loss     % for iiii = 1:size(Group_age, 1)  %     count_1 = 0 ;%     tmp = Group ...

  7. Python-正则零宽断言及命名捕获(类PHP)

    (一)零宽断言 说明:本文的例子使用python描述      首先说明一下什么是零宽断言,所谓零宽断言就是并不去真正的匹配字符串文本,而仅仅是匹配对应的位置.      正则表达式中有很多这样的断言 ...

  8. android:ScrollView嵌套ListView的问题

    在ScrollView中嵌套使用ListView,看起来ListView只会显示一行多一点,不能滑动.ListView的高度怎么改都有问题,与预期不符合.搜索了一些解决方案,我觉得最好不要用这样的设计 ...

  9. adaptive hash index

    An optimization for InnoDB tables that can speed up lookups using = and IN operators, by constructin ...

  10. C/C++数组名与指针的区别详解

    1.数组名不是指针我们看下面的示例: #include <iostream> int main() { ]; char *pStr = str; cout << sizeof( ...