windows下eclipse遠端連線hadoop叢集開發mapreduce

來源:互聯網
上載者:User

標籤:3.2   throws   package   window   輸出   hdf   intern   cli   xxx   

轉載請註明出處,謝謝 2017-10-22 17:14:09 之前都是用python開發maprduce程式的,今天試了在windows下通過eclipse java開發,在開發前先搭建開發環境。在此,總結這個過程,希望能夠協助有需要的朋友。用Hadoop eclipse plugin,可以瀏覽管理HDFS,自動建立MR程式的模板檔案,最爽的就是可以直接Run on hadoop。 1、安裝外掛程式下載hadoop-eclipse-plugin-1.2.1.jar,並把它放到 F:\eclipse\plugins 目錄下。  2、外掛程式配置與使用 2.1指定hadoop的源碼目錄   2.2、開啟Map/Reduce視圖”Window”->”Open Perspective”->”Other”->“Map/Reduce”.“Window”->”Show views”->”Other”->”Map Reduce Tools”->”Map/Reduce locations”.

 

正常情況下回出現左上方的HDFS標誌,等eclipse與hadoop叢集串連後,會在這顯示HDFS目錄結構。  2.3、建立Map/Reduce Localtion點擊圖中紅色框或者滑鼠右擊選中建立,然後出現下面的介面,配置hadoop叢集的資訊。這裡需要注意的是hadoop叢集資訊的填寫。因為我是在windows下用eclipse遠端連線hadoop叢集【完全分布式】開發的,所以這裡填寫的host是master的IP地址。如果是hadoop偽分布式的可以填寫localhost。【Jser name】填寫的windows電腦的使用者名稱,右擊【我的電腦】-->【管理】-->【本機使用者和組】-->【修改使用者名稱字】 完成前面的步驟後,正常的eclipse介面應該像那樣的。注意example1工程是我自己建立的,主要是用來驗證eclipse能否遠端連線hadoop叢集來開發mapreduce程式。並且,此時在eclipse的HDFS視圖介面對HDFS的操作(增刪查)和在命令列上對HDFS操作的結果是一樣的。  3、開發mapreduce程式 3.1、建立mapreduce工程 使用外掛程式開發的好處這時顯示出來了,完成這一個步驟,在工程視圖會出現一個mapreduce工程模板,不用我們自己匯入hadoop的jar包。紅框就是建立mapreduce工程後產生的空模板,我們需要做的是在src檔案夾中建立包和開發java程式。  3.3、在遠程終端中通過命令列方式上傳檔案hadoop fs -put test.txt /input/  或者 通過eclipse 的HDFS視圖 上傳input檔案: /input/test.txt,內容如下:
liang ni hao mawo hen haohaqweasasaxcxc vbv xxxx aaa eee
  3.2、WordCount.java程式
package com.hadoop.example1;import java.io.IOException;import java.util.Iterator;import java.util.StringTokenizer;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapred.FileInputFormat;import org.apache.hadoop.mapred.FileOutputFormat;import org.apache.hadoop.mapred.JobClient;import org.apache.hadoop.mapred.JobConf;import org.apache.hadoop.mapred.MapReduceBase;import org.apache.hadoop.mapred.Mapper;import org.apache.hadoop.mapred.OutputCollector;import org.apache.hadoop.mapred.Reducer;import org.apache.hadoop.mapred.Reporter;import org.apache.hadoop.mapred.TextInputFormat;import org.apache.hadoop.mapred.TextOutputFormat;public class WordCount {    public static class Map extends MapReduceBase implements            Mapper<LongWritable, Text, Text, IntWritable> {        private final static IntWritable one = new IntWritable(1);        private Text word = new Text();        public void map(LongWritable key, Text value,                OutputCollector<Text, IntWritable> output, Reporter reporter)                throws IOException {            String line = value.toString();            StringTokenizer tokenizer = new StringTokenizer(line);            while (tokenizer.hasMoreTokens()) {                word.set(tokenizer.nextToken());                output.collect(word, one);            }        }    }    public static class Reduce extends MapReduceBase implements            Reducer<Text, IntWritable, Text, IntWritable> {        public void reduce(Text key, Iterator<IntWritable> values,                OutputCollector<Text, IntWritable> output, Reporter reporter)                throws IOException {            int sum = 0;            while (values.hasNext()) {                sum += values.next().get();            }            output.collect(key, new IntWritable(sum));        }    }    public static void main(String[] args) throws Exception {        JobConf conf = new JobConf(WordCount.class);        conf.setJobName("wordcount");        conf.setOutputKeyClass(Text.class);        conf.setOutputValueClass(IntWritable.class);        conf.setMapperClass(Map.class);        conf.setCombinerClass(Reduce.class);        conf.setReducerClass(Reduce.class);        conf.setInputFormat(TextInputFormat.class);        conf.setOutputFormat(TextOutputFormat.class);        FileInputFormat.setInputPaths(conf, new Path(args[0]));        FileOutputFormat.setOutputPath(conf, new Path(args[1]));        JobClient.runJob(conf);    }}

 

