1. 程式人生 > >myeclipse下搭建hadoop2.7.3開發環境

myeclipse下搭建hadoop2.7.3開發環境

感謝分享:http://www.cnblogs.com/duking1991/p/6056923.html

需要下載的檔案:連結:http://pan.baidu.com/s/1i5yRyuh 密碼:ms91

一  下載並編譯  hadoop-eclipse-plugin-2.7.3.jar

二  將hadoop-eclipse-plugin-2.7.3.jar放到myeclipse的安裝目錄下的plugins目錄下,並重啟myeclipse

  在windows->preferences下可看見hadoop Map/Reduce介面,路徑選擇你WINDOWS下的hadoop解壓後的路徑。

三 選擇Windows->show view->others下的MapReduce Locations

四  新建一個配置 配置如下

host為你的遠端hadoop待連線的主機IP地址

Port:50030 對應mapred-site.xml下的jobtracher地址,如下

 

Port:9000對應core-site.xml下的fs.default.name的埠

user name 填你windows的使用者名稱; 

修改Advanced parameters下的引數

值對應 core-site.xml下的hadoop.tmp.dir引數

修改hdfs-site.xml下的dfs.permissions引數,允許連線

四 儲存配置引數並重啟myeclipse,可以看見如下的檔案結構說明配置連線成功。

五 下載hadoop.ll和winutils.exe 到windows的hadoop/bin目錄下

並將hadoop.dll新增到windows->system32目錄下

五 環境測試

  新建專案:File-->New-->Other-->Map/Reduce Project ,專案名可以隨便取

  它會自動新增依賴包,如下:

   

 新建如下檔案:

編寫實現程式碼,與官方例子為例

package com.duking.hadoop;

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.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 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);
}
}
}

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();

String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println(otherArgs.length);
System.err.println("Usage: wordcount <in> <out>");
System.exit(2);
}
Job job = new Job(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

右擊wordcount,選擇run as - run configurations

右擊wordcount-run as -run on hadoop

注意:HDFS的目錄結構應如下:

protocols為輸入待計算的資料。

檢視執行結果

至此環境搭建成功!!!!!!!!!!

問題總結:環境搭建好後執行mapreduce程式發現output目錄下為空,但把程式打包為jar到hadoop環境下執行是有資料輸出的。

最後查資料解決方法如下:首先把

這個檔案加入工程目錄,注意解壓的hadoop目錄下有兩個這個檔案,不要加錯了。

最後工程目錄如下

然後執行程式發現報錯了,錯誤提示為:Could not locate executable null

查閱資料後發現是沒有新增HADOOP_HOME環境變數,新增即可。

如果不想重啟電腦可以在程式碼下加如下程式碼

注意路徑改為自己的windows  hadoop路徑