一、安裝Hadoop開發插件 java
hadoop安裝包contrib/目錄下有個插件hadoop-0.20.2-eclipse-plugin.jar,拷貝到myeclipse根目錄下/dropins目錄下。 node
二、 啓動myeclipse,打開Perspective: apache
【Window】->【Open Perspective】->【Other...】->【Map/Reduce】->【OK】 app
三、 打開一個View: eclipse
【Window】->【Show View】->【Other...】->【MapReduce Tools】->【Map/Reduce Locations】->【OK】 ssh
四、 添加Hadoop location: 分佈式
location name: 我填寫的是:localhost.
Map/Reduce Master 這個框裏
Host:就是jobtracker 所在的集羣機器,這裏寫localhost
Hort:就是jobtracker 的port,這裏寫的是9999
這兩個參數就是mapred-site.xml裏面mapred.job.tracker裏面的ip和port
DFS Master 這個框裏
Host:就是namenode所在的集羣機器,這裏寫localhost
Port:就是namenode的port,這裏寫8888
這兩個參數就是core-site.xml裏面fs.default.name裏面的ip和port
(Use M/R master host,這個複選框若是選上,就默認和Map/Reduce Master這個框裏的host同樣,若是不選擇,就能夠本身定義輸入,這裏jobtracker 和namenode在一個機器上,所以是同樣的,就勾選上) oop
user name:這個是鏈接hadoop的用戶名,由於我是用lsq用戶安裝的hadoop,並且沒創建其餘的用戶,因此就用lsq。下面的不用填寫。
而後點擊finish按鈕,此時,這個視圖中就有多了一條記錄。 spa
重啓myeclipse並從新編輯剛纔創建的那個鏈接記錄,如今咱們編輯advance parameters tab頁 插件
(重啓編輯advance parameters tab頁緣由:在新建鏈接的時候,這個advance paramters tab頁面的一些屬性會顯示不出來,顯示不出來也就無法設置,因此必須重啓一下eclipse再進來編輯才能看到)
這裏大部分的屬性都已經自動填寫上了,其實就是把core-defaulte.xml、hdfs-defaulte.xml、mapred-defaulte.xml裏面的一些配置屬性展現出來。由於在安裝hadoop的時候,其site系列配置文件裏有改動,因此這裏也要弄成同樣的設置。主要關注的有如下屬性:
fs.defualt.name:這個在General tab頁已經設置了
mapred.job.tracker:這個在General tab頁也設置了
dfs.replication:這個這裏默認是3,由於我在hdfs-site.xml裏面設置成了1,因此這裏也要設置成1
hadoop.job.ugi:這裏要填寫:lsq,Tardis,逗號前面的是鏈接的hadoop的用戶,逗號後面就寫死Tardis(這個屬性不知道我怎麼沒有...)
而後點擊finish,而後就鏈接上了(先要啓動sshd服務,啓動hadoop進程),鏈接上的標誌如圖:
五、新建Map/Reduce Project:
【File】->【New】->【Project...】->【Map/Reduce】->【Map/Reduce Project】->【Project name: WordCount】->【Configure Hadoop install directory...】->【Hadoop installation directory: D:\cygwin\home\lsq\hadoop-0.20.2】->【Apply】->【OK】->【Next】->【Allow output folders for source folders】->【Finish】
六、新建WordCount類:
添加/編寫源代碼:
D:\cygwin\home\lsq\hadoop-0.20.2/src/examples/org/apache/hadoop/examples/WordCount.java
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 {
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("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);
}
}
七、上傳模擬數據文件夾。
爲了運行程序,須要一個輸入的文件夾和輸出的文件夾。輸出文件夾,在程序運行完成後會自動生成。咱們須要給程序一個輸入文件夾。
(1)、在當前目錄(如hadoop安裝目錄)下新建文件夾input,並在文件夾下新建兩個文件file一、file2,這兩個文件內容分別以下:
file1
①在新建的項目WordCount,點擊WordCount.java,右鍵-->Run As-->Run Configurations
②在彈出的Run Configurations對話框中,點Java Application,右鍵-->New,這時會新建一個application名爲WordCount
③配置運行參數,點Arguments,在Program arguments中輸入「你要傳給程序的輸入文件夾和你要求程序將計算結果保存的文件夾」,如:
(若是運行時報java.lang.OutOfMemoryError: Java heap space 配置VM arguments(在Program arguments下)
-Xms512m -Xmx1024m -XX:MaxPermSize=256m
八、點擊Run,運行程序
點擊Run,運行程序,過段時間將運行完成,等運行結束後,能夠在終端中用命令以下,查看是否生成文件夾output:
bin/hadoop fs -ls
用下面命令查看生成的文件內容:
bin/hadoop fs -cat output/*
若是顯示以下,說明已經成功在myeclipse下運行第一個MapReduce程序了。