6

我正在重写 RecordReader 类的方法“next”和 TextInputFormat 类的“getRecordReader”,以便将整个段落发送到映射器而不是逐行发送。(我使用的是旧的 api,并且我的段落的定义是附加的,直到我的文本文件中有一个空行。)
下面是我的代码:

public class NLinesInputFormat extends TextInputFormat  
{  
   @Override
   public RecordReader<LongWritable, Text> getRecordReader(InputSplit split, JobConf conf, Reporter reporter)throws IOException     {   
        reporter.setStatus(split.toString());  
        return new ParagraphRecordReader(conf, (FileSplit)split);
    }
}



public class ParagraphRecordReader implements RecordReader<LongWritable, Text> 
{
        private LineRecordReader lineRecord;
        private LongWritable lineKey;
        private Text lineValue;
        public ParagraphRecordReader(JobConf conf, FileSplit split) throws IOException {
            lineRecord = new LineRecordReader(conf, split);
            lineKey = lineRecord.createKey();
            lineValue = lineRecord.createValue();
        }

        @Override
        public void close() throws IOException {
            lineRecord.close();
        }

        @Override
        public LongWritable createKey() {
            return new LongWritable();

        }

        @Override
        public Text createValue() {
            return new Text("");

        }

        @Override
        public float getProgress() throws IOException {
            return lineRecord.getPos();

        }

        @Override
        public synchronized boolean next(LongWritable key, Text value) throws IOException {
            boolean appended, gotsomething;
            boolean retval;
            byte space[] = {' '};
            value.clear();
            gotsomething = false;
            do {
                appended = false;
                retval = lineRecord.next(lineKey, lineValue);
                if (retval) {
                    if (lineValue.toString().length() > 0) {
                        byte[] rawline = lineValue.getBytes();
                        int rawlinelen = lineValue.getLength();
                        value.append(rawline, 0, rawlinelen);
                        value.append(space, 0, 1);
                        appended = true;
                    }
                    gotsomething = true;
                }
            } while (appended);

            //System.out.println("ParagraphRecordReader::next() returns "+gotsomething+" after setting value to: ["+value.toString()+"]");
            return gotsomething;
        }

        @Override
        public long getPos() throws IOException {
            return lineRecord.getPos();
        }
    }  

问题:
1. 我没有找到任何关于如何做到这一点的具体指南,所以可能是我做错了什么,请评论任何建议?
2. 我能够正确编译它,但是当我运行我的工作时,我的映射器一直在运行,我无法弄清楚问题出在哪里?

4

1 回答 1

3

你的代码对我来说非常好。我所做的唯一更改是将这些类作为内部类并使它们成为静态的。

输入文件如下:

This is awesome.
WTF is this.

This is just a test.

映射器代码如下所示:

@Override
public void map(LongWritable key, Text value, OutputCollector<Text, Text> output, Reporter reporter)
    throws IOException {

    System.out.println(key+" : "+value);
}

输出是:

0 : This is awesome. WTF is this. 
0 : This is just a test.

我相信你不会忘记设置输入格式,但以防万一,设置如下:

conf.setInputFormat(NLinesInputFormat.class);
于 2013-03-25T09:18:42.370 回答