我使用3台Centos虛擬機搭建了一個Hadoop2.6的集群。希望在windows7上面使用IDEA開發mapreduce程序,然后提交的遠程的Hadoop集群上執行。經過不懈的google終於搞定
開始我使用hadoop的eclipse插件來執行job,竟然成功了,后來發現mapreduce是在本地執行的,根本沒有提交到集群上。我把hadoop的4個配置文件加上后就開始出現了問題。
1:org.apache.hadoop.util.Shell$ExitCodeException: /bin/bash: line 0: fg: no job control
網上說要修改源碼,在Hadoop2.6已經合並了那個補丁。這個錯誤怎么解決的也忘記了
2:Stack trace: ExitCodeException exitCode=1:
3:Error: Could not find or load main class org.apache.hadoop.mapreduce.v2.app.MRAppMaster
4:Error: java.lang.RuntimeException: java.lang.ClassNotFoundException: Class WordCount$Map not found
按照我的步驟走,這些問題都能解決,我使用的IDE是IDEA
1:復制Hadoop的4個配置文件放到src目錄下面:core-site.xml,hdfs-site.xml,log4j.properties,mapred-site.xml,yarn-site.xml
2:配置mapred-site.xml
<configuration> <property> <name>mapreduce.framework.name</name> <value>yarn</value> </property> <property> <name>mapred.remote.os</name> <value>Linux</value> </property> <property> <name>mapreduce.app-submission.cross-platform</name> <value>true</value> </property> <property> <name>mapreduce.application.classpath</name> <value> /opt/hadoop-2.6.0/etc/hadoop, /opt/hadoop-2.6.0/share/hadoop/common/*, /opt/hadoop-2.6.0/share/hadoop/common/lib/*, /opt/hadoop-2.6.0/share/hadoop/hdfs/*, /opt/hadoop-2.6.0/share/hadoop/hdfs/lib/*, /opt/hadoop-2.6.0/share/hadoop/mapreduce/*, /opt/hadoop-2.6.0/share/hadoop/mapreduce/lib/*, /opt/hadoop-2.6.0/share/hadoop/yarn/*, /opt/hadoop-2.6.0/share/hadoop/yarn/lib/* </value> </property> <property> <name>mapreduce.jobhistory.address</name> <value>master:10020</value> </property> <property> <name>mapreduce.jobhistory.webapp.address</name> <value>master:19888</value> </property> </configuration>
注意mapreduce.application.classpath一定是絕對路徑,不要搞什么$HADOOP_HOME,我這里反正是報錯的
3:修改yarn-site.xml
-
<configuration> <!-- Site specific YARN configuration properties --> <property> <name>yarn.nodemanager.aux-services</name> <value>mapreduce_shuffle</value> </property> <property> <name>yarn.resourcemanager.address</name> <value>master:8032</value> </property> <property> <name>yarn.application.classpath</name> <value> /opt/hadoop-2.6.0/etc/hadoop, /opt/hadoop-2.6.0/share/hadoop/common/*, /opt/hadoop-2.6.0/share/hadoop/common/lib/*, /opt/hadoop-2.6.0/share/hadoop/hdfs/*, /opt/hadoop-2.6.0/share/hadoop/hdfs/lib/*, /opt/hadoop-2.6.0/share/hadoop/mapreduce/*, /opt/hadoop-2.6.0/share/hadoop/mapreduce/lib/*, /opt/hadoop-2.6.0/share/hadoop/yarn/*, /opt/hadoop-2.6.0/share/hadoop/yarn/lib/* </value> </property> </configuration>
注意yarn.application.classpath一定是絕對路徑,不要搞什么$HADOOP_HOME
4:看下我的代碼
-
package com.gaoxing.hadoop; import java.io.IOException; import java.security.PrivilegedExceptionAction; 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.security.UserGroupInformation; import org.apache.hadoop.util.GenericOptionsParser; public class WordCount { //繼承mapper接口,設置map的輸入類型為<Object,Text> //輸出類型為<Text,IntWritable> public static class Map extends Mapper<Object,Text,Text,IntWritable>{ //one表示單詞出現一次 private static IntWritable one = new IntWritable(1); //word存儲切下的單詞 private Text word = new Text(); public void map(Object key,Text value,Context context) throws IOException,InterruptedException{ //對輸入的行切詞 StringTokenizer st = new StringTokenizer(value.toString()); while(st.hasMoreTokens()){ word.set(st.nextToken());//切下的單詞存入word context.write(word, one); } } } //繼承reducer接口,設置reduce的輸入類型<Text,IntWritable> //輸出類型為<Text,IntWritable> public static class Reduce extends Reducer<Text,IntWritable,Text,IntWritable>{ //result記錄單詞的頻數 private static IntWritable result = new IntWritable(); public void reduce(Text key,Iterable<IntWritable> values,Context context) throws IOException,InterruptedException{ int sum = 0; //對獲取的<key,value-list>計算value的和 for(IntWritable val:values){ sum += val.get(); } //將頻數設置到result result.set(sum); //收集結果 context.write(key, result); } } /** * @param args */ public static void main(String[] args) throws Exception{ Configuration conf = new Configuration(); // conf.set("mapred.remote.os","Linux"); // conf.set("yarn.resourcemanager.address","master:8032"); // conf.set("mapreduce.framework.name","yarn"); conf.set("mapred.jar","D:\\IdeaProjects\\hadooplearn\\out\\artifacts\\hadoo.jar"); //conf.set("mapreduce.app-submission.cross-platform","true"); Job job = Job.getInstance(conf); job.setJobName("test"); //配置作業各個類 job.setJarByClass(WordCount.class); job.setMapperClass(Map.class); job.setCombinerClass(Reduce.class); job.setReducerClass(Reduce.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path("hdfs://master:9000/tmp/hbase-env.sh")); FileOutputFormat.setOutputPath(job, new Path("hdfs://master:9000/tmp/out11")); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
conf
.
set
(
"mapred.jar"
,
"D:\\IdeaProjects\\hadooplearn\\out\\artifacts\\hadoo.jar"
);這是最重要的一句,不然會報上面第4個問題
IDEA中有個功能就是編譯的時候打包:

下班了。
