Input path does not exist: hdfs://master:8020/user/root/MyWordCount

在使用 hadoop jar /opt/Task.jar MyWordCount input.txt /output时报错,错误信息为
Input path does not exist: hdfs://master:8020/user/root/MyWordCount
检查后感觉是把MyWordCount 识别为输入文件参数了,但是不知代码具体问题在哪里,求赐教!!

main()函数代码如下(即纯复制WordCount原代码,仍然出错):

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> [<in>...] <out>");
            System.exit(2);
        }
        Job job = Job.getInstance(conf, "my word count");
        job.setJarByClass(myWordCount.class);
        job.setMapperClass(TokenizerMapper.class);
        job.setCombinerClass(IntSumReducer.class);
        job.setReducerClass(IntSumReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        for (int i = 1; i < otherArgs.length - 1; ++i) {
            FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
        }
        FileOutputFormat.setOutputPath(job,
                new Path(otherArgs[otherArgs.length - 1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }

根据你提供的代码,错误可能出在以下几个地方:

  1. 输入路径参数问题:检查一下你在命令行中输入的参数是否正确。你的命令应该是 hadoop jar /opt/Task.jar MyWordCount input.txt /output,其中 input.txt 应该是存在的文件,/output 是你期望的输出路径。

  2. 输入路径问题:检查一下 input.txt 文件是否存在,并且确保你有权限访问该文件。你可以使用 hdfs dfs -ls 命令来查看 HDFS 中的文件列表。

  3. 代码逻辑问题:检查一下你的代码逻辑是否正确。在你的代码中,otherArgs 数组中的第一个参数应该是输入路径,最后一个参数应该是输出路径。你可以使用 System.out.println 来打印出 otherArgs 数组的内容,以确认参数的正确性。

希望以上提示能够帮助你找到问题所在。如果问题仍然存在,请提供更多的错误信息和相关代码,以便更好地帮助你解决问题。

根据你提供的代码和错误信息,问题出在获取输入文件路径的部分。在你的代码中,它假设输入文件路径是otherArgs[1],而不是otherArgs[0]。因此,对于你的命令行参数/opt/Task.jar MyWordCount input.txt /output,otherArgs[1]实际上是MyWordCount,otherArgs[0]是input.txt。

为了解决这个问题,请将以下代码行:

for (int i = 1; i < otherArgs.length - 1; ++i) {
    FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
}

更改为:

for (int i = 0; i < otherArgs.length - 1; ++i) {
    FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
}



```这样修改后,你的代码会正确地将第一个(otherArgs[0])和之后的参数作为输入文件路径。重新编译并运行代码,应该能够解决这个问题。
要是还不行改代码为:

```java
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> [<in>...] <out>");
        System.exit(2);
    }
    Job job = Job.getInstance(conf, "my word count");
    job.setJarByClass(MyWordCount.class);
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    FileInputFormat.setInputPaths(job, new Path(otherArgs[0]));
    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
}

上面代码假设你的输入路径是通过命令行参数传递给程序的。修改后的代码会将第一个参数作为输入路径,第二个参数作为输出路径。确保提供正确数量的命令行参数(最少2个)。