此文主要介紹mac環(huán)境下泳姐,在eclipse中搭建hadoop2的開(kāi)發(fā)環(huán)境迷帜,以編寫mapreduce程序沈贝。
開(kāi)發(fā)環(huán)境:
- mac os x 10.10
- eclipse
- hadoop2.6.0
- hadoop2x-eclipse-plugin
主要流程是先搭建hadoop的偽分布集群袍暴,當(dāng)然也可以搭建真的集群環(huán)境倒源。然后編譯hadoop eclipse插件坊萝。最后配置eclipse并編寫map reduce.
- hadoop2安裝孵稽,這個(gè)網(wǎng)上資料挺多,分分鐘可以在本機(jī)上搭個(gè)standlone的偽分布集群十偶。
- 安裝eclipse,自己可以到官網(wǎng)下載最新版的eclipse. 然后解壓即安裝菩鲜。
- github上下載hadoop2x-eclipse-plugin. 并編譯,拷貝到eclipse下面。注意惦积,這里mac的eclipse home為.
/XXX/Eclipse.app/Contents/Eclipse.
mac下面app是一個(gè)文件夾接校。比如我的就是:
$ pwd
/Users/luke/dev/Eclipse.app/Contents/Eclipse
- 配置eclipse并編寫mapreduce程序。著重介紹下這個(gè)狮崩。
完成上面三步蛛勉,裝好插件后,打開(kāi)eclipse. 現(xiàn)在你可以看到這個(gè)hadoop的console了睦柴。
現(xiàn)在诽凌,你需要?jiǎng)?chuàng)建一個(gè)location, 到時(shí)候你的程序就可以在上面跑了。點(diǎn)擊小象圖標(biāo)坦敌。
map/reduce(V2) Master得和你mapred-site.xml里的配置相同侣诵,一般默認(rèn)9001.
DFS Master得和你defaust fs配置相同,一般默認(rèn)9000.
其他可以隨意配置狱窘。
配置好后啟動(dòng)hadoop(start-all.sh)杜顺,即可在eclipse里面refresh看到如下內(nèi)容;
接下來(lái)創(chuàng)建一個(gè)mapreduce項(xiàng)目。并添加代碼训柴。
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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 FirstOne {
//map將輸入中的value復(fù)制到輸出數(shù)據(jù)的key上哑舒,并直接輸出
public static class Map extends Mapper<Object,Text,Text,Text>{
private static Text line=new Text();//每行數(shù)據(jù)
//實(shí)現(xiàn)map函數(shù)
public void map(Object key,Text value,Context context)
throws IOException,InterruptedException{
line=value;
context.write(line, new Text(""));
}
}
//reduce將輸入中的key復(fù)制到輸出數(shù)據(jù)的key上妇拯,并直接輸出
public static class Reduce extends Reducer<Text,Text,Text,Text>{
//實(shí)現(xiàn)reduce函數(shù)
public void reduce(Text key,Iterable<Text> values,Context context)
throws IOException,InterruptedException{
context.write(key, new Text(""));
}
}
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();
//這句話很關(guān)鍵幻馁,hdfs設(shè)置不成功默認(rèn)用本地輸入洗鸵、輸出
//conf.set("mapred.job.tracker", "localhost:9001");
conf.set("fs.default.name", "hdfs://localhost:9000");
String[] ioArgs=new String[]{"/input","/output"};
String[] otherArgs = new GenericOptionsParser(conf, ioArgs).getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println("Usage: Data Deduplication <in> <out>");
System.exit(2);
}
Job job = new Job(conf, "Data Deduplication");
job.setJarByClass(FirstOne.class);
//設(shè)置Map、Combine和Reduce處理類
job.setMapperClass(Map.class);
job.setCombinerClass(Reduce.class);
job.setReducerClass(Reduce.class);
//設(shè)置輸出類型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
//設(shè)置輸入和輸出目錄
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
導(dǎo)入hadoop相關(guān)的jar文件仗嗦。
這里請(qǐng)注意膘滨,導(dǎo)入以下目錄下的所有庫(kù),不然可能程序里面沒(méi)錯(cuò)了稀拐,但run的時(shí)候一堆依賴包找不到火邓。
- $HADOOP_HOME/share/hadoop/common
- $HADOOP_HOME/share/hadoop/common/lib
- $HADOOP_HOME/share/hadoop/hdfs
- $HADOOP_HOME/share/hadoop/mapred
- $HADOOP_HOME/share/hadoop/yarn
你可以發(fā)現(xiàn)除common外其他目錄下也有XX/lib,但他們都是從common/lib拷貝過(guò)去的,所以common/lib是必須的德撬。
比如這種類型的錯(cuò)誤就是庫(kù)沒(méi)導(dǎo)入完全造成的铲咨。
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.hadoop.conf.Configuration.<clinit>(Configuration.java:173)
at FirstOne.main(FirstOne.java:40)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
好了,接下來(lái)你可以run on hadoop了蜓洪。選擇你項(xiàng)目的那個(gè)application.
這里附上測(cè)試文件內(nèi)容纤勒。
file1.txt
2012-3-1 a
2012-3-2 b
2012-3-3 c
2012-3-4 d
2012-3-5 a
2012-3-6 b
2012-3-7 c
2012-3-3 c
file2
2012-3-1 b
2012-3-2 a
2012-3-3 b
2012-3-4 d
2012-3-5 a
2012-3-6 c
2012-3-7 d
2012-3-3 c