Я использую PDFBox для извлечения текста из файла PDF и вставки этого текста в виде слов в таблицу базы данных.
Он работает отлично.Однако, когда я пытаюсь загрузить файл большего размера, который содержит 10 миллионов слов, он выдает OutOfMemoryError: Java heap space
.
. Поэтому я вместо обработки всего файла обрабатываю файл постранично, поэтомудолжен занимать меньше памяти.
Вот как это выглядит:
метод processtext:
public void processText(String text) throws SQLException {
String lines[] = text.split("\\r?\\n");
for (String line : lines) {
String[] words = line.split(" ");
String sql="insert IGNORE into test.indextable values (?,?);";
preparedStatement = con1.prepareStatement(sql);
int i=0;
for (String word : words) {
// check if one or more special characters at end of string then remove OR
// check special characters in beginning of the string then remove
// insert every word directly to table db
word=word.replaceAll("([\\W]+$)|(^[\\W]+)", "");
preparedStatement.setString(1, path1);
preparedStatement.setString(2, word);
preparedStatement.addBatch();
i++;
if (i % 1000 == 0) {
preparedStatement.executeBatch();
System.out.print("Add Thousand");
}
}
if (i > 0) {
preparedStatement.executeBatch();
System.out.print("Add Remaining");
}
}
preparedStatement.close();
System.out.println("Successfully commited changes to the database!");
}
метод index (), вызывающий вышеуказанный метод:
public void index() throws Exception {
// Connection con1 = con.connect();
try {
// Connection con1=con.connect();
// Connection con1 = con.connect();
Statement statement = con1.createStatement();
ResultSet rs = statement.executeQuery("select * from filequeue where Status='Active' LIMIT 5");
while (rs.next()) {
// get the filepath of the PDF document
path1 = rs.getString(2);
int getNum = rs.getInt(1);
// while running the process, update status : Processing
//updateProcess_DB(getNum);
Statement test = con1.createStatement();
test.executeUpdate("update filequeue SET STATUS ='Processing' where UniqueID="+getNum);
try {
// call the index function
/*Indexing process = new Indexing();
process.index(path1);*/
PDDocument document = PDDocument.load(new File(path1));
if (!document.isEncrypted()) {
PDFTextStripper tStripper = new PDFTextStripper();
for(int p=1; p<=document.getNumberOfPages();++p) {
tStripper.setStartPage(p);
tStripper.setEndPage(p);
String pdfFileInText = tStripper.getText(document);
processText(pdfFileInText);
}
}
Didснова протестируйте этот файл, и кажется, что он все равно выдает ту же ошибку.У меня явно нет идей относительно того, как я могу это исправить.