我最近开始使用 Hadoop,刚刚创建了自己的输入格式来处理 pdf。
出于某种原因,我的自定义 RecordReader 类没有调用它的初始化方法。 (用sysout检查了一下,因为我还没有设置调试环境)
我在 Windows 7 32 位上运行 hadoop 2.2.0。使用yarn jar进行调用,因为hadoop jar在Windows下被窃听......
import ...
public class PDFInputFormat extends FileInputFormat<Text, Text>
{
@Override
public RecordReader<Text, Text> getRecordReader(InputSplit arg0,
JobConf arg1, Reporter arg2) throws IOException
{
return new PDFRecordReader();
}
public static class PDFRecordReader implements RecordReader<Text, Text>
{
private FSDataInputStream fileIn;
public String fileName=null;
HashSet<String> hset=new HashSet<String>();
private Text key=null;
private Text value=null;
private byte[] output=null;
private int position = 0;
@Override
public Text createValue() {
int endpos = -1;
for (int i = position; i < output.length; i++){
if (output[i] == (byte) '\n') {
endpos = i;
}
}
if (endpos == -1) {
return new Text(Arrays.copyOfRange(output,position,output.length));
}
return new Text(Arrays.copyOfRange(output,position,endpos));
}
@Override
public void initialize(InputSplit genericSplit, TaskAttemptContext job) throws
IOException, InterruptedException
{
System.out.println("initialization is called");
FileSplit split=(FileSplit) genericSplit;
Configuration conf=job.getConfiguration();
Path file=split.getPath();
FileSystem fs=file.getFileSystem(conf);
fileIn= fs.open(split.getPath());
fileName=split.getPath().getName().toString();
System.out.println(fileIn.toString());
PDDocument docum = PDDocument.load(fileIn);
ByteArrayOutputStream boss = new ByteArrayOutputStream();
OutputStreamWriter ow = new OutputStreamWriter(boss);
PDFTextStripper stripper=new PDFTextStripper();
stripper.writeText(docum, ow);
ow.flush();
output = boss.toByteArray();
}
}
}
最佳答案
正如我昨晚想出的那样,我可能会帮助其他人:
RecordReader 是 Hadoop 的一个已弃用的接口(interface) (hadoop.common.mapred),它实际上并不包含初始化方法,这解释了为什么它不会被自动调用。
扩展 hadoop.common.mapreduce 中的 RecordReader 类确实可以让您扩展该类的初始化方法。
关于java - 自定义 RecordReader 初始化未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21855690/