[HADOOP] 지도 축소 프로그램에서 PDF 파일을 구문 분석하는 방법은 무엇입니까?
HADOOP지도 축소 프로그램에서 PDF 파일을 구문 분석하는 방법은 무엇입니까?
내 hadoop 2.2.0 프로그램에서 PDF 파일을 구문 분석하고 싶습니다. 그리고 이것을 발견하고 그것이 말하는 것과 다음과 같은 세 가지 클래스가 있습니다.
내 실수는 무엇인지 알고 싶습니까?
추가 수업이 필요한가요?
해결법
-
==============================
1.PDF 읽기는 그다지 어렵지 않습니다. FileReadputFormat과 RecordReader 클래스를 확장해야합니다. FileInputClass는 바이너리이므로 PDF 파일을 분할해서는 안됩니다.
PDF 읽기는 그다지 어렵지 않습니다. FileReadputFormat과 RecordReader 클래스를 확장해야합니다. FileInputClass는 바이너리이므로 PDF 파일을 분할해서는 안됩니다.
public class PDFInputFormat extends FileInputFormat<Text, Text> { @Override public RecordReader<Text, Text> createRecordReader(InputSplit split, TaskAttemptContext context) throws IOException, InterruptedException { return new PDFLineRecordReader(); } // Do not allow to ever split PDF files, even if larger than HDFS block size @Override protected boolean isSplitable(JobContext context, Path filename) { return false; } }
RecordReader는 독서 자체를 수행합니다 (PDF를 읽으려면 PDFBox를 사용하고 있습니다).
public class PDFLineRecordReader extends RecordReader<Text, Text> { private Text key = new Text(); private Text value = new Text(); private int currentLine = 0; private List<String> lines = null; private PDDocument doc = null; private PDFTextStripper textStripper = null; @Override public void initialize(InputSplit split, TaskAttemptContext context) throws IOException, InterruptedException { FileSplit fileSplit = (FileSplit) split; final Path file = fileSplit.getPath(); Configuration conf = context.getConfiguration(); FileSystem fs = file.getFileSystem(conf); FSDataInputStream filein = fs.open(fileSplit.getPath()); if (filein != null) { doc = PDDocument.load(filein); // Konnte das PDF gelesen werden? if (doc != null) { textStripper = new PDFTextStripper(); String text = textStripper.getText(doc); lines = Arrays.asList(text.split(System.lineSeparator())); currentLine = 0; } } } // False ends the reading process @Override public boolean nextKeyValue() throws IOException, InterruptedException { if (key == null) { key = new Text(); } if (value == null) { value = new Text(); } if (currentLine < lines.size()) { String line = lines.get(currentLine); key.set(line); value.set(""); currentLine++; return true; } else { // All lines are read? -> end key = null; value = null; return false; } } @Override public Text getCurrentKey() throws IOException, InterruptedException { return key; } @Override public Text getCurrentValue() throws IOException, InterruptedException { return value; } @Override public float getProgress() throws IOException, InterruptedException { return (100.0f / lines.size() * currentLine) / 100.0f; } @Override public void close() throws IOException { // If done close the doc if (doc != null) { doc.close(); } }
희망이 도움이!
-
==============================
2.
package com.sidd.hadoop.practice.pdf; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import com.sidd.hadoop.practice.input.pdf.PdfFileInputFormat; import com.sidd.hadoop.practice.output.pdf.PdfFileOutputFormat; public class ReadPdfFile { public static class MyMapper extends Mapper<LongWritable, Text, LongWritable, Text> { public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { // context.progress(); context.write(key, value); } } public static class MyReducer extends Reducer<LongWritable, Text, LongWritable, Text> { public void reduce(LongWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException { if (values.iterator().hasNext()) { context.write(key, values.iterator().next()); } else { context.write(key, new Text("")); } } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = new Job(conf, "Read Pdf"); job.setJarByClass(ReadPdfFile.class); job.setMapperClass(MyMapper.class); job.setReducerClass(MyReducer.class); job.setOutputKeyClass(LongWritable.class); job.setOutputValueClass(Text.class); job.setInputFormatClass(PdfFileInputFormat.class); job.setOutputFormatClass(PdfFileOutputFormat.class); removeDir(args[1], conf); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } public static void removeDir(String path, Configuration conf) throws IOException { Path output_path = new Path(path); FileSystem fs = FileSystem.get(conf); if (fs.exists(output_path)) { fs.delete(output_path, true); } } }
from https://stackoverflow.com/questions/20758956/how-to-parse-pdf-files-in-map-reduce-programs by cc-by-sa and MIT license
'HADOOP' 카테고리의 다른 글
[HADOOP] multidelimeter를 사용하여 하이브에 데이터 삽입 (0) | 2019.06.26 |
---|---|
[HADOOP] hadoop에서 스택 오버플로 오류 발생 (0) | 2019.06.26 |
[HADOOP] 왜 YARN에 mapreduce.jobtracker.address 구성이 있습니까? (0) | 2019.06.26 |
[HADOOP] HBase에서 SingleColumnValueFilter가있는 사용자 정의 비교기를 어떻게 사용합니까? (0) | 2019.06.26 |
[HADOOP] MapReduce 작업이 원격 데이터에 액세스하는 것을 제한 할 수 있습니까? (0) | 2019.06.26 |