小宝老豆的专栏

小宝老豆出品必出精品

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-jdksun-java6-jre

1.1.1.安裝sun-java6-doc

1 將javadoc(jdk-6u10-docs.zip) 下載下來 下載點

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

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

<configuration>

  <property>

    <name>fs.default.name</name>

   <value>hdfs://localhost:9000</value>

  </property>

  <property>

    <name>hadoop.tmp.dir</name>

    <value>/tmp/hadoop/hadoop-${user.name}</value>

  </property>

</configuration>

·   編輯 /opt/hadoop/conf/hdfs-site.xml

<configuration>

  <property>

    <name>dfs.replication</name>

    <value>1</value>

  </property>

</configuration>

·   編輯 /opt/hadoop/conf/mapred-site.xml

<configuration>

  <property>

    <name>mapred.job.tracker</name>

    <value>localhost:9001</value>

  </property>

</configuration>

·   啟動

·        $ 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

·   在此提供兩個方法來下載檔案

o  方法一:下載 eclipseSDK 3.4.2 Classic,並且放這檔案到家目錄

o  方法二:貼上指令

o $ cd ~

o $ wgethttp://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內容(非必要)

-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 &

一開始會出現問你要將工作目錄放在哪裡:在這我們用預設值


PS: 之後的說明則是在eclipse 上的介面操作


2.3選擇視野

window ->

open pers.. ->

other.. ->

map/reduce


設定要用 Map/Reduce 的視野


使用 Map/Reduce 的視野後的介面呈現


2.4建立專案

file ->

new ->

project ->

Map/Reduce ->

Map/Reduce Project ->

next


建立mapreduce專案(1)


建立mapreduce專案的(2)

project name-> 輸入 : icas (隨意)

use default hadoop-> Configur Hadoop install... -> 輸入: "/opt/hadoop" -> ok

Finish


2.5設定專案

由於剛剛建立了icas這個專案,因此eclipse已經建立了新的專案,出現在左邊視窗,右鍵點選該資料夾,並選properties


Step1. 右鍵點選project的properties做細部設定


Step2. 進入專案的細部設定頁

hadoop的javadoc的設定(1)

·   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

o  以hadoop-0.20.2-core.jar 的設定內容如下,其他依此類推

source ...-> 輸入:/opt/hadoop/src/core

javadoc ...-> 輸入:file:/opt/hadoop/docs/api/


Step3. hadoop的javadoc的設定完後(2)


Step4. java本身的javadoc的設定(3)

·   javadoc location -> 輸入:file:/usr/lib/jvm/java-6-sun/docs/api/


設定完後回到eclipse 主視窗

2.6連接hadoop server


Step1. 視窗右下角黃色大象圖示"Map/ReduceLocations tag" -> 點選齒輪右邊的藍色大象圖示:


Step2. 進行eclipse 與 hadoop 間的設定(2)

Location Name -> 輸入:hadoop  (隨意)

Map/Reduce Master-> Host-> 輸入:localhost

Map/Reduce Master-> Port-> 輸入:9001

DFS Master ->Host-> 輸入:9000

Finish


設定完後,可以看到下方多了一隻藍色大象,左方展開資料夾也可以秀出在hdfs內的檔案結構


三、 撰寫範例程式

·   之前在eclipse上已經開了個專案icas,因此這個目錄在:

o  /home/hadooper/workspace/icas

·   在這個目錄內有兩個資料夾:

o  src : 用來裝程式原始碼

o  bin : 用來裝編譯後的class檔

·   如此一來原始碼和編譯檔就不會混在一起,對之後產生jar檔會很有幫助

·   在這我們編輯一個範例程式 : WordCount

3.1mapper.java

1.  new

File ->

new ->

mapper


2.  create

source folder-> 輸入: icas/src

Package : Sample

Name -> : mapper


3.  modify

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 extendsMapper<Object, Text, Text, IntWritable>{

 

  private final staticIntWritable one = new IntWritable(1);

  private Text word = newText();

 

  public void map(Objectkey, Text value, Context context)

      throws IOException,InterruptedException {

    StringTokenizer itr = newStringTokenizer(value.toString());

    while (itr.hasMoreTokens()) {

      word.set(itr.nextToken());

      context.write(word, one);

    }

  }

}

