1.服務器端配置
服務端使用Ubuntu 16.0搭建
①:準備軟件環境
(1) java linux版本,最好是1.8
下載地址:http://www.oracle.com/technetwork/java/javase/downloads /jdk8-downloads-2133151.html
②:Hadoop 下載,我用的hadoop2.6.x
下載地址:http://hadoop.apache.org/releases.html
③:解壓配置
使用 Master,Slaver1,Slaver2作爲集羣主機名
使用tar -zxvf 包名,將java,hadoop解壓到/usr/local/下
④:在~/.bashrc 下,配置環境:
Vim ~/.bashrc
如圖所示,JAVA_HOME 表示配置java的環境,HADOOP_HOME 表示配置hadoop的環境,最後配置PATH.
⑤:安裝ssh服務
使用 apt-get install openssh-server 安裝ssh服務
免密碼登錄ssh:
cd ~
使用 ssh-keygen -t rsa -P “”
就可生成公共祕鑰id_rsa.pub
使用 cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys,寫入公共祕鑰,接着Slaver1,Slaver2按照此方法,生成此id_rsa.pub,接着在相應的注意上分別改成slaver1.pub,slaver2.pub。
利用scp slaver1.pub root@Master:~/.ssh/
將slaver1.pub slaver2.pub 分別複製到主機的 ~/.ssh/下
利用cat ~/.ssh/slaver1.pub >> ~/.ssh/authorized_keys
cat ~/.ssh/slaver2.pub >> ~/.ssh/authorized_keys寫到Master機器上,接着將Master上的authorized_keys分別複製到對應的~/.ssh/下,ssh免密碼登錄配置完成。
⑥:配置hadoop
cd /usr/local/Hadoop/etc/Hadoop/
配置core-site.xml
配置hdfs-site.xml
配置mapred-site.xml.template
配置slaves文件
叢機也一樣。
配置hadoop-env.sh,配置java環境
配置/etc/hosts文件
配置主從機。
至此hadoop集羣配置結束。
二.window eclipse開發環境搭建:
首先下載hadoop+eclipse plugin2.6,下載完成之後,將
將hadoop-eclipse-plugin-2.6.0.jar放在eclipse的plugin的目錄下,啓動eclipse
在window—》preference下,看到:
看到上圖Hadoop Map/Reducer證明hadoop插件安裝成功。
eclipse下配置hadoop:
配置本地hadoop解壓的目錄,另外在window下也要配置hadoop環境
eclipse中配置Map/Reducer
在eclipse看到這些,證明Map/Reducer搭建好了
三.wordcount代碼展示:
mapper:
/**
* IntWritable, Text 均是 Hadoop 中實現的用於封裝 Java 數據類型的類,這些類實現了WritableComparable接口,
* 都能夠被串行化從而便於在分佈式環境中進行數據交換,你可以將它們分別視爲int,String 的替代品。 聲明one常量和word用於存放單詞的變量
*/
public class WordCount_Mapper extends Mapper<Object, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
/**
* Mapper中的map方法: void map(K1 key, V1 value, Context context)
* 映射一個單個的輸入k/v對到一箇中間的k/v對 輸出對不需要和輸入對是相同的類型,輸入對可以映射到0個或多個輸出對。
* Context:收集Mapper輸出的<k,v>對。 Context的write(k, v)方法:增加一個(k,v)對到context
* 程序員主要編寫Map和Reduce函數.這個Map函數使用StringTokenizer函數對字符串進行分隔,通過write方法把單詞存入word中
* write方法存入(單詞,1)這樣的二元組到context中
*/
@Override
protected void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
String text = tokenizer.nextToken();
word.set(text);
context.write(word, one);
}
}
}
Reducer:
public class WordCount_Reducer extends Reducer<Text, IntWritable, Text, IntWritable>{
/**
* Reducer類中的reduce方法:
* void reduce(Text key, Iterable<IntWritable> values, Context context)
* 中k/v來自於map函數中的context,可能經過了進一步處理(combiner),同樣通過context輸出
*/
@Override
protected void reduce(Text key, Iterable<IntWritable> values,
Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
int sum = 0;
for(IntWritable val:values){
sum+=val.get();
}
context.write(key, new IntWritable(sum));
}
}
Main:
public class WordCount_Main {
@SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception {
/**
* Configuration:map/reduce的配置類,向hadoop框架描述map-reduce執行的工作
*/
Configuration conf = new Configuration();
//設置一個用戶定義的job名稱
Job job =new Job(conf,"wordCount");
job.setJarByClass(WordCount_Main.class);
//爲job設置輸入路徑
FileInputFormat.addInputPath(job, new Path("hdfs://192.168.1.129:9000/myHdfs/word/data"));
//爲job設置輸出路徑
FileOutputFormat.setOutputPath(job, new Path("hdfs://192.168.1.129:9000/myHdfs/word/word_result"));
//爲job設置Mapper類
job.setMapperClass(WordCount_Mapper.class);
//爲job設置Combiner類
job.setCombinerClass(WordCount_Reducer.class);
//爲job設置Reducer類
job.setReducerClass(WordCount_Reducer.class);
//爲job的輸出數據設置Key類
job.setOutputKeyClass(Text.class);
//爲job輸出設置value類
job.setOutputValueClass(IntWritable.class);
System.exit(job.waitForCompletion(true)? 0 : 1);
}
}
運行過程可能會報錯。
錯誤一:
在hadoop_home/bin下加入:
錯誤二:
在windows/system32下加入:
這樣就不會報錯了。
另外注意一點,在windows下配置完hadoop的環境之後,會莫名的報錯java環境配置不正確,這是因爲java環境默認配置在:
有空格,就會報錯找不到java路徑,解決方法:
①:更換java路徑,注意無空格路徑
②:打開hadoop的安裝路徑下的etc下的hadoop下的hadoop-env.cmd
修改:
至此,hadoop的服務與開發環境搭建完畢,有什麼問題,歡迎大家指正。如果大家有不明白的地方,也歡迎給我留言。