我想在我的hadoop 2.2.0程序中解析pdf文件,我发现了这个,按照它所说的,直到现在,我有以下三个类: PDFWordCount
:包含map和reduce函数的主类(就像本机hadoop wordcount示例,但是 TextInputFormat
我用了我的 PDFInputFormat
班级。 PDFRecordReader extends RecordReader<LongWritable, Text>
:这是这里的主要工作。尤其是我把我的 initialize
函数在这里进行更多说明。
public void initialize(InputSplit genericSplit, TaskAttemptContext context)
throws IOException, InterruptedException {
System.out.println("initialize");
System.out.println(genericSplit.toString());
FileSplit split = (FileSplit) genericSplit;
System.out.println("filesplit convertion has been done");
final Path file = split.getPath();
Configuration conf = context.getConfiguration();
conf.getInt("mapred.linerecordreader.maxlength", Integer.MAX_VALUE);
FileSystem fs = file.getFileSystem(conf);
System.out.println("fs has been opened");
start = split.getStart();
end = start + split.getLength();
System.out.println("going to open split");
FSDataInputStream filein = fs.open(split.getPath());
System.out.println("going to load pdf");
PDDocument pd = PDDocument.load(filein);
System.out.println("pdf has been loaded");
PDFTextStripper stripper = new PDFTextStripper();
in =
new LineReader(new ByteArrayInputStream(stripper.getText(pd).getBytes(
"UTF-8")));
start = 0;
this.pos = start;
System.out.println("init has finished");
}
(你可以看到我的 system.out.println
用于调试。此方法转换失败 genericSplit
至 FileSplit
. 我在控制台中看到的最后一件事是:
hdfs://localhost:9000/in:0+9396432
哪个是
genericSplit.toString() PDFInputFormat extends FileInputFormat<LongWritable, Text>
:它只是创建 new PDFRecordReader
在 createRecordReader
方法。
我想知道我犯了什么错?
我需要额外的课程吗?
2条答案
按热度按时间kxeu7u2r1#
tzcvj98z2#
阅读PDF并不是那么困难,您需要扩展fileinputformat类以及recordreader。fileinputclass不能分割pdf文件,因为它们是二进制文件。
然后recordreader自己执行读取(我使用pdfbox读取PDF)。
希望这有帮助!