建立mapper.java後,貼入程式碼


3.2reducer.java

1.  new

·   File -> new -> reducer


2.  create

source folder-> 輸入: icas/src

Package : Sample

Name -> : reducer


3.  modify

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 extendsReducer<Text, IntWritable, Text, IntWritable>{

  private IntWritable result = newIntWritable();

 

  public void reduce(Textkey, Iterable<IntWritable> values, Contextcontext)

      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


3.3WordCount.java (mainfunction)

1.  new

建立WordCount.java,此檔用來驅動mapper 與 reducer,因此選擇 Map/Reduce Driver


2.  create

source folder-> 輸入: icas/src

Package : Sample

Name -> :WordCount.java


3.  modify

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 = newConfiguration();

    String[] otherArgs = newGenericOptionsParser(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(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);

  }

}

三個檔完成後並存檔後,整個程式建立完成


·   三個檔都存檔後,可以看到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 onHadoop

五、結論

·   搭配eclipse ,我們可以更有效率的開發hadoop

·   hadoop 0.20 與之前的版本api以及設定都有些改變,因此hadoop 環境的設定,需要看hadoop 0.20 的quickstart; 而如何使用 hadoop 0.20 的api,則可以看/opt/hadoop/src/example/ 裡面的程式碼來提供初步的構想

Attachments

·   hadoop_sample_codes.zip (16.9 kB) - added by waue12 months ago.

·   nchc-example.jar (23.2 kB) - added by waue 12 months ago.

·   Makefile (0.8 kB) - added by waue 12 months ago.

·   1-1.png (41.7 kB) - added by waue 4 months ago.

·   2-1.png (28.7 kB) - added by waue 4 months ago.

·   2-2.png (48.6 kB) - added by waue 4 months ago.

·   2-3.png (64.8 kB) - added by waue 4 months ago.

·   2-4.png (42.0 kB) - added by waue 4 months ago.

·   2-4-2.png (52.6 kB) - added by waue 4 months ago.

·   2-5.png (85.1 kB) - added by waue 4 months ago.

·   2-5-1.png (122.6 kB) - added by waue 4 months ago.

·   2-5-2.png (85.0 kB) - added by waue 4 months ago.

·   2-5-3.png (56.4 kB) - added by waue 4 months ago.

·   2-6.png (56.4 kB) - added by waue 4 months ago.

·   2-6-1.png (52.8 kB) - added by waue 4 months ago.

·   2-6-2.png (53.4 kB) - added by waue 4 months ago.

·   3-1.png (40.7 kB) - added by waue 4 months ago.

·   3-2.png (173.1 kB) - added by waue 4 months ago.

·   3-3.png (40.4 kB) - added by waue 4 months ago.

·   3-4.png (52.5 kB) - added by waue 4 months ago.

·   3-5.png (212.1 kB) - added by waue 4 months ago.

·   4-1.png (200.2 kB) - added by waue 4 months ago.

·   4-2.png (236.2 kB) - added by waue 4 months ago.

·   file-new-mapper.png (30.4 kB) - added by waue4 months ago.

·   file-new-mr-driver.png (30.2 kB) - added by waue4 months ago.

·   file-new-project.png (26.7 kB) - added by waue4 months ago.

·   file-new-reducer.png (25.8 kB) - added by waue4 months ago.

·   win-open-other.png (31.4 kB) - added by waue4 months ago.

 

阅读更多
个人分类: Hadoop
想对作者说点什么? 我来说一句

程式源碼c#開發的程式源碼

2010年07月09日 9.36MB 下载

Shane Cook - CUDA Programming -olvasOM

2016年08月13日 17.12MB 下载

PL SQL developer

2014年04月16日 3.97MB 下载

Windows 8应用设计与开发

2013年07月09日 15.64MB 下载

Learn_Objective-C_Mac

2009年02月28日 5.65MB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