在配置好Hadoop Eclipse plugin 连接成功后,提交作业时会抛出下面异常:
2013-10-31 9:38:04 org.apache.hadoop.security.UserGroupInformation doAs
严重: PriviledgedActionException as:admin cause:java.io.IOException: Failed to set permissions of path: \home\hadoop\tmp\mapred\staging\admin-454528829\.staging to 0700
Exception in thread "main" java.io.IOException: Failed to set permissions of path: \home\hadoop\tmp\mapred\staging\admin-454528829\.staging to 0700
at org.apache.hadoop.fs.FileUtil.checkReturnValue(FileUtil.java:689)
at org.apache.hadoop.fs.FileUtil.setPermission(FileUtil.java:662)
at org.apache.hadoop.fs.RawLocalFileSystem.setPermission(RawLocalFileSystem.java:509)
at org.apache.hadoop.fs.RawLocalFileSystem.mkdirs(RawLocalFileSystem.java:344)
at org.apache.hadoop.fs.FilterFileSystem.mkdirs(FilterFileSystem.java:189)
at org.apache.hadoop.mapreduce.JobSubmissionFiles.getStagingDir(JobSubmissionFiles.java:116)
at org.apache.hadoop.mapred.JobClient$2.run(JobClient.java:918)
at org.apache.hadoop.mapred.JobClient$2.run(JobClient.java:912)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:396)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1149)
at org.apache.hadoop.mapred.JobClient.submitJobInternal(JobClient.java:912)
at org.apache.hadoop.mapreduce.Job.submit(Job.java:500)
at org.apache.hadoop.mapreduce.Job.waitForCompletion(Job.java:530)
at com.chenzehe.hadoop.action.WordCount.main(WordCount.java:86)
网上有些人的解决方法是把该异常的代码注释掉,如下:
这个是Windows下文件权限问题,在Linux下可以正常运行,不存在这样的问题。
解决方法是,修改/hadoop-1.0.2/src/core/org/apache/hadoop/fs/FileUtil.java里面的checkReturnValue,注释掉即可(有些粗暴,在Window下,可以不用检查):
......
private static void checkReturnValue(boolean rv, File p,
FsPermission permission
) throws IOException {
/**
if (!rv) {
throw new IOException("Failed to set permissions of path: " + p +
" to " +
String.format("%04o", permission.toShort()));
}
**/
}
......
该解决方法不尽美,通过跟踪Hadoop提交作业的代码可发现导致问题的原因,下面代码为在main方法中提交作业到hadoop中处理:
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, "my word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(MapClass.class);
// job.setCombinerClass(ReduceClass.class);
job.setReducerClass(ReduceClass.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);
}
提交作业的代码为job.waitForCompletion(true),跟踪进去代码:
public boolean waitForCompletion(boolean verbose)
throws IOException, InterruptedException, ClassNotFoundException
{
if(state == JobState.DEFINE)
submit();
if(verbose)
jobClient.monitorAndPrintJob(conf, info);
else
info.waitForCompletion();
return isSuccessful();
}
submit()提交方法代码为:
public void submit()
throws IOException, InterruptedException, ClassNotFoundException
{
ensureState(JobState.DEFINE);
setUseNewAPI();
connect();
info = jobClient.submitJobInternal(conf);
super.setJobID(info.getID());
state = JobState.RUNNING;
}
里面还是调用了jobClient的方法jobClient.submitJobInternal(conf),在submitJobInternal()方法中使用status = jobSubmitClient.submitJob(jobId, submitJobDir.toString(), jobCopy.getCredentials())方法来提交job,jobSubmitClient有两个实现如下:
通过看JobClient中的init方法发现默认实例化的是LocalJobRunner对象:
public void init(JobConf conf)
throws IOException
{
String tracker = conf.get("mapred.job.tracker", "local");
tasklogtimeout = conf.getInt("mapreduce.client.tasklog.timeout", 60000);
ugi = UserGroupInformation.getCurrentUser();
if("local".equals(tracker))
{
conf.setNumMapTasks(1);
jobSubmitClient = new LocalJobRunner(conf);
} else
{
rpcJobSubmitClient = createRPCProxy(JobTracker.getAddress(conf), conf);
jobSubmitClient = createProxy(rpcJobSubmitClient, conf);
}
}
所以job默认是提交到本地的job tracker,所以运行失败,可以在提交job时设置下conf的mapred.job.tracker属性为集群,如下:
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
conf.set("mapred.job.tracker", "master-2:49001");
......
System.exit(job.waitForCompletion(true) ? 0 : 1);
}