windows下搭建hadoop開發環境(Eclipse)

windows下搭建hadoop開發環境(Eclipse) html

注:hadoop 是java 編寫的,能夠經過java 直接編譯出 hadoop 包。所以,操做系統影響 java

不大,只是須要剝去 linux這個外殼。 
 
材料: 
1. JDK 版本"1.7.0_01" 
2. Cygwin——在線安裝 
3. Eclipse SDK版本3.3.2 
4. Hadoop版本  hadoop-0.20.2 
5. windows 7 
本教程適合以上版本,特別是 hadoop 版本和Eclipse 版本。若是換別的搭配,不必定成
功。提示:hadoop0.20.2 版本下的Eclipse 插件只適合3.3.2的eclipse ,Eclispe 版本高不
能運行。另外,hadoop 0.20.203 版本在和sygwin 搭配上有bug,不能啓動jodtracker 。 
實施: 
1.   安裝JDK 
詳細步驟略去。要注意的是:jdk 的安裝 
1.   下載cygwin 的setup.exe 進行在線安裝,setup版本:2.764 
1.1  install from Internet 
1.2  設置cygwin 最好簡短,地址中間不帶空格 
1.3  Direct Connection 
1.4  選擇down site (網易163:  mirrors.163.com) 
1.5  開始選擇須要安裝的組件 
1.5.1  Net Category  下的兩個OpenSSL(重要) 
 
1.5.2  Base Category 下的sed 
 
1.5.3  Editor Category 下的vim 
 

1.5.4  Devel Category下的subversion  node

 
1.5.5  點擊下一步開始下載並安裝,一鼓作氣,不然從新安裝。 
1.6  配置系統環境變量 
JAVA_HOME=C:\Java\jdk1.7.0_01 
CLASSPATH=.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar 
PATH=%JAVA_HOME%/bin;%JAVA_HOME%/jre/bin;C:\windows\system32\; 
1.7 BINGO 用戶中的變量 
PATH=D:\study\cygwin\bin;D:\study\cygwin\usr\bin 
2.   在cygwin 中安裝ssh 
2.1  打開cygwin 會提示用戶沒有權限的問題,按照提示敲入相應兩個命令解決,待參數
-l 
2.2  敲入命令 ssh-host-config開始配置ssh。遇到問題「should privilege separation be used? 」
回答no ,其餘的按默認,它會詢問是否註冊爲系統服務,回答是。 
2.3  在「管理」- 「服務」裏開啓 CYGWIN sshd服務。 
2.4  配置SSH 空密碼登陸 
執行如下命令: 
ssh-keygen    生成本機密鑰 
cd      ~/.ssh/ 
cp id_rsa.pub authorized_keys 
關閉cygwin ,從新打開cygwin 。嘗試ssh localhost鏈接。 
2.5可是,在 cygwin 中沒法執行命令ssh localhost,提示,Connection closed by 1.  這是權
限問題。 
解決方案:一、開端——運行——services.msc二、右鍵  CYGWIN  sshd—— 屬性——登陸
選項卡——選擇「 此帳戶」—— 瀏覽——高級——當即查找——選擇你的帳戶名(必須爲
管理員權限)——輸進密碼(必須  要有,空密碼不將不被接受)——肯定。3 、重啓
CYGWIN sshd便可。 
2.6  執行命令 ssh localhost正常 
3.   Hadoop 
入門:cygwin 中看到的windows 文件所有都在/cygwin/ 目錄下。這個目錄下的文件夾對
應這windows 系統中的c 盤,d 盤… 
所以,若是在 windows 目錄下的路徑是c:\1.txt 
那麼,在linux目錄下的路徑是/cygdrive/c/1.txt 
3.1  解壓hadoop 到D:\study\hadoop-0.20.2 
3.2  配置hadoop 
3.2.1 配置conf 下的hadoop-env.sh 
# The java implementation to use.    Required. 
export JAVA_HOME=/cygdrive/c/Java/jdk1.7.0_01 
記得去掉export 前面的註釋 
3.2.2 分別從src 下的core ,hdfs 和mapred 三個目錄下的core-default.xml ,hdfs-default.xml
和mapred-default.xml 拷貝到conf 目錄中;分別替換掉目錄下的core-site.xml,hdfs-site.xml
和mapred-site.xml 
3.2.3 編輯core-site.xml 
<name>fs.default.name</name> 
    <value>hdfs://localhost: 8888</value> 
3.2.4 編輯mapred-site.xml 
    <name>mapred.job.tracker</name> 
    <value>localhost:9999</value> 
