【问题标题】:Passing a file to Hadoop using the -files argument使用 -files 参数将文件传递给 Hadoop
【发布时间】:2020-07-08 02:03:43
【问题描述】:

我有一个可以在本地正确执行的 MapReduce 程序。

它在映射器类的 setup() 方法中使用一个名为 new-positions.csv 的文件来填充内存中的哈希表:

public void setup(Context context) throws IOException,  InterruptedException {
        newPositions = new Hashtable<String, Integer>();
        File file = new File("new-positions.csv");

        Scanner inputStream = new Scanner(file);
        String line = null;
        String firstline = inputStream.nextLine();
        while(inputStream.hasNext()){
            line = inputStream.nextLine();
            String[] splitLine = line.split(",");
            Integer id = Integer.valueOf(splitLine[0].trim());
            // String firstname = splitLine[1].trim();
            // String surname = splitLine[2].trim();
            String[] emails = new String[4];
            for (int i = 3; i < 7; i++) {
                emails[i-3] = splitLine[i].trim();
            }
            for (String email : emails) {
                if (!email.equals("")) newPositions.put(email, id);
            }
            // String position = splitLine[7].trim();
            inputStream.close();
        }   
    }

Java 程序已导出为可执行 JAR。 JAR 和 full-positions.csv 都保存在我们本地文件系统的同一目录中。

然后,在该目录中,我们在终端执行以下操作(我们也尝试使用 new-positions.csv 的完整路径名):

hadoop jar MR2.jar Reader2 -files new-positions.csv InputDataset OutputFolder

它执行得很好,但是当它到达映射器时,我们得到:

Error: java.io.FileNotFoundException: new-positions.csv (No such file or directory)

这个文件肯定存在于本地,而且我们肯定是在那个目录中执行的。

我们遵循 Hadoop: The Definitive Guide (4th Ed.), p. 中给出的指导。从 274 开始,看不到我们的程序和参数在结构上有何不同。

这可能与 Hadoop 配置有关吗?我们知道有一些变通方法,例如将文件复制到 HDFS,然后从那里执行,但我们需要了解为什么这个“-files”参数没有按预期工作。

编辑:下面是驱动类的一些代码,这也可能是问题的根源:

public int run(String[] args) 抛出 IOException、InterruptedException、ClassNotFoundException { if (args.length != 5) { printUsage(this, ""); 返回 1; }

     Configuration config = getConf();

     FileSystem fs = FileSystem.get(config);

     Job job = Job.getInstance(config);
     job.setJarByClass(this.getClass());
     FileInputFormat.addInputPath(job, new Path(args[3]));

     // Delete old output if necessary
     Path outPath = new Path(args[4]);
     if (fs.exists(outPath)) 
         fs.delete(outPath, true);

     FileOutputFormat.setOutputPath(job, new Path(args[4]));

     job.setInputFormatClass(SequenceFileInputFormat.class);

     job.setOutputKeyClass(NullWritable.class);
     job.setOutputValueClass(Text.class);

     job.setMapOutputKeyClass(EdgeWritable.class);
     job.setMapOutputValueClass(NullWritable.class);

     job.setMapperClass(MailReaderMapper.class);
     job.setReducerClass(MailReaderReducer.class);

     job.setJar("MR2.jar");


     boolean status = job.waitForCompletion(true);
     return status ? 0 : 1;
 }

 public static void main(String[] args) throws Exception {
     int exitCode = ToolRunner.run(new Reader2(), args);
     System.exit(exitCode);
 }

【问题讨论】:

    标签: java hadoop


    【解决方案1】:

    假设您的“new-positions.csv”存在于文件夹中:H:/HDP/,那么您需要将此文件传递为:

    file:///H:/HDP/new-positions.csv

    您需要使用file:/// 限定路径,以表明它是本地文件系统路径。此外,您需要传递完全限定的路径。

    这对我来说非常有效。

    例如,我将本地文件myini.ini 传递如下:

    yarn jar hadoop-mapreduce-examples-2.4.0.2.1.5.0-2060.jar teragen -files "file:///H:/HDP/hadoop-2.4.0.2.1.5.0-2060/share/hadoop/common/myini.ini" -Dmapreduce.job.maps=10 10737418 /usr/teraout/

    【讨论】:

    • 新命令如下所示:hadoop jar MR2.jar Reader2 -files file:///home/local/xxx360/FinalProject/new-positions.csv InputDataset OutputFolder ... 我得到了相同的尝试在 Java 程序中访问“new-positions.csv”时出错。它可能是我们的 Hadoop 配置中的东西吗?
    • 用双引号给出整个路径
    • 仍然不起作用 - 我想知道问题是否出在我的驱动程序类中。我将使用附加信息编辑主要问题。
    【解决方案2】:

    我认为 Manjunath Ballur 给了你一个正确的答案,但你传递的 URI,file:///home/local/xxx360/FinalProject/new-positions.csv 可能无法从 Hadoop 工作机器解析。

    该路径看起来像是机器上的绝对路径,但哪台机器包含home?将服务器添加到路径中,我认为它可能会起作用。

    或者,如果您使用单数 -file,看起来 Hadoop 会复制文件,而不是像使用 -files 那样创建符号链接。

    请参阅文档here

    【讨论】:

      【解决方案3】:

      我没有发现您的代码有任何问题。 从我在技术上与您的相同的工作代码中,当我将- 添加到文件名时,我也得到了java.io.FileNotFoundException。删除-,然后重试:

              File file = new File("newpositions.csv");
      
      hadoop jar MR2.jar Reader2 -files newpositions.csv InputDataset OutputFolder
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-12
        • 2013-06-30
        • 1970-01-01
        • 2021-05-15
        • 1970-01-01
        • 1970-01-01
        • 2019-03-18
        相关资源
        最近更新 更多