[[PageOutline]] {{{ #!html
hadoop 0.20 程式開發
eclipse plugin + Makefile
}}} = 零. 前言 = * 開發hadoop 需要用到許多的物件導向語法,包括繼承關係、介面類別,而且需要匯入正確的classpath,否則寫hadoop程式只是打字練習... * 用類 vim 來處理這種複雜的程式,有可能會變成一場惡夢,因此用eclipse開發,搭配mapreduce-plugin會事半功倍。 * 若繼承練習一的系統,可以直接跳到'''二、 建立專案 ''' 開始 == 0.1 環境說明 == * ubuntu 8.10 * sun-java-6 * eclipse 3.3.2 * hadoop 0.20.2 == 0.2 目錄說明 == * 使用者:waue * 使用者家目錄: /home/hadooper * 專案目錄 : /home/hadooper/workspace * hadoop目錄: /opt/hadoop = 一、安裝 = 安裝的部份沒必要都一模一樣,僅提供參考,反正只要安裝好java , hadoop , eclipse,並清楚自己的路徑就可以了 == 1.1. 安裝java == 首先安裝java 基本套件 {{{ $ sudo apt-get install java-common sun-java6-bin sun-java6-jdk sun-java6-jre }}} == 1.1.1. 安裝sun-java6-doc == 1 將javadoc (jdk-6u10-docs.zip) 下載下來 [https://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/ViewProductDetail-Start?ProductRef=jdk-6u10-docs-oth-JPR@CDS-CDS_Developer 下載點] [[Image(wiki:Hadoop_Lab5:1-1.png)]] 2 下載完後將檔案放在 /tmp/ 下 3 執行 {{{ $ sudo apt-get install sun-java6-doc }}} == 1.2. ssh 安裝設定 == {{{ $ apt-get install ssh $ ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa $ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys $ ssh localhost }}} 執行ssh localhost 沒有出現詢問密碼的訊息則無誤 == 1.3. 安裝hadoop == 安裝hadoop0.20到/opt/並取目錄名為hadoop {{{ $ cd ~ $ wget http://apache.ntu.edu.tw/hadoop/core/hadoop-0.20.2/hadoop-0.20.2.tar.gz $ tar zxvf hadoop-0.20.2.tar.gz $ sudo mv hadoop-0.20.2 /opt/ $ sudo chown -R waue:waue /opt/hadoop-0.20.2 $ sudo ln -sf /opt/hadoop-0.20.2 /opt/hadoop }}} * 編輯 /opt/hadoop/conf/hadoop-env.sh {{{ #!sh export JAVA_HOME=/usr/lib/jvm/java-6-sun export HADOOP_HOME=/opt/hadoop export PATH=$PATH:/opt/hadoop/bin }}} * 編輯 /opt/hadoop/conf/core-site.xml {{{ #!sh fs.default.name hdfs://localhost:9000 hadoop.tmp.dir /tmp/hadoop/hadoop-${user.name} }}} * 編輯 /opt/hadoop/conf/hdfs-site.xml {{{ #!sh dfs.replication 1 }}} * 編輯 /opt/hadoop/conf/mapred-site.xml {{{ #!sh mapred.job.tracker localhost:9001 }}} * 啟動 {{{ $ cd /opt/hadoop $ source /opt/hadoop/conf/hadoop-env.sh $ hadoop namenode -format $ start-all.sh $ hadoop fs -put conf input $ hadoop fs -ls }}} * 沒有錯誤訊息則代表無誤 == 1.4. 安裝eclipse == * 在此提供兩個方法來下載檔案 * 方法一:下載 [http://www.eclipse.org/downloads/download.php?file=/eclipse/downloads/drops/R-3.4.2-200902111700/eclipse-SDK-3.4.2-linux-gtk.tar.gz eclipse SDK 3.4.2 Classic],並且放這檔案到家目錄 * 方法二:貼上指令 {{{ $ cd ~ $ wget http://ftp.cs.pu.edu.tw/pub/eclipse/eclipse/downloads/drops/R-3.4.2-200902111700/eclipse-SDK-3.4.2-linux-gtk.tar.gz }}} * eclipse 檔已下載到家目錄後,執行下面指令: {{{ $ cd ~ $ tar -zxvf eclipse-SDK-3.4.2-linux-gtk.tar.gz $ sudo mv eclipse /opt $ sudo ln -sf /opt/eclipse/eclipse /usr/local/bin/ }}} = 二、 建立專案 = == 2.1 安裝hadoop 的 eclipse plugin == * 匯入hadoop 0.20.2 eclipse plugin {{{ $ cd /opt/hadoop $ sudo cp /opt/hadoop/contrib/eclipse-plugin/hadoop-0.20.2-eclipse-plugin.jar /opt/eclipse/plugins }}} {{{ $ sudo vim /opt/eclipse/eclipse.ini }}} * 可斟酌參考eclipse.ini內容(非必要) {{{ #!sh -startup plugins/org.eclipse.equinox.launcher_1.0.101.R34x_v20081125.jar --launcher.library plugins/org.eclipse.equinox.launcher.gtk.linux.x86_1.0.101.R34x_v20080805 -showsplash org.eclipse.platform --launcher.XXMaxPermSize 512m -vmargs -Xms40m -Xmx512m }}} == 2.2 開啟eclipse == * 打開eclipse {{{ $ eclipse & }}} 一開始會出現問你要將工作目錄放在哪裡:在這我們用預設值 [[Image(wiki:Hadoop_Lab5:2-1.png)]] ------- '''PS: 之後的說明則是在eclipse 上的介面操作''' ------- == 2.3 選擇視野 == || window -> || open pers.. -> || other.. -> || map/reduce|| [[Image(wiki:Hadoop_Lab5:win-open-other.png)]] ------- 設定要用 Map/Reduce 的視野 [[Image(wiki:Hadoop_Lab5:2-2.png)]] --------- 使用 Map/Reduce 的視野後的介面呈現 [[Image(wiki:Hadoop_Lab5:2-3.png)]] -------- == 2.4 建立專案 == || file -> || new -> || project -> || Map/Reduce -> || Map/Reduce Project -> || next || [[Image(wiki:Hadoop_Lab5:file-new-project.png)]] -------- 建立mapreduce專案(1) [[Image(wiki:Hadoop_Lab5:2-4.png)]] ----------- 建立mapreduce專案的(2) {{{ #!sh project name-> 輸入 : icas (隨意) use default hadoop -> Configur Hadoop install... -> 輸入: "/opt/hadoop" -> ok Finish }}} [[Image(wiki:Hadoop_Lab5:2-4-2.png)]] -------------- == 2.5 設定專案 == 由於剛剛建立了icas這個專案,因此eclipse已經建立了新的專案,出現在左邊視窗,右鍵點選該資料夾,並選properties -------------- Step1. 右鍵點選project的properties做細部設定 [[Image(wiki:Hadoop_Lab5:2-5.png)]] ---------- Step2. 進入專案的細部設定頁 hadoop的javadoc的設定(1) [[Image(wiki:Hadoop_Lab5:2-5-1.png)]] * java Build Path -> Libraries -> hadoop-0.20.2-ant.jar * java Build Path -> Libraries -> hadoop-0.20.2-core.jar * java Build Path -> Libraries -> hadoop-0.20.2-tools.jar * 以 hadoop-0.20.2-core.jar 的設定內容如下,其他依此類推 {{{ #!sh source ...-> 輸入:/opt/hadoop/src/core javadoc ...-> 輸入:file:/opt/hadoop/docs/api/ }}} ------------ Step3. hadoop的javadoc的設定完後(2) [[Image(wiki:Hadoop_Lab5:2-5-2.png)]] ------------ Step4. java本身的javadoc的設定(3) * javadoc location -> 輸入:file:/usr/lib/jvm/java-6-sun/docs/api/ [[Image(wiki:Hadoop_Lab5:2-5-3.png)]] ----- 設定完後回到eclipse 主視窗 == 2.6 連接hadoop server == -------- Step1. 視窗右下角黃色大象圖示"Map/Reduce Locations tag" -> 點選齒輪右邊的藍色大象圖示: [[Image(wiki:Hadoop_Lab5:2-6.png)]] ------------- Step2. 進行eclipse 與 hadoop 間的設定(2) [[Image(wiki:Hadoop_Lab5:2-6-1.png)]] {{{ #!sh Location Name -> 輸入:hadoop (隨意) Map/Reduce Master -> Host-> 輸入:localhost Map/Reduce Master -> Port-> 輸入:9001 DFS Master -> Host-> 輸入:9000 Finish }}} ---------------- 設定完後,可以看到下方多了一隻藍色大象,左方展開資料夾也可以秀出在hdfs內的檔案結構 [[Image(wiki:Hadoop_Lab5:2-6-2.png)]] ------------- = 三、 撰寫範例程式 = * 之前在eclipse上已經開了個專案icas,因此這個目錄在: * /home/hadooper/workspace/icas * 在這個目錄內有兩個資料夾: * src : 用來裝程式原始碼 * bin : 用來裝編譯後的class檔 * 如此一來原始碼和編譯檔就不會混在一起,對之後產生jar檔會很有幫助 * 在這我們編輯一個範例程式 : WordCount == 3.1 mapper.java == 1. new || File -> || new -> || mapper || [[Image(wiki:Hadoop_Lab5:file-new-mapper.png)]] ----------- 2. create [[Image(wiki:Hadoop_Lab5:3-1.png)]] {{{ #!sh source folder-> 輸入: icas/src Package : Sample Name -> : mapper }}} ---------- 3. modify {{{ #!java package Sample; import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; public class mapper extends Mapper { 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); } } } }}} 建立mapper.java後,貼入程式碼 [[Image(wiki:Hadoop_Lab5:3-2.png)]] ------------ == 3.2 reducer.java == 1. new * File -> new -> reducer [[Image(wiki:Hadoop_Lab5:file-new-reducer.png)]] ------- 2. create [[Image(wiki:Hadoop_Lab5:3-3.png)]] {{{ #!sh source folder-> 輸入: icas/src Package : Sample Name -> : reducer }}} ----------- 3. modify {{{ #!java package Sample; import java.io.IOException; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; public class reducer extends Reducer { private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); } } }}} * File -> new -> Map/Reduce Driver [[Image(wiki:Hadoop_Lab5:file-new-mr-driver.png)]] ---------- == 3.3 WordCount.java (main function) == 1. new 建立WordCount.java,此檔用來驅動mapper 與 reducer,因此選擇 Map/Reduce Driver [[Image(wiki:Hadoop_Lab5:3-4.png)]] ------------ 2. create {{{ #!sh source folder-> 輸入: icas/src Package : Sample Name -> : WordCount.java }}} ------- 3. modify {{{ #!java package Sample; 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.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.util.GenericOptionsParser; public class WordCount { 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 "); System.exit(2); } Job job = new Job(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(mapper.class); job.setCombinerClass(reducer.class); job.setReducerClass(reducer.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); } } }}} 三個檔完成後並存檔後,整個程式建立完成 [[Image(wiki:Hadoop_Lab5:3-5.png)]] ------- * 三個檔都存檔後,可以看到icas專案下的src,bin都有檔案產生,我們用指令來check {{{ $ cd workspace/icas $ ls src/Sample/ mapper.java reducer.java WordCount.java $ ls bin/Sample/ mapper.class reducer.class WordCount.class }}} = 四、測試範例程式 = * 右鍵點選WordCount.java -> run as -> run on Hadoop = 五、結論 = * 搭配eclipse ,我們可以更有效率的開發hadoop * hadoop 0.20 與之前的版本api以及設定都有些改變,因此hadoop 環境的設定,需要看[http://hadoop.apache.org/core/docs/r0.20.2/quickstart.html hadoop 0.20 的quickstart]; 而如何使用 [http://hadoop.apache.org/core/docs/r0.20.2/api/ hadoop 0.20] 的api,則可以看 /opt/hadoop/src/example/ 裡面的程式碼來提供初步的構想