第一个Hadoop程序——Hello Hadoop
本人原创,转载请注明出处:http://blog.csdn.net/panjunbiao/article/details/12773163
下载Hadoop程序包,下载地址:http://hadoop.apache.org/releases.html#Download
如果是在CentOS服务器安装,则执行:
yum install hadoop-1.2.1-1.x86_64.rpm
如果是在Linux或者Mac OS X开发环境下,可以下载bin或者源码包,然后解压缩即可。
验证hadoop二进制执行文件(假设放在~/Developments/toolkits/hadoop-1.2.1文件夹中):
cd ~/Developments/toolkits/hadoop-1.2.1
执行hadoop程序:
bin/hadoop
Usage: hadoop [--config confdir] COMMAND
where COMMAND is one of:
namenode -format format the DFS filesystem
secondarynamenode run the DFS secondary namenode
namenode run the DFS namenode
datanode run a DFS datanode...
出现hadoop命令用法帮助,表示二进制文件可执行。
创建Hello Hadoop的Java项目:
按照《Hadoop权威指南(Hadoop: The Definitive Guide)》的例子,创建3个程序文件。
MaxTemperature.java
/**
* Created with IntelliJ IDEA.
* User: james
* Date: 8/27/13
* Time: 11:33 AM
* To change this template use File | Settings | File Templates.
*/
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 MaxTemperature {
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.err.println("Usage: MaxTemperature <input path> <output path>");
System.exit(-1);
} Job job = new Job();
job.setJarByClass(MaxTemperature.class);
job.setJobName("Max temperature");
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1])); job.setMapperClass(MaxTemperatureMapper.class);
job.setReducerClass(MaxTemperatureReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
MaxTemperatureMapper.java
/**
* Created with IntelliJ IDEA.
* User: james
* Date: 8/27/13
* Time: 11:28 AM
* To change this template use File | Settings | File Templates.
*/
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 MaxTemperatureMapper
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) == '+') { // parseInt doesn't like leading plus signs
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));
}
}
}
MaxTemperatureReducer.java
/**
* Created with IntelliJ IDEA.
* User: james
* Date: 8/27/13
* Time: 11:32 AM
* To change this template use File | Settings | File Templates.
*/
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class MaxTemperatureReducer
extends Reducer<Text, IntWritable, Text, IntWritable> { @Override
public void reduce(Text key, Iterable<IntWritable> values,
Context context)
throws IOException, InterruptedException { int maxValue = Integer.MIN_VALUE;
for (IntWritable value : values) {
maxValue = Math.max(maxValue, value.get());
}
context.write(key, new IntWritable(maxValue));
}
}
需要将hadoop-core-1.2.1.jar文件添加到项目的库中,这个jar文件在解压缩的文件夹中
编译之,假设项目编译到文件夹~/Developments/hello-hadoop/out/production/hello-hadoop/中,将这个文件夹位置输出到HADOOP_CLASSPATH:
export HADOOP_CLASSPATH=~/Developments/hello-hadoop/out/production/hello-hadoop/
另外还要注意定义JAVA_HOME,以Mac OS X为例:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_21.jdk/Contents/Home/
下载天气数据(
http://hadoopbook.com/code.html
),上面有1901年和1902年的天气例子数据。
进入hadoop文件夹:
cd ~/Developments/toolkits/hadoop-1.2.1
执行例子程序(这个MaxTemperature是hadoop程序通过HADOOP_CLASSPATH查找到的):
bin/hadoop MaxTemperature 1901 output
2013-10-15 17:56:40.412 java[5522:1703] Unable to load realm info from SCDynamicStore
13/10/15 17:56:41 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
13/10/15 17:56:41 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same.
13/10/15 17:56:41 WARN mapred.JobClient: No job jar file set. User classes may not be found. See JobConf(Class) or JobConf#setJar(String).
13/10/15 17:56:41 INFO input.FileInputFormat: Total input paths to process : 1
13/10/15 17:56:41 WARN snappy.LoadSnappy: Snappy native library not loaded
13/10/15 17:56:42 INFO mapred.JobClient: Running job: job_local1783370164_0001
13/10/15 17:56:42 INFO mapred.LocalJobRunner: Waiting for map tasks
13/10/15 17:56:42 INFO mapred.LocalJobRunner: Starting task: attempt_local1783370164_0001_m_000000_0
13/10/15 17:56:42 INFO mapred.Task: Using ResourceCalculatorPlugin : null
13/10/15 17:56:42 INFO mapred.MapTask: Processing split: file:/Users/james/Developments/hello-hadoop/out/production/hello-hadoop/1901:0+888190
13/10/15 17:56:42 INFO mapred.MapTask: io.sort.mb = 100
13/10/15 17:56:42 INFO mapred.MapTask: data buffer = 79691776/99614720
13/10/15 17:56:42 INFO mapred.MapTask: record buffer = 262144/327680
13/10/15 17:56:42 INFO mapred.MapTask: Starting flush of map output
13/10/15 17:56:42 INFO mapred.MapTask: Finished spill 0
13/10/15 17:56:42 INFO mapred.Task: Task:attempt_local1783370164_0001_m_000000_0 is done. And is in the process of commiting
13/10/15 17:56:42 INFO mapred.LocalJobRunner:
13/10/15 17:56:42 INFO mapred.Task: Task 'attempt_local1783370164_0001_m_000000_0' done.
13/10/15 17:56:42 INFO mapred.LocalJobRunner: Finishing task: attempt_local1783370164_0001_m_000000_0
13/10/15 17:56:42 INFO mapred.LocalJobRunner: Map task executor complete.
13/10/15 17:56:42 INFO mapred.Task: Using ResourceCalculatorPlugin : null
13/10/15 17:56:42 INFO mapred.LocalJobRunner:
13/10/15 17:56:42 INFO mapred.Merger: Merging 1 sorted segments
13/10/15 17:56:42 INFO mapred.Merger: Down to the last merge-pass, with 1 segments left of total size: 72206 bytes
13/10/15 17:56:42 INFO mapred.LocalJobRunner:
13/10/15 17:56:42 INFO mapred.Task: Task:attempt_local1783370164_0001_r_000000_0 is done. And is in the process of commiting
13/10/15 17:56:42 INFO mapred.LocalJobRunner:
13/10/15 17:56:42 INFO mapred.Task: Task attempt_local1783370164_0001_r_000000_0 is allowed to commit now
13/10/15 17:56:42 INFO output.FileOutputCommitter: Saved output of task 'attempt_local1783370164_0001_r_000000_0' to output
13/10/15 17:56:42 INFO mapred.LocalJobRunner: reduce > reduce
13/10/15 17:56:42 INFO mapred.Task: Task 'attempt_local1783370164_0001_r_000000_0' done.
13/10/15 17:56:43 INFO mapred.JobClient: map 100% reduce 100%
13/10/15 17:56:43 INFO mapred.JobClient: Job complete: job_local1783370164_0001
13/10/15 17:56:43 INFO mapred.JobClient: Counters: 17
13/10/15 17:56:43 INFO mapred.JobClient: File Output Format Counters
13/10/15 17:56:43 INFO mapred.JobClient: Bytes Written=21
13/10/15 17:56:43 INFO mapred.JobClient: File Input Format Counters
13/10/15 17:56:43 INFO mapred.JobClient: Bytes Read=888190
13/10/15 17:56:43 INFO mapred.JobClient: FileSystemCounters
13/10/15 17:56:43 INFO mapred.JobClient: FILE_BYTES_READ=1848986
13/10/15 17:56:43 INFO mapred.JobClient: FILE_BYTES_WRITTEN=245951
13/10/15 17:56:43 INFO mapred.JobClient: Map-Reduce Framework
13/10/15 17:56:43 INFO mapred.JobClient: Reduce input groups=1
13/10/15 17:56:43 INFO mapred.JobClient: Map output materialized bytes=72210
13/10/15 17:56:43 INFO mapred.JobClient: Combine output records=0
13/10/15 17:56:43 INFO mapred.JobClient: Map input records=6565
13/10/15 17:56:43 INFO mapred.JobClient: Reduce shuffle bytes=0
13/10/15 17:56:43 INFO mapred.JobClient: Reduce output records=1
13/10/15 17:56:43 INFO mapred.JobClient: Spilled Records=13128
13/10/15 17:56:43 INFO mapred.JobClient: Map output bytes=59076
13/10/15 17:56:43 INFO mapred.JobClient: Total committed heap usage (bytes)=331350016
13/10/15 17:56:43 INFO mapred.JobClient: SPLIT_RAW_BYTES=141
13/10/15 17:56:43 INFO mapred.JobClient: Map output records=6564
13/10/15 17:56:43 INFO mapred.JobClient: Combine input records=0
13/10/15 17:56:43 INFO mapred.JobClient: Reduce input records=6564
查看输出结果
ls output/
_SUCCESS part-r-00000
vi output/part-r-00000
1901 317
第一个Hadoop程序——Hello Hadoop的更多相关文章
- 编写hadoop程序并打成jar包上传到hadoop集群运行
准备工作: 1. hadoop集群(我用的是hadoop-2.7.3版本),这里hadoop有两种:1是编译好的hadoop-2.7.3:2是源代码hadoop-2.7.3-src: 2. 自己的机器 ...
- IntelliJ IDEA + Maven环境编写第一个hadoop程序
1. 新建IntelliJ下的maven项目 点击File->New->Project,在弹出的对话框中选择Maven,JDK选择你自己安装的版本,点击Next 2. 填写Maven的Gr ...
- hadoop浅尝 第一个hadoop程序
hadoop编程程序员需要完成三个类. map类,reduce类和主类. map和reduce类自然是分别完成map和reduce.而主类则负责对这两个类设置job.完成这三个类之后,我们生成一个ja ...
- 运行第一个Hadoop程序,WordCount
系统: Ubuntu14.04 Hadoop版本: 2.7.2 参照http://www.cnblogs.com/taichu/p/5264185.html中的分享,来学习运行第一个hadoop程序. ...
- 一起学Hadoop——使用IDEA编写第一个MapReduce程序(Java和Python)
上一篇我们学习了MapReduce的原理,今天我们使用代码来加深对MapReduce原理的理解. wordcount是Hadoop入门的经典例子,我们也不能免俗,也使用这个例子作为学习Hadoop的第 ...
- 一个完整的hadoop程序开发过程
目的 说明hadoop程序开发过程 前提条件 ubuntu或同类OS java1.6.0_45 eclipse-indigo hadoop-0.20.2 hadoop-0.20.2-eclipse-p ...
- 第一个Hadoop程序-单词计数
上一篇配置了Hadoop,本文将测试一个Hadoop的小案例 hadoop的Wordcount程序是hadoop自带的一个小的案例,是一个简单的单词统计程序,可以在hadoop的解压包里找到,如下: ...
- 第一个hadoop 程序
首先检查hadoop是否安装并配置正确然后建立WordCount.java文件里面保存package org.myorg; import java.io.IOException;import java ...
- 深入剖析HADOOP程序日志
深入剖析HADOOP程序日志 前提 本文来自于 博客园 逖靖寒的世界 http://gpcuster.cnblogs.com 了解log4j的使用. 正文 本文来自于 博客园 逖靖寒的世界 http: ...
随机推荐
- DEV控件之ChartControl用法
一.总体概述 这个控件包含3层,最外面的chartControl层.中间的XYDiagram层.最里面的Series层.功能非常强大,但同时使用起来也相对复杂,需要各个层之间相互协调设置才能达到自己想 ...
- html系列教程--标题,水平线,注释以及段落
HTML标题 标题,用来显示文章重要性的文字,包含了文章的主旨,类似于作文题目. 标题(Heading)是通过 <h1> - <h6> 等标签进行定义的,由大到小一次排列,h1 ...
- SharePoint2010 Form验证配置流程
1.修改管理中心的Web.config文件,位置:C:\inetpub\wwwroot\wss\VirtualDirectories\42903 2.修改应用程序的Web.config文件,位置:C: ...
- unix网络编程笔记
TCP连接状态转换图:
- U3D学习使用笔记(二)
1.在移动端www.texture使用时不能实时加载纹理,www.LoadImageIntoTexture使用没问题 2.public FaceFeature FaceFeatureData ...
- windows server 2008 NTP授时服务[转]
转自 http://www.cnblogs.com/jingdian1956/admin/EditPosts.aspx?opt=1 服务端: 默认情况下,独立服务器WINDOWS SERVER 20 ...
- 判断浏览器及设备的打开方式,自动跳转app中
如果安装了APP则自动条状app,如果没安装则自动跳转下载页面 <head> 放在head中加载 <script> function redirect() { var appU ...
- listview 点击条目 自动置顶或者自动置底部
关于Listview点击条目,自动滑动到点击条目实现: map_searchresult_list.post(new Runnable() { @Override public void run() ...
- jquery的$().each,$.each的区别与应用
在jquery中,遍历对象和数组,经常会用到$().each和$.each(),两个方法.两个方法是有区别的,从而这两个方法在针对不同的操作上,显示了各自的特点. $().each,对于这个方法,在d ...
- 读Flask源代码学习Python--config原理
读Flask源代码学习Python--config原理 个人学习笔记,水平有限.如果理解错误的地方,请大家指出来,谢谢!第一次写文章,发现好累--!. 起因 莫名其妙在第一份工作中使用了从来没有接 ...