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

Jac*_*row 6 hadoop

我重写了RecordReader类的"next"方法和TextInputFormat类的"getRecordReader",以便将整个段落发送到mapper而不是逐行.(我正在使用旧api和defination为我的段落添加,直到我的文本文件中出现空白行.)
下面是我的代码:

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();
        }
    }  
Run Code Online (Sandbox Code Playgroud)

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

Ama*_*mar 3

你的代码对我来说工作得很好。我所做的唯一改变是将这些类作为内部类并使它们静态。

输入文件如下:

This is awesome.
WTF is this.

This is just a test.
Run Code Online (Sandbox Code Playgroud)

映射器代码如下所示:

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

    System.out.println(key+" : "+value);
}
Run Code Online (Sandbox Code Playgroud)

输出是:

0 : This is awesome. WTF is this. 
0 : This is just a test.
Run Code Online (Sandbox Code Playgroud)

我相信您不会忘记设置输入格式,但为了以防万一,请按如下方式设置:

conf.setInputFormat(NLinesInputFormat.class);
Run Code Online (Sandbox Code Playgroud)