0

这可能是一个基本问题,但在 map reduce 程序中,我想读取输入文件夹中存在的所有文件的名称而不是内容,并且我想将这些文件的名称发送到我的映射器类。配置conf=新配置();

    Job job=new Job(conf,"Analysis");
    job.setInputFormatClass(KeyValueTextInputFormat.class);
    //Path pa =new Path("hdfs://localhost:54310/home/aparajith");
    //pa.

    FileInputFormat.addInputPath(job,new Path("/hduser/"));
    FileOutputFormat.setOutputPath(job, new Path("/CrawlerOutput23/"));

    job.setJarByClass(mapper.Mapper1.class);

    job.setMapperClass(mapper.Mapper1.class);
    job.setReducerClass(mapper.Reducer1.class);

    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(Text.class);
    System.exit(job.waitForCompletion(true) ? 0 : -1);

这是我的主要课程,我似乎无法弄清楚。

4

2 回答 2

1

如果您希望文件键和值的名称来自映射器:

在您的映射器中,您可以简单地忽略传入的键和值(默认情况下,文件中的位置作为LongWritable键,行内容作为Text值)并执行以下操作:

@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
    String fileName = ((FileSplit) context.getInputSplit()).getPath().getName();
    // insert remaining mapper logic here
}

这将获取从中读取映射器中当前键和值的文件名。


如果您只想将目录中的文件名作为映射器的输入:

您可以遍历输入目录 ( yourInputDirPath) 中的文件并编写一个包含其文件名 ( inputDirFilenamesPath) 的新文件,如下所示:

    FSDataOutputStream stream;
    try {
        stream = fs.create(inputDirFilenamesPath);
        RemoteIterator<LocatedFileStatus> it = fs.listFiles(yourInputDirPath, false);
        while (it.hasNext()) {
            stream.write(it.next().getPath().toString().getBytes());
            stream.write('\n');
        }
    } finally {
        stream.close();
    }

然后,您可以简单地使用FileInputFormat.addInputPath(job, inputDirFilenamesPath);将此文件添加到您对 MR 作业的输入中。

于 2013-04-16T08:21:32.057 回答
0

最简单的解决方案是将该目录中文件的所有名称放在一个文件中,并将该文件作为输入文件提供给作业

于 2013-04-16T07:46:11.440 回答