第一个例子wordCount
Hadoop学习全程记录——在Eclipse中运行第一个MapReduce程序
这是Hadoop学习全程记录第2篇,在这篇里我将介绍一下如何在Eclipse下写第一个MapReduce程序。
新说明一下我的开发环境:
操作系统:在windows下使用wubi安装了Ubuntu 10.10
Hadoop版本:hadoop-0.20.2.tar.gz
Eclipse版本:eclipse-jee-helios-SR1-linux-gtk.tar.gz
为了学习方便这个例子在“伪分布式模式”Hadoop安装方式下开发。
第一步,我们先启动Hadoop守护进程。
如果你读过我第1篇文章Hadoop学习全程记录——hadoop 入门应该比较清楚在“伪分布式模式”下启动Hadoop守护进程的方法,在这里就不多说了。
第二步,在Eclipse下安装Hadoop-plugin。
1.复制 Hadoop安装目录/contrib/eclipse-plugin/hadoop-0.20.2-eclipse-plugin.jar 到 eclipse安装目录/plugins/ 下。
2.重启eclipse,配置Hadoop installation directory。
如果安装插件成功,打开Window-->Preferens,你会发现Hadoop Map/Reduce选项,在这个选项里你需要配置Hadoop installation directory。配置完成后退出。
3.配置Map/Reduce Locations。
在Window-->Show View中打开Map/Reduce Locations。
在Map/Reduce Locations中新建一个Hadoop Location。在这个View中,右键-->New Hadoop Location。在弹出的对话框中你需要配置Location name,如myUbuntu,还有Map/Reduce Master和DFS Master。这里面的Host、Port分别为你在mapred-site.xml、core-site.xml中配置的地址及端口。如:
Map/Reduce Master
- localhost
- 9001
DFS Master
- localhost
- 9000
配置完后退出。点击DFS Locations-->myUbuntu如果能显示文件夹(2)说明配置正确,如果显示"拒绝连接",请检查你的配置。
第三步,新建项目。
File-->New-->Other-->Map/Reduce Project
项目名可以随便取,如Hadoop-test。
复制 Hadoop安装目录/src/example/org/apache/hadoop/example/WordCount.java到刚才新建的项目下面。
第四步,上传模拟数据文件夹。
为了运行程序,我们需要一个输入的文件夹,和输出的文件夹。输出文件夹,在程序运行完成后会自动生成。我们需要给程序一个输入文件夹。
1.在当前目录(如Hadoop安装目录)下新建文件夹input,并在文件夹下新建两个文件file01、file02,这两个文件内容分别如下:
file01
- Hello World Bye World
file02
- Hello Hadoop Goodbye Hadoop
2.将文件夹input上传到分布式文件系统中。
在已经启动Hadoop守护进程终端中cd 到hadoop安装目录,运行下面命令:
- bin/Hadoop fs -put input input01
这个命令将input文件夹上传到了Hadoop文件系统了,在该系统下就多了一个input01文件夹,你可以使用下面命令查看:
- bin/Hadoop fs -ls
第五步,运行项目。
1.在新建的项目Hadoop-test,点击WordCount.java,右键-->Run As-->Run Configurations
2.在弹出的Run Configurations对话框中,点Java Application,右键-->New,这时会新建一个application名为WordCount
3.配置运行参数,点Arguments,在Program arguments中输入“你要传给程序的输入文件夹和你要求程序将计算结果保存的文件夹”,如:
- hdfs://localhost:9000/user/panhuizhi/input01 hdfs://localhost:9000/user/panhuizhi/output01
这里面的input01就是你刚传上去文件夹。文件夹地址你可以根据自己具体情况填写。
4.点击Run,运行程序。
显示信息如下:
12/11/24 17:08:59 INFO jvm.JvmMetrics: Initializing JVM Metrics with processName=JobTracker, sessionId=
12/11/24 17:08:59 WARN mapred.JobClient: No job jar file set. User classes may not be found. See JobConf(Class) or JobConf#setJar(String).
12/11/24 17:08:59 INFO input.FileInputFormat: Total input paths to process : 2
12/11/24 17:08:59 INFO mapred.JobClient: Running job: job_local_0001
12/11/24 17:08:59 INFO input.FileInputFormat: Total input paths to process : 2
12/11/24 17:09:00 INFO mapred.MapTask: io.sort.mb = 100
12/11/24 17:09:00 INFO mapred.MapTask: data buffer = 79691776/99614720
12/11/24 17:09:00 INFO mapred.MapTask: record buffer = 262144/327680
12/11/24 17:09:00 INFO mapred.MapTask: Starting flush of map output
12/11/24 17:09:00 INFO mapred.MapTask: Finished spill 0
12/11/24 17:09:00 INFO mapred.TaskRunner: Task:attempt_local_0001_m_000000_0 is done. And is in the process of commiting
12/11/24 17:09:00 INFO mapred.LocalJobRunner:
12/11/24 17:09:00 INFO mapred.TaskRunner: Task 'attempt_local_0001_m_000000_0' done.
12/11/24 17:09:00 INFO mapred.MapTask: io.sort.mb = 100
12/11/24 17:09:00 INFO mapred.MapTask: data buffer = 79691776/99614720
12/11/24 17:09:00 INFO mapred.MapTask: record buffer = 262144/327680
12/11/24 17:09:00 INFO mapred.MapTask: Starting flush of map output
12/11/24 17:09:00 INFO mapred.MapTask: Finished spill 0
12/11/24 17:09:00 INFO mapred.TaskRunner: Task:attempt_local_0001_m_000001_0 is done. And is in the process of commiting
12/11/24 17:09:00 INFO mapred.LocalJobRunner:
12/11/24 17:09:00 INFO mapred.TaskRunner: Task 'attempt_local_0001_m_000001_0' done.
12/11/24 17:09:00 INFO mapred.LocalJobRunner:
12/11/24 17:09:00 INFO mapred.Merger: Merging 2 sorted segments
12/11/24 17:09:00 INFO mapred.Merger: Down to the last merge-pass, with 2 segments left of total size: 77 bytes
12/11/24 17:09:00 INFO mapred.LocalJobRunner:
12/11/24 17:09:00 INFO mapred.TaskRunner: Task:attempt_local_0001_r_000000_0 is done. And is in the process of commiting
12/11/24 17:09:00 INFO mapred.LocalJobRunner:
12/11/24 17:09:00 INFO mapred.TaskRunner: Task attempt_local_0001_r_000000_0 is allowed to commit now
12/11/24 17:09:00 INFO output.FileOutputCommitter: Saved output of task 'attempt_local_0001_r_000000_0' to hdfs://localhost:9000/user/tgrqap6qhvnoh4d/administrator/output01
12/11/24 17:09:00 INFO mapred.LocalJobRunner: reduce > reduce
12/11/24 17:09:00 INFO mapred.TaskRunner: Task 'attempt_local_0001_r_000000_0' done.
12/11/24 17:09:01 INFO mapred.JobClient: map 100% reduce 100%
12/11/24 17:09:01 INFO mapred.JobClient: Job complete: job_local_0001
12/11/24 17:09:01 INFO mapred.JobClient: Counters: 14
12/11/24 17:09:01 INFO mapred.JobClient: FileSystemCounters
12/11/24 17:09:01 INFO mapred.JobClient: FILE_BYTES_READ=51328
12/11/24 17:09:01 INFO mapred.JobClient: HDFS_BYTES_READ=139
12/11/24 17:09:01 INFO mapred.JobClient: FILE_BYTES_WRITTEN=104570
12/11/24 17:09:01 INFO mapred.JobClient: HDFS_BYTES_WRITTEN=41
12/11/24 17:09:01 INFO mapred.JobClient: Map-Reduce Framework
12/11/24 17:09:01 INFO mapred.JobClient: Reduce input groups=5
12/11/24 17:09:01 INFO mapred.JobClient: Combine output records=6
12/11/24 17:09:01 INFO mapred.JobClient: Map input records=4
12/11/24 17:09:01 INFO mapred.JobClient: Reduce shuffle bytes=0
12/11/24 17:09:01 INFO mapred.JobClient: Reduce output records=5
12/11/24 17:09:01 INFO mapred.JobClient: Spilled Records=12
12/11/24 17:09:01 INFO mapred.JobClient: Map output bytes=82
12/11/24 17:09:01 INFO mapred.JobClient: Combine input records=8
12/11/24 17:09:01 INFO mapred.JobClient: Map output records=8
12/11/24 17:09:01 INFO mapred.JobClient: Reduce input records=6
点击Run,运行程序,过段时间将运行完成,等运行结束后,可以在终端中用命令:
查看是否生成文件夹output01。
用下面命令查看生成的文件内容:
如果显示如下,恭喜你一切顺利,你已经成功在eclipse下运行第一个MapReduce程序了。
12/11/24 17:08:59 WARN mapred.JobClient: No job jar file set. User classes may not be found. See JobConf(Class) or JobConf#setJar(String).
12/11/24 17:08:59 INFO input.FileInputFormat: Total input paths to process : 2
12/11/24 17:08:59 INFO mapred.JobClient: Running job: job_local_0001
12/11/24 17:08:59 INFO input.FileInputFormat: Total input paths to process : 2
12/11/24 17:09:00 INFO mapred.MapTask: io.sort.mb = 100
12/11/24 17:09:00 INFO mapred.MapTask: data buffer = 79691776/99614720
12/11/24 17:09:00 INFO mapred.MapTask: record buffer = 262144/327680
12/11/24 17:09:00 INFO mapred.MapTask: Starting flush of map output
12/11/24 17:09:00 INFO mapred.MapTask: Finished spill 0
12/11/24 17:09:00 INFO mapred.TaskRunner: Task:attempt_local_0001_m_000000_0 is done. And is in the process of commiting
12/11/24 17:09:00 INFO mapred.LocalJobRunner:
12/11/24 17:09:00 INFO mapred.TaskRunner: Task 'attempt_local_0001_m_000000_0' done.
12/11/24 17:09:00 INFO mapred.MapTask: io.sort.mb = 100
12/11/24 17:09:00 INFO mapred.MapTask: data buffer = 79691776/99614720
12/11/24 17:09:00 INFO mapred.MapTask: record buffer = 262144/327680
12/11/24 17:09:00 INFO mapred.MapTask: Starting flush of map output
12/11/24 17:09:00 INFO mapred.MapTask: Finished spill 0
12/11/24 17:09:00 INFO mapred.TaskRunner: Task:attempt_local_0001_m_000001_0 is done. And is in the process of commiting
12/11/24 17:09:00 INFO mapred.LocalJobRunner:
12/11/24 17:09:00 INFO mapred.TaskRunner: Task 'attempt_local_0001_m_000001_0' done.
12/11/24 17:09:00 INFO mapred.LocalJobRunner:
12/11/24 17:09:00 INFO mapred.Merger: Merging 2 sorted segments
12/11/24 17:09:00 INFO mapred.Merger: Down to the last merge-pass, with 2 segments left of total size: 77 bytes
12/11/24 17:09:00 INFO mapred.LocalJobRunner:
12/11/24 17:09:00 INFO mapred.TaskRunner: Task:attempt_local_0001_r_000000_0 is done. And is in the process of commiting
12/11/24 17:09:00 INFO mapred.LocalJobRunner:
12/11/24 17:09:00 INFO mapred.TaskRunner: Task attempt_local_0001_r_000000_0 is allowed to commit now
12/11/24 17:09:00 INFO output.FileOutputCommitter: Saved output of task 'attempt_local_0001_r_000000_0' to hdfs://localhost:9000/user/tgrqap6qhvnoh4d/administrator/output01
12/11/24 17:09:00 INFO mapred.LocalJobRunner: reduce > reduce
12/11/24 17:09:00 INFO mapred.TaskRunner: Task 'attempt_local_0001_r_000000_0' done.
12/11/24 17:09:01 INFO mapred.JobClient: map 100% reduce 100%
12/11/24 17:09:01 INFO mapred.JobClient: Job complete: job_local_0001
12/11/24 17:09:01 INFO mapred.JobClient: Counters: 14
12/11/24 17:09:01 INFO mapred.JobClient: FileSystemCounters
12/11/24 17:09:01 INFO mapred.JobClient: FILE_BYTES_READ=51328
12/11/24 17:09:01 INFO mapred.JobClient: HDFS_BYTES_READ=139
12/11/24 17:09:01 INFO mapred.JobClient: FILE_BYTES_WRITTEN=104570
12/11/24 17:09:01 INFO mapred.JobClient: HDFS_BYTES_WRITTEN=41
12/11/24 17:09:01 INFO mapred.JobClient: Map-Reduce Framework
12/11/24 17:09:01 INFO mapred.JobClient: Reduce input groups=5
12/11/24 17:09:01 INFO mapred.JobClient: Combine output records=6
12/11/24 17:09:01 INFO mapred.JobClient: Map input records=4
12/11/24 17:09:01 INFO mapred.JobClient: Reduce shuffle bytes=0
12/11/24 17:09:01 INFO mapred.JobClient: Reduce output records=5
12/11/24 17:09:01 INFO mapred.JobClient: Spilled Records=12
12/11/24 17:09:01 INFO mapred.JobClient: Map output bytes=82
12/11/24 17:09:01 INFO mapred.JobClient: Combine input records=8
12/11/24 17:09:01 INFO mapred.JobClient: Map output records=8
12/11/24 17:09:01 INFO mapred.JobClient: Reduce input records=6
点击Run,运行程序,过段时间将运行完成,等运行结束后,可以在终端中用命令:
- bin/Hadoop fs -ls
查看是否生成文件夹output01。
用下面命令查看生成的文件内容:
- bin/Hadoop fs -cat output01/*
如果显示如下,恭喜你一切顺利,你已经成功在eclipse下运行第一个MapReduce程序了。
- Bye 1
- Goodbye 1
- Hadoop 2
- Hello 2
- World 2
cygwin下命令及结构图示: