標籤:eclipse hadoop2.4 eclipse運行mapreduce eclipse運行wordcount eclipse hadoop2.4
使用hadoop進行MapReduce編程的時候,我們都希望使用IDE進行開發,本文主要介紹如何使用Eclipse進行hadoop編程。
如果你的叢集還沒搭好,可以參考我的前一篇文章Ubuntu下用hadoop2.4搭建叢集(偽分布式)
一、安裝Eclipse
方法一:直接在Ubuntu的軟體中心進行下載,如所示。
方法二:先下載Eclispe壓縮檔後,使用命令進行安裝,:http://pan.baidu.com/s/1mgiHFok
sudo tar -zxvf eclipse-dsl-juno-SR1-linux-gtk.tar.gz
這樣Eclipse就安裝好了。
二、安裝 Hadoop-Eclipse-Plugin
下載 hadoop2x-eclipse-plugin ,將 release 中的 hadoop-eclipse-kepler-plugin-2.2.0.jar (雖然標註的是 2.2.0,但在 2.4.1 下是沒問題的,應該在 2.x 版本下都可以,不過有時候會提示有些東西到期,對於學習者來說,我個人覺得暫時可以不考慮這些細節)複製到 Eclipse 安裝目錄的 plugin 檔案夾中,運行./eclipse -clean 重啟 Eclipse 即可。使用方法一的Eclipse 的預設安裝目錄為:/usr/lib/eclipse :使用方法二的安裝Eclipse的話目錄根據自己而定了,我的目錄是/usr/local/eclipse。./eclipse -clean命令需要在Eclipse的安裝目錄下。開啟後就可一看到檔案系統了。
外掛程式需要進一步的配置。
第一步:選擇 Window 菜單下的 Preference ,然後彈出一個表單,表單的左側會多出 Hadoop Map/Reduce 選項,點擊此選項,選擇 Hadoop 的安裝目錄(如/usr/local/hadoop)。
第二步:切換 Map/Reduce 工作目錄,選擇 Window 菜單下選擇 Open Perspective,彈出一個表單,從中選擇 Map/Reduce 選項即可進行切換。
第三步 點擊Map/Reduce Location選項卡,點擊右邊小象表徵圖,開啟Hadoop Location配置視窗:
輸入Location Name,任意名稱即可.配置Map/Reduce Master和DFS Mastrer,Host和Port配置成與core-site.xml的設定一致即可。
點擊"Finish"按鈕,關閉視窗。
點擊左側的DFSLocations—>MapReduceProject(上一步配置的location name),如能看到user,表示安裝成功
如果出現這個提示這個錯誤Error:call from mylinux/127.0.1.1 to localhost:9090 failed on connection exception java.Connection.net.ConnectException拒絕串連。
首先確定hadoop有沒有啟動。我當時也是由於沒有啟動hadoop,然後折騰了好久才發現了這個問題,希望對大家有所協助。
具體一些原因可以參考我的一篇部落格在Ubuntu下使用Eclispe串連hadoop時拒絕連結解決方案總結
三、建立WordCount例子
File—>Project,選擇Map/Reduce Project,輸入項目名稱WordCount等。
在WordCount項目裡建立class,名稱為WordCount,代碼如下:
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);}}
1、在HDFS上建立目錄input
hadoop fs -mkdir input
這是使用命令來建立,我們可以在Eclipse裡面右鍵hadoop(根據個人配置不同這個會有出入)進行建立
2、拷貝本地README.txt到HDFS的input裡
hadoop fs -copyFromLocal /usr/local/hadoop/README.txt input
同樣我們可以右鍵input,然後選擇Upload file ,使用可視化的形式進行檔案上傳。
3、點擊WordCount.java,右鍵,點擊Run As—>Run Configurations,配置運行參數,即輸入和輸出檔案夾
當然我們也可以在代碼裡直接寫路徑,真正搞懂檔案系統你會探索方法還有很多,只是需要修改java代碼。
下面這個配置是對應了代碼裡面這個
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();,
hdfs://localhost:9000/user/hadoop/input hdfs://localhost:9000/user/hadoop/output
4、運行完成後,查看運行結果
第一種方法就是在終端裡面直接使用命令列進行查看。
hadoop fs -ls output
可以看到有兩個輸出結果,_SUCCESS和part-r-00000
執行
hadoop fs -cat output/*
第二種方法就是直接在Eclipse裡面查看。首先記得重新整理一下檔案系統~
展開DFS Locations,如所示,雙擊開啟part-r00000查看結果
Ubuntu系統下使用Eclipse搭建hadoop2.4運行環境