前提:已經(jīng)安裝好eclipse并且設(shè)置好jdk環(huán)境變量
1、把hadoop-eclipse-plugin-2.×.×.jar放到eclipse下的plugins目錄中,這里版本號(hào)自己決定,我的是2.8.3

2、啟動(dòng)eclipse,如果1步驟沒有問題,點(diǎn)擊window>preferences,會(huì)看到下圖圈紅部分。

這里如果沒有的話,可以試著刪掉下圖圈出文件再重啟eclipse,如果不行就說明jar與eclipse版本有沖突,換個(gè)jar包試試。

3、配置hadoop安裝路徑>也就是從官網(wǎng)下載的hadoop解壓的路徑,我下載的是2.7.7版本的,與plugin插件版本不一致但是沒有問題,最好還是用相近版本的。這里配置是為了創(chuàng)建mapreduce項(xiàng)目時(shí)可以自動(dòng)導(dǎo)入hadoop相關(guān)包,如果配置錯(cuò)誤,會(huì)發(fā)現(xiàn)項(xiàng)目編寫時(shí)提醒缺少hadoop包。

4、點(diǎn)擊window>show view,找到下圖圈出tool并打開,

然后點(diǎn)擊new hadoop location,

然后按照下圖中所示填寫,

完成以后點(diǎn)擊finish,然后可以在project? explorer(記得在eclipse最右邊上面切換成hadoop布局)那里看到dfs? locations,如下圖。

5、找到hadoop在windows下運(yùn)行的程序工具,winutils.exe和hadoop.dll,將winutils.exe放在hadoop安裝目錄下的bin目錄里,hadoop.dll放在c>windows>system32 里面,否則運(yùn)行程序時(shí)會(huì)報(bào)錯(cuò),這里要注意的是這兩個(gè)工具要和windows版本一致,我是win10,如果不一致程序也會(huì)報(bào)錯(cuò)。
6、配置環(huán)境變量,HADOOP_HOME就是hadoop安裝路徑啦,然后再path里面增加bin,這個(gè)不細(xì)說了。最好重啟電腦使他生效。
7、新建map/reduce project,命名為wordcount,然后新建一個(gè)類叫WordCount,源代碼下面會(huì)貼出。package org.apache.hadoop.examples;
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 {
? ? ? ? ? ? String line = value.toString();
? ? ? ? ? ? StringTokenizer itr = new StringTokenizer(line);
? ? ? ? ? ? while (itr.hasMoreTokens()) {
? ? ? ? ? ? ? ? word.set(itr.nextToken().toLowerCase());
? ? ? ? ? ? ? ? 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, new IntWritable(sum));
? ? ? ? }
? ? }
? ? 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("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);
? ? }
}
這里還要在src下面新建一個(gè)log4j.properties文件,里面代碼如下
# Configure logging for testing: optionally with log file
?
#log4j.rootLogger=debug,appender
log4j.rootLogger=info,appender
#log4j.rootLogger=error,appender
?
#\u8F93\u51FA\u5230\u63A7\u5236\u53F0
log4j.appender.appender=org.apache.log4j.ConsoleAppender
#\u6837\u5F0F\u4E3ATTCCLayout
log4j.appender.appender.layout=org.apache.log4j.TTCCLayout
如果沒有這個(gè)文件運(yùn)行時(shí)會(huì)報(bào)錯(cuò)找不到日志文件。
8、接下來要做運(yùn)行前的準(zhǔn)備了,首先在hdfs里面用命令新建一個(gè)input目錄,hadoop fs -mkdir /input
然后hadoop fs -put test.txt /input/ #向input文件夾里放入所在路徑的test.txt文件,這個(gè)文件也要自己建,自己往里面隨便寫幾個(gè)單詞。
對(duì)wordcount.java右鍵,run as>run? configurations,然后像下圖這樣配置。

注意檢查里面的output在hdfs下是沒有的,如果已經(jīng)有了先把它刪除掉。
9、運(yùn)行,run? on hadoop,等待程序運(yùn)行結(jié)束,結(jié)束后可以看到dfs locations下面藍(lán)色小象下面會(huì)有output目錄,點(diǎn)擊第二個(gè)文件就能看到統(tǒng)計(jì)結(jié)果啦,到這里就結(jié)束啦!

最后是hadoop-eclipse-plugin-2.8.3.jar插件和win10下hadoop2.7.7對(duì)應(yīng)的winutils.exe,hadoop.dll資源。
鏈接:https://pan.baidu.com/s/1kw33aDoE-sauXYUH7qzyww
提取碼:v695