3.3、運行examplse1工程注意的這種開發方式運行採用的是:run on haoop運行方法:【右擊工程】-->【Run as】-->【run on hadoop】 。在這裡如果跳出一個介面讓你選擇,證明現在工程選用的Java Applicaltion不對。這時可以這樣做:【右擊工程】-->【Run as】-->【run on configrations】。並填寫傳的參數是輸入檔案路徑和輸出目錄路徑。 

在Linux eclipse上開發,以上步驟都成功的話程式會正常運行。但是在windows eclipse 下開發會以下錯誤。因為在hadoop源碼中會檢查windows檔案許可權,因此,我們要修改hadoop源碼。
14/05/29 13:49:16 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable14/05/29 13:49:16 ERROR security.UserGroupInformation: PriviledgedActionException as:ISCAS cause:java.io.IOException: Failed to set permissions of path: \tmp\hadoop-ISCAS\mapred\staging\ISCAS1655603947\.staging to 0700Exception in thread "main" java.io.IOException: Failed to set permissions of path: \tmp\hadoop-ISCAS\mapred\staging\ISCAS1655603947\.staging to 0700at org.apache.hadoop.fs.FileUtil.checkReturnValue(FileUtil.java:691)at org.apache.hadoop.fs.FileUtil.setPermission(FileUtil.java:664)at org.apache.hadoop.fs.RawLocalFileSystem.setPermission(RawLocalFileSystem.java:514)at org.apache.hadoop.fs.RawLocalFileSystem.mkdirs(RawLocalFileSystem.java:349)at org.apache.hadoop.fs.FilterFileSystem.mkdirs(FilterFileSystem.java:193)at org.apache.hadoop.mapreduce.JobSubmissionFiles.getStagingDir(JobSubmissionFiles.java:126)at org.apache.hadoop.mapred.JobClient$2.run(JobClient.java:942)at org.apache.hadoop.mapred.JobClient$2.run(JobClient.java:936)at java.security.AccessController.doPrivileged(Native Method)at javax.security.auth.Subject.doAs(Unknown Source)at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1190)at org.apache.hadoop.mapred.JobClient.submitJobInternal(JobClient.java:936)at org.apache.hadoop.mapreduce.Job.submit(Job.java:550)at org.apache.hadoop.mapreduce.Job.waitForCompletion(Job.java:580)at org.apache.hadoop.examples.WordCount.main(WordCount.java:82)
  3.4、修改hadoop源碼以支援windows下eclipse開發mapreduce程式。出現問題的代碼位於 【hadoop-1.2.1\src\core\org\apache\hadoop\fs\FileUtil.java】。修改方式如下,注釋掉對檔案許可權的判斷。
private static void checkReturnValue(boolean rv, File p,FsPermission permission)throws IOException{    /**    * comment the following, disable this function     if (!rv)    {        throw new IOException("Failed to set permissions of path: " + p +        " to " +        String.format("%04o", permission.toShort()));    }    */}
然後將修改好的檔案重新編譯,並將.class檔案打包到hadoop-core-1.2.1.jar中,並重新重新整理工程。這裡,為了方便大家,我提供已經修改後的jar檔案包,如果需要可以點擊下載,並替換掉原有的hadoop-1.2.1中的jar包,位於hadoop-1.2.1根目錄。再次3,3步驟的操作,這時運行成功了。  3.5查看結果在HDFS視圖重新整理後,可以看到產生output_wordcount檔案夾,進入此目錄可以看見產生的part-00000,其結果為: 

 

          

windows下eclipse遠端連線hadoop叢集開發mapreduce

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.