我正在重写recordreader类的“next”方法和textinputformat类的“getrecordreader”,以便将整个段落发送到Map器,而不是逐行发送(我使用的是旧的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我能够正确地编译这个,但当我运行我的工作,我的Map器是不断运行,我不能找出问题出在哪里?
1条答案
按热度按时间iyr7buue1#
你的代码对我来说非常好。我所做的唯一改变是将这些类作为内部类,并使它们成为静态的。
输入文件如下:
Map程序代码如下所示:
结果是:
我相信您不会忘记设置输入格式,但为了以防万一,请按以下方式设置: