[Hadoop]基於Eclipse的Hadoop應用開發環境配置

來源:互聯網
上載者:User

標籤:hadoop   eclipse   

  1. 安裝Eclipse
    下載Eclipse(點擊進入下載),解壓安裝。我安裝在/usr/local/software/目錄下。

  2. 在eclipse上安裝hadoop外掛程式

    下載hadoop外掛程式(點擊進入下載) 把外掛程式放到eclipse/plugins目錄下。

  3. 重啟eclipse,配置hadoop installation directory

    如果安裝外掛程式成功,開啟Window–>Preferens,你會發現Hadoop Map/Reduce選項,在這個選項裡你需要配置Hadoop installation directory。配置完成後退出。

  4. 配置Map/Reduce Locations

    在Window–>Show View中開啟Map/Reduce Locations。

    在Map/Reduce Locations中建立一個Hadoop Location。在這個View中,右鍵–>New Hadoop Location。在彈出的對話方塊中你需要配置Location name,如Hadoop1.0,還有Map/Reduce Master和DFS Master。這裡面的Host、Port分別為你在mapred-site.xml、core-site.xml中配置的地址及連接埠。如:
    Map/Reduce Master

    192.168.239.1309001

    DFS Master

    192.168.239.1309000

    配置完後退出。點擊DFS Locations–>Hadoop如果能顯示檔案夾(2)說明配置正確,如果顯示”拒絕串連”,請檢查你的配置。

  5. 建立WordCount項目

    File—>Project,選擇Map/Reduce Project,輸入項目名稱WordCount等。
    在WordCount項目裡建立class,名稱為WordCount,代碼如下:

package WordCount;import java.io.IOException;import java.util.StringTokenizer;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.conf.Configured;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;import org.apache.hadoop.util.Tool;import org.apache.hadoop.util.ToolRunner;public class WordCount extends Configured implements Tool{    /**     *      * @author root     *     */    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);            }// while        }// map    }// mapper    /**     *      * @author root     *     */    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();            }//for            result.set(sum);            context.write(key, result);        }// reduce     }// reducer    /**     *      * @param args     * @return     * @throws Exception     */    public int run(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 name        Job job = new Job(conf, "word count");        // class         job.setJarByClass(WordCount.class);        // mapper        job.setMapperClass(TokenizerMapper.class);        // combiner        job.setCombinerClass(IntSumReducer.class);        // reducer        job.setReducerClass(IntSumReducer.class);        // output key format        job.setOutputKeyClass(Text.class);        // outout value format        job.setOutputValueClass(IntWritable.class);        // input path        FileInputFormat.addInputPath(job, new Path(otherArgs[0]));        // output path        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));        job.waitForCompletion(true);        return job.isSuccessful() ? 0: 1;    }    /**     *      * @param args     * @throws Exception     */    public static void main(String[] args) throws Exception {        int res = ToolRunner.run(new Configuration(), new WordCount(), args);        System.exit(res);    }}

[Hadoop]基於Eclipse的Hadoop應用開發環境配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.