重写RecordReader类的方法“下一步”和TextInputFormat类的“getRecordReader”以便发送整个段落到映射器而不是逐行。 (我用旧的API和认定中对我的款追加至一个空行来在我的文本文件的时间。)
下面是我的代码:覆盖RecordReader一次而不是行
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.我能够正确编译,但是当我运行我的作业时,我的映射器不断运行,我无法弄清楚问题出在哪里?
您是否尝试过仅输入一个段落? – Amar 2013-03-25 09:02:21
我认为你有一个bug;当你穿越分裂时你会得到额外的段落。我认为你需要区分从0开始的分割和其他分割。从0开始的第一行开始一段,但以行开头的分割不应该开始一个新段落。 (通常情况下,你已经读过一个拆分边界,所以如果你的拆分文件有连续段落的行,它们将会被前一个拆分文件所发出)。我错过了什么吗? – 2017-04-15 22:10:56