重写RecordReader以立即读取段落而不是行

时间:2013-03-25 08:24:52

标签: hadoop

我重写了RecordReader类的“next”方法和TextInputFormat类的“getRecordReader”,以便将整个段落发送到mapper而不是逐行。 (我正在使用旧的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.我能够正确编译,但是当我运行我的工作时,我的映射器一直在运行,我无法弄清楚问题出在哪里?

1 个答案:

答案 0 :(得分: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);