1. 安裝插件
準 備 程序:
eclipse-4.2
hadoop-0.20.2-eclipse-plugin.jar
(http://wiki.apache.org/hadoop/EclipsePlugIn
)
將hadoop-0.20.2-eclipse-plugin.jar 複製到eclipse/dropins 目 錄 下,重啓eclipse 。
2.打開MapReduce視圖
Window -> Open Perspective -> Other 選擇Map/Reduce,圖標是個藍色的象。
3.添加一個MapReduce環境
在eclipse下端,控制檯旁邊會多一個Tab,叫“Map/Reduce Locations”,在下面空白的地方點右鍵,選擇“New Hadoop location...”,如圖所示:
在彈出的對話框中填寫如下內容:
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來填寫)
4.使用eclipse對HDFS內容進行修改
經過上一步驟,左側“Project Explorer”中應該會出現配置好的HDFS,點擊右鍵,可以進行新建文件夾、刪除文件夾、上傳文件、下載文件、刪除文件等操作。
注意:每一次操作完在eclipse中不能馬上顯示變化,必須得刷新一下。
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
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.LongWritable;
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;
public class WordCount {
public static class TokenizerMapper extends 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, 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();
if (args.length != 2) {
System.err.println("Usage: wordcount ");
System.exit(2);
}
Job job = new Job(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setReducerClass(IntSumReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
6.2配置運行參數
Run As -> Open Run Dialog... 選擇WordCount程序,在Arguments中配置運行參數:/mapreduce/wordcount/input /mapreduce/wordcount/output/1
分別表示HDFS下的輸入目錄和輸出目錄,其中輸入目錄中有幾個文本文件,輸出目錄必須不存在。
6.3運行
Run As -> Run on Hadoop 選擇之前配置好的MapReduce運行環境,點擊“Finish”運行。
控制檯會輸出相關的運行信息。
6.4查看運行結果
在輸出目錄/mapreduce/wordcount/output/1中,可以看見WordCount程序的輸出文件。除此之外,還可以看見一個logs文件夾,裏面會有運行的日誌。