3.3  格式化namenode,cd 到bin 目錄下 
命令: 
$ ./hadoop namenode –format 
3.4  開啓hadoop 
命令: 
./start-all.sh 
3.5  查詢hdfs 文件 
./hadoop fs –ls / 
3.6  爲了測試每一個組件是否正常工做,能夠開啓五個 cygwin 窗口依次執行 
./hadoop namenode 
./hadoop secondarynamenode 
./hadoop jobtracker 
./hadoop datanode 
./hadoop tasktracker 
若是datanode 出問題,直接進 D:\tmp\hadoop-BinGo\dfs\data把這個目錄刪了!從新啓動。 
4.   配置eclipse 插件 
在 hadoop 的 hadoop-0.20.2\contrib\eclipse-plugin 目 錄 下 找 到
hadoop-0.20.2-eclipse-plugin.jar 插件 
4.1  將插件拷貝至 eclipse 的plugins 目錄下。 
4.2  打開eclipse ,Window -> Open Perspective -> Other  選擇Map/Reduce。下方將出現: 
 
4.3  點擊右上方的 New Hadoop location...配置一下信息 
 
4.4  Window -> Preferences 選擇  「Hadoop Map/Reduce」 ,點擊「Browse...」 選擇Hadoop文
件夾的路徑。 
這個步驟與運行環境無關,只是在新建工程的時候能將 hadoop 根目錄和lib 目錄下的所
有jar 包自動導入。 
4.5  確認插件是否成功。新建,刪除操做後都要 refresh 一次目錄 
 
5.   測試hadoop環境 
5.1  新建一個 Map/Reduce項目,測試WordCount.java。注:統計文件的單詞數量 
 
WordCount.java  代碼: 
import java.io.IOException; 
import java.util.StringTokenizer; 
 
import org.apache.hadoop.conf.Configuration; 
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.Mapper; 
import org.apache.hadoop.mapreduce.Reducer; 
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; 
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; 
import org.apache.hadoop.util.GenericOptionsParser; 
 
public class  WordCount { 
 
  public static class  TokenizerMapper  
       extends Mapper<Object, Text, Text, IntWritable>{ 
     
    private final  static IntWritable  one  =  new  IntWritable(1); 
    private Text word =  new  Text(); 
       
    public void map(Object key, Text value, Context context 
                    ) throws IOException, InterruptedException { 
      StringTokenizer itr = new  StringTokenizer(value.toString()); 
      while  (itr.hasMoreTokens()) { 
        word.set(itr.nextToken()); 
        context.write(word, one ); 
      } 
    } 
  } 
   
  public static class  IntSumReducer  
       extends Reducer<Text,IntWritable,Text,IntWritable> { 
    private IntWritable  result =  new  IntWritable(); 
 
    public void reduce(Text key, Iterable<IntWritable> values,  
                       Context context 
                       )  throws IOException, InterruptedException { 
      int  sum = 0; 
      for  (IntWritable val : values) { 
        sum += val.get(); 
      } 
      result.set(sum); 
      context.write(key, result); 
    } 
  } 
 
  public static void main(String[] args) throws Exception { 
    Configuration conf = new  Configuration(); 
    String[] otherArgs = new  GenericOptionsParser(conf, 
args).getRemainingArgs(); 
    if (otherArgs.length != 2) { 
      System.err .println("Usage: wordcount <in> <out>"); 
      System.exit(2); 
    } 
    Job job =  new  Job(conf, "word count"); 
    job.setJarByClass(WordCount. class ); 
    job.setMapperClass(TokenizerMapper. class ); 
    job.setCombinerClass(IntSumReducer. class ); 
    job.setReducerClass(IntSumReducer.class ); 
    job.setOutputKeyClass(Text.class ); 
    job.setOutputValueClass(IntWritable.class ); 
    FileInputFormat. addInputPath(job, new  Path(otherArgs[0])); 
    FileOutputFormat.setOutputPath (job, new  Path(otherArgs[1])); 
    System.exit(job.waitForCompletion( true) ? 0 : 1); 
  } 

5.2  配置運行參數 
Run  As  ->  Open  Run  Dialog...  選擇WordCount 程序,在Arguments中配置運行參數:
/tmp/hello/FileToMatrix.java /tmp/hello/Exon 
5.3  運行 
Run As -> Run on Hadoop  選擇以前配置好的MapReduce 運行環境,點擊「Finish」運行 
 
6.   參考文章 
http://www.cnblogs.com/hiddenfox/archive/2011/11/26/2264461.html  
http://blog.csdn.net/yanical/article/details/4474991 
http://blog.csdn.net/hongweigg/article/details/7197662  
http://www.tech126.com/eclipse-hadoop/ 
Hadoop開發者入門專刊.pdf 
 
2/26/2012 
Written by BinGo 
相關文章
相關標籤/搜索