【Hadoop】出错:org.apache.hadoop.mapreduce.lib.input.InvalidInputException: Input path does not exist

简介

在一次运行MR任务的时候,使用hadoop jar 或者 yarn jar都可以正常运行,但是使用java -cp的时候就总是出现Exception in thread "main" org.apache.hadoop.mapreduce.lib.input.InvalidInputException: Input path does not exist: file:/wordcount/word.txt 这种错误。

原因

其实提示这种错误是正确的,当你在使用java -cp提交,并且Configuration configuration = new Configuration(); 没有添加集群配置文件的时候,我们使用的是本地模式,为什么?因为 Configuration加载配置文件的顺序是先添加core-default.xml,core-site.xml这两个文件,但是是使用hadoop jar 或者 yarn jar 才会去hadoop 的 /etc/hadoop 目录下找配置文件。如果你使用java -cp 他是找不到得出,自然就是本地模式了。
在这里插入图片描述

解决办法

解决办法自然就是把集群配置文件手动添加到Configuration 中 ,使用addResource方法。
例如:
在这里插入图片描述

添加代码:
Configuration configuration = new Configuration();
String properties = System.getProperty("user.dir");
configuration.addResource(properties+"/conf/hdfs-site.xml");
configuration.addResource(properties+"/conf/mapred-site.xml");
configuration.addResource(properties+"/conf/yarn-site.xml");

加载顺序:
Configuration: core-default.xml, core-site.xml, /opt/module/oozie-4.0.0-cdh5.3.6/oozie-apps/java-main/conf/core-site.xml, /opt/module/oozie-4.0.0-cdh5.3.6/oozie-apps/java-main/conf/hdfs-site.xml, /opt/module/oozie-4.0.0-cdh5.3.6/oozie-apps/java-main/conf/mapred-site.xml, /opt/module/oozie-4.0.0-cdh5.3.6/oozie-apps/java-main/conf/yarn-site.xml

!!!!但是你这样添加是不行的,加载不到这些配置文件的!!!!!!!

正确的书写方式是:
Configuration configuration = new Configuration();
String properties = System.getProperty("user.dir");
configuration.addResource(new File(properties+"/conf/core-site.xml").toURI().toURL());
configuration.addResource(new File(properties+"/conf/hdfs-site.xml").toURI().toURL());
configuration.addResource(new File(properties+"/conf/mapred-site.xml").toURI().toURL());
configuration.addResource(new File(properties+"/conf/yarn-site.xml").toURI().toURL());

加载顺序:
Configuration: core-default.xml, core-site.xml, file:/opt/module/oozie-4.0.0-cdh5.3.6/oozie-apps/java-main/conf/core-site.xml, file:/opt/module/oozie-4.0.0-cdh5.3.6/oozie-apps/java-main/conf/hdfs-site.xml, file:/opt/module/oozie-4.0.0-cdh5.3.6/oozie-apps/java-main/conf/mapred-site.xml, file:/opt/module/oozie-4.0.0-cdh5.3.6/oozie-apps/java-main/conf/yarn-site.xml
这种才能加载到配置文件,使用java -cp demo1.jar:lib/* com.xing.mr.demo1.WordCountDriver才可以了
这里的lib是所有要用到的jar包括hadoop的一些包
在这里插入图片描述
运行截图:
在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值