1.增長插件
將插件hadoop-eclipse-plugin-1.0.4.jar放入/usr/lib/eclipse/plugins目錄下
(完成後從新啓動eclipse)[插件存放路徑視eclipse存放位置而定]
2.配置hadoop的安裝路徑
eclipse中
window—preferences,在左邊欄中找到Hadoop Map/Reduce,將hadoop的目錄設置爲hadoop的安裝目錄
3.創建MapRedece工程
建立一個MapReduce Project,點擊eclipse主菜單上的File—New—Project,在彈出的對話框中選擇MapReduce Project,以後輸入Project的名
4.創建MapReduce程序
就和創建普通的java程序是同樣的java
package com.sun.mapreduce; 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.input.TextInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; public class WordCount { public static class Map 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{ String line = value.toString(); StringTokenizer tokenizer = new StringTokenizer(line); while (tokenizer.hasMoreTokens()) { word.set(tokenizer.nextToken()); context.write(word, one); } } } public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> { @Override public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { // TODO Auto-generated method stub int sum = 0; for (IntWritable val : values) sum += val.get(); context.write(key, new IntWritable(sum)); } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = new Job(conf, "wordcount"); job.setJarByClass(WordCount.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); job.setMapperClass(Map.class); job.setReducerClass(Reduce.class); job.setInputFormatClass(TextInputFormat.class); job.setOutputFormatClass(TextOutputFormat.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true); } }
5.執行程序
執行程序時要附加必定的參數
點擊Run-run configurations ,在Arguments中填寫參數,參數分別爲輸入文件的目錄 輸出文件的目錄
例如/home/asheng/hadoop/in /home/asheng/hadoop/out(in目錄下應該放置須要分析的文件,out目錄不須要手工創建)
設置完成後點擊Run便可,經過控制檯能夠觀察運行狀態,具體的運行結果在out目錄下apache