需要下載的文件:鏈接: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路徑