天天看點

如何在eclipse中配置hadoop插件

作者:文渡

1.安裝插件

準備程式:

eclipse-3.3.2(這個版本的插件隻能用這個版本的eclipse)

hadoop-0.20.2-eclipse-plugin.jar (在hadoop-0.20.2/contrib/eclipse-plugin目錄下)

将hadoop-0.20.2-eclipse-plugin.jar 複制到eclipse/plugins目錄下,重新開機eclipse。

2.打開MapReduce視圖

Window -> Open Perspective -> Other 選擇Map/Reduce,圖示是個藍色的象。

如何在eclipse中配置hadoop插件

3.添加一個MapReduce環境

在eclipse下端,控制台旁邊會多一個Tab,叫“Map/Reduce Locations”,在下面空白的地方點右鍵,選擇“New Hadoop location...”,如圖所示:

如何在eclipse中配置hadoop插件

在彈出的對話框中填寫如下内容:

Location name(取個名字)

Map/Reduce Master(Job Tracker的IP和端口,根據mapred-site.xml中配置的mapred.job.tracker來填寫)

DFS Master(Name Node的IP和端口,根據core-site.xml中配置的fs.default.name來填寫)

如何在eclipse中配置hadoop插件

4.使用eclipse對HDFS内容進行修改

經過上一步驟,左側“Project Explorer”中應該會出現配置好的HDFS,點選右鍵,可以進行建立檔案夾、删除檔案夾、上傳檔案、下載下傳檔案、删除檔案等操作。

注意:每一次操作完在eclipse中不能馬上顯示變化,必須得重新整理一下。

如何在eclipse中配置hadoop插件

5.建立MapReduce工程

5.1配置Hadoop路徑

Window -> Preferences 選擇 “Hadoop Map/Reduce”,點選“Browse...”選擇Hadoop檔案夾的路徑。

這個步驟與運作環境無關,隻是在建立工程的時候能将hadoop根目錄和lib目錄下的所有jar包自動導入。

5.2建立工程

File -> New -> Project 選擇“Map/Reduce Project”,然後輸入項目名稱,建立項目。插件會自動把hadoop根目錄和lib目錄下的所有jar包導入。

5.3建立Mapper或者Reducer

File -> New -> Mapper 建立Mapper,自動繼承mapred包裡面的MapReduceBase并實作Mapper接口。

注意:這個插件自動繼承的是mapred包裡舊版的類和接口,新版的Mapper得自己寫。

Reducer同理。

6.在eclipse中運作WordCount程式

6.1導入WordCount

WordCount

1 import java.io.IOException;
 2 import java.util.StringTokenizer;
 3 
 4 import org.apache.hadoop.conf.Configuration;
 5 import org.apache.hadoop.fs.Path;
 6 import org.apache.hadoop.io.IntWritable;
 7 import org.apache.hadoop.io.LongWritable;
 8 import org.apache.hadoop.io.Text;
 9 import org.apache.hadoop.mapreduce.Job;
10 import org.apache.hadoop.mapreduce.Mapper;
11 import org.apache.hadoop.mapreduce.Reducer;
12 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
13 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
14 
15 public class WordCount {
16     public static class TokenizerMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
17 
18         private final static IntWritable one = new IntWritable(1);
19         private Text word = new Text();
20 
21         public void map(LongWritable key, Text value, Context context)
22                 throws IOException, InterruptedException {
23             StringTokenizer itr = new StringTokenizer(value.toString());
24             while (itr.hasMoreTokens()) {
25                 word.set(itr.nextToken());
26                 context.write(word, one);
27             }
28         }
29     }
30 
31     public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
32         private IntWritable result = new IntWritable();
33 
34         public void reduce(Text key, Iterable<IntWritable> values, Context context)
35                 throws IOException, InterruptedException {
36             int sum = 0;
37             for (IntWritable val : values) {
38                 sum += val.get();
39             }
40             result.set(sum);
41             context.write(key, result);
42         }
43     }
44 
45     public static void main(String[] args) throws Exception {
46         Configuration conf = new Configuration();
47         if (args.length != 2) {
48             System.err.println("Usage: wordcount  ");
49             System.exit(2);
50         }
51 
52         Job job = new Job(conf, "word count");
53         job.setJarByClass(WordCount.class);
54         job.setMapperClass(TokenizerMapper.class);
55         job.setReducerClass(IntSumReducer.class);
56         job.setMapOutputKeyClass(Text.class);
57         job.setMapOutputValueClass(IntWritable.class);
58         job.setOutputKeyClass(Text.class);
59         job.setOutputValueClass(IntWritable.class);
60 
61         FileInputFormat.addInputPath(job, new Path(args[0]));
62         FileOutputFormat.setOutputPath(job, new Path(args[1]));
63 
64         System.exit(job.waitForCompletion(true) ? 0 : 1);
65 
66     }
67 
68 }
           

6.2配置運作參數

Run As -> Open Run Dialog... 選擇WordCount程式,在Arguments中配置運作參數:/mapreduce/wordcount/input /mapreduce/wordcount/output/1

分别表示HDFS下的輸入目錄和輸出目錄,其中輸入目錄中有幾個文本檔案,輸出目錄必須不存在。

如何在eclipse中配置hadoop插件

6.3運作

Run As -> Run on Hadoop 選擇之前配置好的MapReduce運作環境,點選“Finish”運作。

如何在eclipse中配置hadoop插件

控制台會輸出相關的運作資訊。

如何在eclipse中配置hadoop插件

6.4檢視運作結果

在輸出目錄/mapreduce/wordcount/output/1中,可以看見WordCount程式的輸出檔案。除此之外,還可以看見一個logs檔案夾,裡面會有運作的日志。

繼續閱讀