標籤:hadoop eclipse
安裝Eclipse
下載Eclipse(點擊進入下載),解壓安裝。我安裝在/usr/local/software/目錄下。
在eclipse上安裝hadoop外掛程式
下載hadoop外掛程式(點擊進入下載) 把外掛程式放到eclipse/plugins目錄下。
重啟eclipse,配置hadoop installation directory
如果安裝外掛程式成功,開啟Window–>Preferens,你會發現Hadoop Map/Reduce選項,在這個選項裡你需要配置Hadoop installation directory。配置完成後退出。
配置Map/Reduce Locations
在Window–>Show View中開啟Map/Reduce Locations。
在Map/Reduce Locations中建立一個Hadoop Location。在這個View中,右鍵–>New Hadoop Location。在彈出的對話方塊中你需要配置Location name,如Hadoop1.0,還有Map/Reduce Master和DFS Master。這裡面的Host、Port分別為你在mapred-site.xml、core-site.xml中配置的地址及連接埠。如:
Map/Reduce Master
192.168.239.1309001
DFS Master
192.168.239.1309000
配置完後退出。點擊DFS Locations–>Hadoop如果能顯示檔案夾(2)說明配置正確,如果顯示”拒絕串連”,請檢查你的配置。
建立WordCount項目
File—>Project,選擇Map/Reduce Project,輸入項目名稱WordCount等。
在WordCount項目裡建立class,名稱為WordCount,代碼如下:
package WordCount;import java.io.IOException;import java.util.StringTokenizer;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.conf.Configured;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;import org.apache.hadoop.util.Tool;import org.apache.hadoop.util.ToolRunner;public class WordCount extends Configured implements Tool{ /** * * @author root * */ 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); }// while }// map }// mapper /** * * @author root * */ 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(); }//for result.set(sum); context.write(key, result); }// reduce }// reducer /** * * @param args * @return * @throws Exception */ public int run(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 name Job job = new Job(conf, "word count"); // class job.setJarByClass(WordCount.class); // mapper job.setMapperClass(TokenizerMapper.class); // combiner job.setCombinerClass(IntSumReducer.class); // reducer job.setReducerClass(IntSumReducer.class); // output key format job.setOutputKeyClass(Text.class); // outout value format job.setOutputValueClass(IntWritable.class); // input path FileInputFormat.addInputPath(job, new Path(otherArgs[0])); // output path FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); job.waitForCompletion(true); return job.isSuccessful() ? 0: 1; } /** * * @param args * @throws Exception */ public static void main(String[] args) throws Exception { int res = ToolRunner.run(new Configuration(), new WordCount(), args); System.exit(res); }}
[Hadoop]基於Eclipse的Hadoop應用開發環境配置