开发环境:Mac OS(Unix)
Hadoop版本:1.03
Eclipse版本: 3.6.0
第一步:下载Hadoop
下载地址:http://hadoop.apache.org/common/releases.html#Download
第二步:配置Hadoop
将下载的Hadoop压缩文件解压缩,找到conf目录,打开core-site.xml,修改代码如下所示:
- <?xml version="1.0"?>
- <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
- <!-- Put site-specific property overrides in this file. -->
- <configuration>
- <property>
- <name>fs.default.name</name>
- <value>hdfs://localhost:8020</value>
- </property>
- </configuration>
找到mapred-site.xml修改代码如下所示:
- <?xml version="1.0"?>
- <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
- <!-- Put site-specific property overrides in this file. -->
- <configuration>
- <property>
- <name>mapred.job.tracker</name>
- <value>localhost:9001</value>
- </property>
- </configuration>
找到hdfs-site.xml修改代码如下所示:
- <?xml version="1.0"?>
- <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
- <!-- Put site-specific property overrides in this file. -->
- <configuration>
- <property>
- <name>dfs.replication</name>
- <value>1</value>
- </property>
- </configuration>
找到hadoop-env.sh打开,加入以下配置
引用
export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home
export HADOOP_INSTALL=/Users/alex/Documents/DevRes/hadoop-0.21.0
export PATH=$PATH:$HADOOP_INSTALL/bin
其中,具体的目录根据你的实际情况配置。
第二步:配置SSH
windows版本可安装openssh,本章主要介绍Mac OS,打开“系统偏好设置”,找到共享,勾选远程登录,如下图所示:

第三步:运行Hadoop
打开终端,定位到Hadoop目录输入以下命令:
bin/hadoop namenode -format
此时终端输出:
引用
DEPRECATED: Use of this script to execute hdfs command is deprecated.
Instead use the hdfs command for it.
11/09/04 21:19:38 INFO namenode.NameNode: STARTUP_MSG:
/************************************************************
STARTUP_MSG: Starting NameNode
STARTUP_MSG: host = localhost/127.0.0.1
STARTUP_MSG: args = [-format]
STARTUP_MSG: version = 0.21.0
STARTUP_MSG: classpath = /Users/alex/documents/devres/hadoop-0.21.0/bin/../conf:/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/lib/tools.jar:/Users/alex/documents/devres/hadoop-0.21.0/bin/..:/Users/alex/documents/devres/hadoop-0.21.0/bin/../hadoop-common-0.21.0.ja
.........省略若干
STARTUP_MSG: build = https://svn.apache.org/repos/asf/hadoop/common/branches/branch-0.21 -r 985326; compiled by 'tomwhite' on Tue Aug 17 01:02:28 EDT 2010
************************************************************/
11/09/04 21:19:39 INFO namenode.FSNamesystem: defaultReplication = 1
11/09/04 21:19:39 INFO namenode.FSNamesystem: maxReplication = 512
11/09/04 21:19:39 INFO namenode.FSNamesystem: minReplication = 1
11/09/04 21:19:39 INFO namenode.FSNamesystem: maxReplicationStreams = 2
11/09/04 21:19:39 INFO namenode.FSNamesystem: shouldCheckForEnoughRacks = false
11/09/04 21:19:39 INFO security.Groups: Group mapping impl=org.apache.hadoop.security.ShellBasedUnixGroupsMapping; cacheTimeout=300000
11/09/04 21:19:39 INFO namenode.FSNamesystem: fsOwner=alex
11/09/04 21:19:39 INFO namenode.FSNamesystem: supergroup=supergroup
11/09/04 21:19:39 INFO namenode.FSNamesystem: isPermissionEnabled=true
11/09/04 21:19:39 INFO namenode.FSNamesystem: isAccessTokenEnabled=false accessKeyUpdateInterval=0 min(s), accessTokenLifetime=0 min(s)
11/09/04 21:19:39 INFO common.Storage: Image file of size 110 saved in 0 seconds.
11/09/04 21:19:39 INFO common.Storage: Storage directory /tmp/hadoop-alex/dfs/name has been successfully formatted.
11/09/04 21:19:39 INFO namenode.NameNode: SHUTDOWN_MSG:
/************************************************************
SHUTDOWN_MSG: Shutting down NameNode at localhost/127.0.0.1
************************************************************/
然后输入bin/start-all.sh
在启动期间会多次要求输入密码(你机器的登录密码)
此时终端输出:
引用
This script is Deprecated. Instead use start-dfs.sh and start-mapred.sh
starting namenode, logging to /Users/alex/documents/devres/hadoop-0.21.0/bin/../logs/hadoop-alex-namenode-localhost.out
Password:
localhost: starting datanode, logging to /Users/alex/documents/devres/hadoop-0.21.0/bin/../logs/hadoop-alex-datanode-localhost.out
Password:
localhost: starting secondarynamenode, logging to /Users/alex/documents/devres/hadoop-0.21.0/bin/../logs/hadoop-alex-secondarynamenode-localhost.out
starting jobtracker, logging to /Users/alex/documents/devres/hadoop-0.21.0/bin/../logs/hadoop-alex-jobtracker-localhost.out
Password:
localhost: starting tasktracker, logging to /Users/alex/documents/devres/hadoop-0.21.0/bin/../logs/hadoop-alex-tasktracker-localhost.out
第四步:检查启动
打开连接地址http://localhost:50070
如果找不到页面,请查看log日志(在hadoop目录中的log文件夹下),否则应当会看到以下页面:

