一.環境html
Hadoop部署環境:java
Centos3.10.0-327.el7.x86_64git
Hadoop2.6.5github
Java1.8.0_221apache
代碼運行環境:服務器
Windows 10app
Hadoop 2.6.5eclipse
Java11.0.2 oop
二.安裝Hadoop-Eclipse-Pluginui
在Eclipse中編譯和運行Mapreduce程序,須要安裝hadoop-eclipse-plugin,可下載Github上的 hadoop2x-eclipse-plugin 。
下載後將release中的hadoop-eclipse-plugin-2.6.0.jar放在eclipse下面plugins目錄下。
三.配置Hadoop-Plugin
運行eclipse後,點擊Window->Preferences在Hadoop Map/Reduce中填上計算機中安裝的hadoop目錄。
四.在Eclipse中操做HDFS中的文件
咱們以前一直使用命令操做Hdfs,接下來再配置幾步就能夠在Eclipse中可視化操做啦。
選擇Window下面的Show View->Other... ,在彈出的框裏面展開MapReduce Tools,選擇Map/Reduce Locations點擊Open。
而後在彈出的欄目右鍵,點擊New Hadoop location在彈出框General下面填上活躍的NameNode和端口號信息。
配置好後,能夠在左側刷新便可看到HDFS文件(Tips:對HDFS不少操做後,插件不會自動幫咱們刷新內容,須要咱們手動刷新)
五.在Eclipse中建立MapReduce項目
選擇File->New->Project... 選擇Map/Reduce Project ,選擇Next,填寫項目名稱,這裏我起名MapReduceFirstDemo。
而後將服務器上的core-site.xml和hdfs-site.xml複製到項目根目錄下,並在根目錄下建立一個log4j.properties,填上以下內容:
hadoop.root.logger=DEBUG, console
log4j.rootLogger = DEBUG, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n
在src中右鍵建立一個Package,起名MapReduceFirstPack,而後在MapReduceFirstPack下面建立一個WordCount類。大體結構以下圖:
將下面的代碼複製到WordCount裏面
package MapRedoceFirstPack; import java.io.IOException; import java.util.Iterator; 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 void main(String[] args) throws Exception { // TODO Auto-generated method stub Configuration conf=new Configuration(); String[] otherArgs=(new GenericOptionsParser(conf, args)).getRemainingArgs(); if(otherArgs.length<2) { System.err.println("Usage:wordcount"); System.exit(2); } Job job=Job.getInstance(conf,"word count"); job.setJarByClass(WordCount.class); job.setMapperClass(WordCount.TokenizerMapper.class); job.setCombinerClass(WordCount.IntSumReducer.class); job.setReducerClass(WordCount.IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); for(int i=0;i<otherArgs.length-1;++i) { FileInputFormat.addInputPath(job, new Path(otherArgs[i])); } FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length-1])); System.exit(job.waitForCompletion(true)?0:1); } private static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable>{ public IntSumReducer() {} private IntWritable result=new IntWritable(); public void reduce(Text key,Iterable<IntWritable> values,Reducer<Text,IntWritable,Text,IntWritable>.Context context) throws IOException,InterruptedException{ int sum=0; IntWritable val; for(Iterator i$=values.iterator();i$.hasNext();sum+=val.get()) { val=(IntWritable)i$.next(); } this.result.set(sum); context.write(key, this.result); } } public static class TokenizerMapper extends Mapper<Object,Text,Text,IntWritable>{ private static final IntWritable one=new IntWritable(1); private Text word=new Text(); public TokenizerMapper() { } public void map(Object key,Text value,Mapper<Object,Text,Text,IntWritable>.Context context) throws IOException,InterruptedException { StringTokenizer itr=new StringTokenizer(value.toString()); while(itr.hasMoreTokens()) { this.word.set(itr.nextToken()); context.write(this.word, one); } } } }
六.在Eclipse中運行MapReduce項目
在運行上述項目以前,咱們須要配置下運行參數。 在項目右鍵Run As->Run Configuration。 在彈出的框裏面選擇Java Applicaton下面的WordCount(Tips:若是沒有WordCount,則雙擊Java Application就有了),在Arguments下面添加input output(Tips:表明了輸入目錄和輸出目錄,輸入目錄放要計算的內容,這個須要本身建立,輸出目錄必定不要建立,它會自動生成,不然會提示已存在此目錄的錯誤),以下圖:
而後點擊Run運行。
運行完畢後,在左側刷新,在output目錄能夠看到兩個文件,_SUCCESS是標識文件,表明執行成功的意思。part-r-00000存放的執行結果。
另外推薦一篇不錯的教程: http://dblab.xmu.edu.cn/blog/hadoop-build-project-using-eclipse/