Hadoop - 从记录阅读器到地图功能的多个文件

时间:2014-02-18 14:43:49

标签: hadoop mapreduce recordreader

我已经实现了自定义组合文件输入格式,以便为由文件组组成的Map任务创建拆分。我创建了一个解决方案,通过记录阅读器传递拆分的每个文件,一切都很好。现在我试图将整套文件传递给map函数。

这是我的记录阅读器代码:

public class MultiImagesRecordReader extends
        RecordReader<Text[], BytesWritable[]> {
private long start = 0;
private long end = 0;
private int pos = 0;
private BytesWritable[] value;
private Text key[];
private CombineFileSplit split;
private Configuration conf;
private FileSystem fs;
private static boolean recordsRead;

public MultiImagesRecordReader(CombineFileSplit split,
        TaskAttemptContext context, Integer index) throws IOException {
    this.split = split;
    this.conf = context.getConfiguration();
}

@Override
public void initialize(InputSplit genericSplit, TaskAttemptContext context)
        throws IOException, InterruptedException {
    start = split.getOffset(0);
    end = start + split.getLength();
    recordsRead = false;
    this.pos = (int) start;
    fs = FileSystem.get(conf);
    value = new BytesWritable[split.getNumPaths()];
    key = new Text[split.getNumPaths()];
}

@Override
public boolean nextKeyValue() throws IOException, InterruptedException {
    if (recordsRead == true) {
        System.out.println("Sono nel next true"+InetAddress.getLocalHost());
        return false;
    } else {
        recordsRead = true;
        System.out.println("Sono nel next false"+InetAddress.getLocalHost());
        for (int i = 0; i < split.getNumPaths(); i++) {

            int fileLength = (int) split.getLength(i);
            Path path = split.getPath(i);
            byte[] result = new byte[fileLength];

            FSDataInputStream in = null;

            String file_path = path.toString();
            key[i] = new Text(file_path);
            try {
                in = fs.open(path);
                IOUtils.readFully(in, result, 0, fileLength);

            } finally {
                IOUtils.closeStream(in);
            }

            value[i] = new BytesWritable(result);
        }
        return true;
    }
}

使用此代码时,map函数会重复正确地接收键和值的向量。我的意思是,我期望map函数被调用一次,而不是多次调用它。我做错了什么?

1 个答案:

答案 0 :(得分:0)

我想您知道,对于您的阅读器从map()Mapper返回的每条记录,将会调用currentKey() currentValue(),直到给定{中的所有键值对{1}}已经完成了。 我知道你的map函数是为同一个键值对重复调用的(对于单个键值对应该调用一次)。这意味着您的记录阅读器重复读取相同的记录(键值对)。 我还实现了自定义组合文件输入格式和记录阅读器。您可以在同一个项目中看到他们的通用表单here和实现here