然后再打开地址http://localhost:50030/
应当能看到以下页面(如果看不到,则证明你的jobtracker启动失败,请查看log):

至此,Hadoop配置成功。
第五步:测试
将一个文件拷贝到HDFS中,打开终端定位到Hadoop目录,输入以下命令:
bin/hadoop fs -copyFromLocal [源文件] [目标地址]
比如我输入的是:
bin/hadoop fs -copyFromLocal /Users/alex/desktop/persons.rtf hdfs://localhost/tmp/hadoop-alex
此时终端会输出以下文字:
引用
11/09/04 21:23:45 INFO security.Groups: Group mapping impl=org.apache.hadoop.security.ShellBasedUnixGroupsMapping; cacheTimeout=300000
11/09/04 21:23:45 WARN conf.Configuration: mapred.task.id is deprecated. Instead, use mapreduce.task.attempt.id
这样代表成功。
第六步:配置Eclipse
首先找到目录hadoop-0.21.0/mapred/contrib/eclipse-plugin,然后将里面的eclipse-plugin复制到你的Eclipse的plugin文件夹中。
注意,如果该插件无法使用,本文下方有提供。如果还不能使用,请尝试在网络上寻找。
此时打开Eclipse,在Open Perspective中选择其它,找到Map/Reduce视图,打开它。
此时你会发现在Project Explorer和下面的视图中多了一个DFS Locations,右键新建Location如下图所示:

此时会打开一个窗口,对其进行配置,端口号就是我们刚才在core-site.xml中配置的端口好,既9000,如下图所示:

点击确定后即可在左边的Project Explorer面板中对DFS Location进行展开,如下图所示:

