Hadoop 何时为Map reduce作业选择自定义输入格式

Hadoop 何时为Map reduce作业选择自定义输入格式,hadoop,mapreduce,Hadoop,Mapreduce,在使用Map Reduce编程时,我们应该何时使用自定义输入格式 假设我有一个需要逐行读取的文件,它有15列由管道分隔,我应该选择自定义输入格式吗 在这种情况下,我可以使用文本输入格式以及自定义输入格式。是的,您可以在您的情况下使用文本输入格式 需要自定义输入时,可以编写CustomInputFormat 记录读数。但在您的情况下,您不需要这样的实现 请参阅下面的CustomInputFormat示例,其中有许多这样的示例 示例:将段落作为输入记录阅读 如果您正在使用Hadoop MapRedu

在使用Map Reduce编程时,我们应该何时使用自定义输入格式

假设我有一个需要逐行读取的文件,它有15列由管道分隔,我应该选择自定义输入格式吗


在这种情况下,我可以使用文本输入格式以及自定义输入格式。

是的,您可以在您的情况下使用文本输入格式

需要自定义输入时,可以编写CustomInputFormat 记录读数。但在您的情况下,您不需要这样的实现

请参阅下面的CustomInputFormat示例,其中有许多这样的示例

示例:将段落作为输入记录阅读

如果您正在使用Hadoop MapReduce或AWS EMR,那么可能存在这样一个用例:输入文件将段落作为键值记录而不是一行(请考虑分析新闻文章评论之类的场景)。因此,如果您需要一次性将完整段落作为单个记录处理,则需要自定义
**TextInputFormat**
的默认行为,即默认情况下将每一行读取为一个输入键值对来读取完整段落,以便在MapReduce作业中进行进一步处理,而不是将一行作为输入进行处理

这要求我们创建一个定制的记录读取器,可以通过实现
类RecordReader
来实现。
next()
方法是告诉记录阅读器获取段落而不是一行。请参见以下实现,这是不言自明的:

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, isNextLineAvailable;
boolean retval;
byte space[] = {' '};
value.clear();
isNextLineAvailable = 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;
}
isNextLineAvailable = true;
}
} while (appended);

return isNextLineAvailable;
}

@Override
public long getPos() throws IOException {
return lineRecord.getPos();
}
}
确保作业配置使用自定义输入格式实现将数据读取到MapReduce作业中。将inputformat类型设置为ParagraphInputFormat非常简单,如下所示:

conf.setInputFormat(ParagraphInputFormat.class)

通过以上更改,我们可以将段落作为输入记录读取到MapReduce程序中

让我们假设输入文件如下所示:

简单的映射程序代码如下所示:

public class ParagrapghInputFormat 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);
}
}
@Override
public void map(LongWritable key, Text value, OutputCollector<Text, Text> output, Reporter reporter)
throws IOException {
System.out.println(key+" : "+value);
}
@覆盖
公共void映射(可长写键、文本值、OutputCollector输出、Reporter)
抛出IOException{
System.out.println(键+“:”+值);
}