第一个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: ...
随机推荐
- Velocity引擎导致jvm内存外内存泄露
我公司一兄弟,在controller层,每次调用controller的时候都创建了velocity引擎,而且没有去关闭,最终导致的现象就是jvm的内存信息正常,但是jvm之外的内存发生了泄露,导致是用 ...
- html系列教程--article audio
<article> 标签 <article> 标签规定独立的自包含内容.一篇文章应有其自身的意义,应该有可能独立于站点的其余部分对其进行分发. <article> ...
- Android 根据EditText搜索框ListView动态显示数据
根据EditText搜索框ListView动态显示数据是根据需求来的,觉得这之中涉及的东西可能比较的有意思,所以动手来写一写,希望对大家有点帮助. 首先,我们来分析下整个过程: 1.建立一个layou ...
- CRM odata方法 js容易出现的错误,大小写区分 Value Id
Id Value 注意大小写,I大写,V大写,typeResults.result[0].yt_category.Value; 否则会报 错,Result.yt_businessunit_terri ...
- [Android]Plug-in com.android.ide.eclipse.adt was unable to load class com.android.ide
今天启动eclipse的时候报了上述错误,打开xml是都报错.其实解决方法很简单:重启eclipse即可.
- HTTP头信息(转)--2
HTTP 头部解释 ========================================================================================== ...
- 指针和const
将指针参数声明为指向常量数据的指针有两条理由: 这样可以避免由于无意间修改数据而导致的编译错误. 使用const使得函数能够处理const和非const实参,否则将只能接收非const数据. ...
- leetcode LRU Cache python
class Node(object): def __init__(self,k,x): self.key=k self.val=x self.prev=None self.next=None clas ...
- Linux学习之traceroute命令
通过traceroute我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径.当然每次数据包由某一同样的出发点(source)到达某一同样的目的地(destination)走的路径可能会不一 ...
- spring 源码之IOC 类图
Spring IoC容器是spring框架的核心和基础,IoC容器负责spring Bean的生命周期,是spring框架实现其他扩展功能的基础.容器的继承结构比较复杂,这里画出了spring IoC ...