第六步:在Eclipse中写代码
在Map/Reduce视图中新建一个Map/Reduce项目(该项目与普通Java项目没什么区别),新建一个Java类,该类主要用于读取我们前面复制到HDFS中的那个文件内容:
- package cn.com.fri;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.net.URISyntaxException;
- import org.apache.hadoop.fs.FSDataInputStream;
- import org.apache.hadoop.fs.FileContext;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.IOUtils;
- import org.apache.hadoop.security.AccessControlException;
- public class Test {
- public static void main(String[] args) throws AccessControlException,
- FileNotFoundException, IOException, URISyntaxException {
- FileContext fc = FileContext
- .getFileContext();//如果运行在hadoop location中,不需要配置URI,否则需要给一个URI
- FSDataInputStream fsInput = fc.open(new Path(
- "/tmp/hadoop-alex/persons.rtf"));
- IOUtils.copyBytes(fsInput, System.out, 4090, false);
- fsInput.seek(0);
- IOUtils.copyBytes(fsInput, System.out, 4090, false);
- }
- }
然后右键->Run As->Run on hadoop,此时会让你选择一个location,就选中我们刚才新建的location即可。
因为我用了seek并给了一个位置,所以会打印两遍结果如下:
引用
11/09/04 22:32:33 WARN conf.Configuration: DEPRECATED: hadoop-site.xml found in the classpath. Usage of hadoop-site.xml is deprecated. Instead use core-site.xml, mapred-site.xml and hdfs-site.xml to override properties of core-default.xml, mapred-default.xml and hdfs-default.xml respectively
11/09/04 22:32:33 WARN conf.Configuration: fs.default.name is deprecated. Instead, use fs.defaultFS
11/09/04 22:32:33 INFO security.Groups: Group mapping impl=org.apache.hadoop.security.ShellBasedUnixGroupsMapping; cacheTimeout=300000
11/09/04 22:32:33 WARN conf.Configuration: mapred.task.id is deprecated. Instead, use mapreduce.task.attempt.id
{rtf1ansiansicpg936cocoartf1038cocoasubrtf360
{fonttblf0fnilfcharset134 STHeitiSC-Light;}
{colortbl;red255green255blue255;}
{*listtable{listlisttemplateid1listhybrid{listlevellevelnfc0levelnfcn0leveljc0leveljcn0levelfollow0levelstartat1levelspace360levelindent0{*levelmarker {decimal}.}{leveltextleveltemplateid1'02'00.;}{levelnumbers'01;}fi-360li720lin720 }{listname ;}listid1}}
{*listoverridetable{listoverridelistid1listoverridecount0ls1}}
paperw11900paperh16840margl1440margr1440vieww11680viewh11900viewkind0
pardtx220tx720tx1133tx1700tx2267tx2834tx3401tx3968tx4535tx5102tx5669tx6236tx6803li720fi-720qlqnaturalpardirnatural
ls1ilvl0
f0fs24 cf0 {listtext 1. }'d0'ec'd6'f9'a3'ac
{listtext 2. }'c0'ee'cf'fe'c0'f2'a3'ac
{listtext 3. }'d5'd4'd6'f9'a3'ac
{listtext 4. }'d6'ec'b9'e3'b7'ef'a3'ac
{listtext 5. }'d0'bb'c0'e8'c0'e8'a3'ac
{listtext 6. }'d6'ec'ce'b0'a3'ac
{listtext 7. }'d5'c5'b5'a4'b5'a4'a3'ac
{listtext 8. }'c0'ee'b5'a4'a3'ac
{listtext 9. }'b2'b7'ce'c4'c4'c8'a3'ac
{listtext 10. }'d6'dc'ca'ab'a3'ac
{listtext 11. }'d5'd4'cd'ee'b6'ab'a3'ac
{listtext 12. }'b9'f9'b7'ad'b5'dc'a3'ac
{listtext 13. }'cc'c6'be'fc'b2'a8'a3'ac
{listtext 14. }'b8'df'cf'e8'a3'ac
{listtext 15. }'d1'a6'c3'cd'a3'ac
{listtext 16. }'ba'fa'b3'a4'ba'd8'a3'ac
{listtext 17. }'c0'ee'c7'bf'a3'ac
{listtext 18. }'c0'ee'bb'd4'a3'ac
{listtext 19. }'c8'ce'c5'f4'a3'ac
{listtext 20. }'d7'db'bc'd2'c7'ed'a3'ac
{listtext 21. }'b6'a1'd1'de'a3'ac
{listtext 22. }'b6'ad'c1'c1'a3'ac
{listtext 23. }'c3'ab'ba'ec'a3'ac
{listtext 24. }'c5'ed'c7'e5'd4'b4'a3'ac
{listtext 25. }'c3'cf'c7'ec'bf'c2'a3'ac
{listtext 26. }'c0'ee'b7'e5'a3'ac
{listtext 27. }'c2'ed'c1'd6'a3'ac
{listtext 28. }'b3'c9'b3'cc'a3'ac
{listtext 29. }'b7'b6'c9'dc'b4'a8'a3'ac
{listtext 30. }'c4'aa'b7'bc'a3'ac
{listtext 31. }'b6'a1'cc'ce'a3'ac
{listtext 32. }'b6'c5'd3'b0'a3'ac
{listtext 33. }'c0'ee'b7'ef'a3'ac
{listtext 34. }'ba'ab'c3'f7'c3'f7'a3'ac
{listtext 35. }'c9'f2'cf'fe'c0'f6'a3'ac
{listtext 36. }'d0'ed'b3'c9'a3'ac
{listtext 37. }'d5'd4'd5'f1'a3'ac
{listtext 38. }'c2'de'be'b2'a3'ac
{listtext 39. }'b6'c5'd3'b0'a3'ac
{listtext 40. }'d6'ec'b7'ef'a3'ac
{listtext 41. }'cb'e5'c0'f6'a3'ac
{listtext 42. }'ba'fa'd1'f4'd1'f4'a3'a8'ba'fa'd5'f1'a3'a9'a3'ac
{listtext 43. }'c2'ed'cf'fe'c3'b7
{listtext 44. }'b5'cb'b1'f3'a3'ac
{listtext 45. }'cf'ee'c3'ce'e9'aa'a3'ac
{listtext 46. }'d1'ee'c1'f8'a3'ac
{listtext 47. }'b8'f0'ea'bb
}{rtf1ansiansicpg936cocoartf1038cocoasubrtf360
{fonttblf0fnilfcharset134 STHeitiSC-Light;}
{colortbl;red255green255blue255;}
{*listtable{listlisttemplateid1listhybrid{listlevellevelnfc0levelnfcn0leveljc0leveljcn0levelfollow0levelstartat1levelspace360levelindent0{*levelmarker {decimal}.}{leveltextleveltemplateid1'02'00.;}{levelnumbers'01;}fi-360li720lin720 }{listname ;}listid1}}
{*listoverridetable{listoverridelistid1listoverridecount0ls1}}
paperw11900paperh16840margl1440margr1440vieww11680viewh11900viewkind0
pardtx220tx720tx1133tx1700tx2267tx2834tx3401tx3968tx4535tx5102tx5669tx6236tx6803li720fi-720qlqnaturalpardirnatural
ls1ilvl0
f0fs24 cf0 {listtext 1. }'d0'ec'd6'f9'a3'ac
{listtext 2. }'c0'ee'cf'fe'c0'f2'a3'ac
{listtext 3. }'d5'd4'd6'f9'a3'ac
{listtext 4. }'d6'ec'b9'e3'b7'ef'a3'ac
{listtext 5. }'d0'bb'c0'e8'c0'e8'a3'ac
{listtext 6. }'d6'ec'ce'b0'a3'ac
{listtext 7. }'d5'c5'b5'a4'b5'a4'a3'ac
{listtext 8. }'c0'ee'b5'a4'a3'ac
{listtext 9. }'b2'b7'ce'c4'c4'c8'a3'ac
{listtext 10. }'d6'dc'ca'ab'a3'ac
{listtext 11. }'d5'd4'cd'ee'b6'ab'a3'ac
{listtext 12. }'b9'f9'b7'ad'b5'dc'a3'ac
{listtext 13. }'cc'c6'be'fc'b2'a8'a3'ac
{listtext 14. }'b8'df'cf'e8'a3'ac
{listtext 15. }'d1'a6'c3'cd'a3'ac
{listtext 16. }'ba'fa'b3'a4'ba'd8'a3'ac
{listtext 17. }'c0'ee'c7'bf'a3'ac
{listtext 18. }'c0'ee'bb'd4'a3'ac
{listtext 19. }'c8'ce'c5'f4'a3'ac
{listtext 20. }'d7'db'bc'd2'c7'ed'a3'ac
{listtext 21. }'b6'a1'd1'de'a3'ac
{listtext 22. }'b6'ad'c1'c1'a3'ac
{listtext 23. }'c3'ab'ba'ec'a3'ac
{listtext 24. }'c5'ed'c7'e5'd4'b4'a3'ac
{listtext 25. }'c3'cf'c7'ec'bf'c2'a3'ac
{listtext 26. }'c0'ee'b7'e5'a3'ac
{listtext 27. }'c2'ed'c1'd6'a3'ac
{listtext 28. }'b3'c9'b3'cc'a3'ac
{listtext 29. }'b7'b6'c9'dc'b4'a8'a3'ac
{listtext 30. }'c4'aa'b7'bc'a3'ac
{listtext 31. }'b6'a1'cc'ce'a3'ac
{listtext 32. }'b6'c5'd3'b0'a3'ac
{listtext 33. }'c0'ee'b7'ef'a3'ac
{listtext 34. }'ba'ab'c3'f7'c3'f7'a3'ac
{listtext 35. }'c9'f2'cf'fe'c0'f6'a3'ac
{listtext 36. }'d0'ed'b3'c9'a3'ac
{listtext 37. }'d5'd4'd5'f1'a3'ac
{listtext 38. }'c2'de'be'b2'a3'ac
{listtext 39. }'b6'c5'd3'b0'a3'ac
{listtext 40. }'d6'ec'b7'ef'a3'ac
{listtext 41. }'cb'e5'c0'f6'a3'ac
{listtext 42. }'ba'fa'd1'f4'd1'f4'a3'a8'ba'fa'd5'f1'a3'a9'a3'ac
{listtext 43. }'c2'ed'cf'fe'c3'b7
{listtext 44. }'b5'cb'b1'f3'a3'ac
{listtext 45. }'cf'ee'c3'ce'e9'aa'a3'ac
{listtext 46. }'d1'ee'c1'f8'a3'ac
{listtext 47. }'b8'f0'ea'bb
}
rtf中存放的是中文。