1. 程式人生 > >Eclipse中搭建MapReduce開發環境

Eclipse中搭建MapReduce開發環境

一、安裝Eclipse

1、下載-官方網址

2、解壓     

tar -zxvf eclipse-committers-oxygen-3a-linux-gtk-x86_64.tar.gz

3、啟動 (建立桌面快捷方式)

        我們可以直接啟動,進入eclipse解壓目錄使用./eclipse可以直接啟動,但是為了後期方便啟動,可以在桌面建立快捷方式,建立快捷方式步驟如下:

        進入/usr/share/applications資料夾,在此資料夾下建立一個eclipse的快捷方式。

 vim eclipse.desktop

        在新建檔案中新增如下內容

[Desktop Entry]
Encoding=UTF-8
Name=Eclipse
Comment=Eclipse IDE
Exec=/usr/local/eclipse/eclipse(eclipse存放路徑)
Icon=/usr/local/eclipse/icon.xpm
Terminal=false
Type=Application
Categories=GNOME;Application;Development;
StartupNotify=true
        這時,我們可以看到在/usr/share/applacations/資料夾下有了eclipse的快捷方式,最後把eclipse快捷方式複製到桌面就可以啟動了。

二、在Eclipse中安裝Hadoop外掛

1.複製jar包

        將hadoop-eclipse-plugin-2.7.1.jar檔案複製到“/usr/local/eclipse/plugins”資料夾下。

2.在Eclipse中設定Hadoop的安裝目錄

        重新啟動Eclipse,在主選單中選擇“Window->Preferences”,在彈出的對話方塊中選擇左邊的Hadoop Map/Reduce,然後在右邊的“Hadoop installation directory”中填入Hadoop的安裝目錄,如下圖所示:


圖1 Eclipse中設定Hadoop的安裝目錄

3.建立並配置Map/Reduce Locations

        在主選單中選擇“Window->Show View->Other”,在彈出的對話方塊中找到並展開“MapReduce Tools”,然後選擇“Map/Reduce Locations”,如下圖所示:


圖2 在Eclipse中選擇Map/Reduce Locations

        返回後出現Map/Reduce Locations子視窗,如下圖所示:


圖3 Map/Reduce Locations子視窗

        在“Map/Reduce Locations”子視窗中右鍵單擊,選擇“New Hadoop Location”,建立一個新的Hadoop Location。



圖4 在Eclipse中設定Hadoop Location

        說明:Map/Reduce (V2) Master的Host和port是Yarn的主機和埠號,預設埠號是50020。DFS Master的Host和Port需要根據core-site.xml檔案配置。預設埠號是9000。

        配置正確後返回主視窗會看到新增的Hadoop Location,如下圖所示:


圖5 成功建立的Hadoop Location

三、開發第一個MapReduce程式

        我們以典型的WordCount為例,在Eclipse中編寫第一個MapReduce程式。

1.資料檔案

        本次我們繼續使用之前執行系統自帶案例中建立的wordtest.txt檔案,檔案內容如下:


圖6 wordtest.txt檔案內容

2.建立Map/Reduce專案

        在Eclipse主選單中,選擇“File->New->Other”,在彈出的對話方塊中選擇“Map/Reduce Project”,如下圖所示:


圖6 建立Map/Reduce專案


圖7 輸入專案名稱

3.編寫程式碼

TokenizerMapper.java

package com.hellohadoop;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public 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);
		}
	}
}

IntSumReducer.java

package com.hellohadoop;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public 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);
	}
}

WordCount.java

package com.hellohadoop;
import java.io.IOException;
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.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 IOException, ClassNotFoundException, InterruptedException {
		Configuration conf = new Configuration();
		String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();	
Job job = new Job(conf,"word count");
		job.setJarByClass(WordCount.class);
		//指定Mapper類
		job.setMapperClass(TokenizerMapper.class);
		job.setCombinerClass(IntSumReducer.class);
		//指定Reducer類
		job.setReducerClass(IntSumReducer.class);
		//設定Reduce函式輸出key的型別
		job.setOutputKeyClass(Text.class);
		//設定Reduce函式輸出value的型別
		job.setOutputValueClass(IntWritable.class);
		//指定輸入路徑
		FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
		//指定輸出路徑
		FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
		System.out.println("OK");
		//提交任務
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}
}

4.執行程式

        首先,要設定程式的輸入引數,右鍵WordCount.java選擇Run As->Run Configurations,如下圖所示:


圖8 為Java Application建立一個新的配置

        接著開啟"Arguments",輸入引數,如下圖所示:


圖9 輸入執行引數

        配置完成後,返回主介面,點選Run As->Run on Hadoop,執行結果如下圖所示:


圖10 MapReduce程式執行結果