我最近开始搞乱 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();
}
}
}