我正在制作一个简单的程序,在其中使用 PDF 文件样本在我的数据库上构建全文索引。这个想法是我阅读每个 PDF 文件,提取单词并将它们存储在哈希集中。
然后,将循环中的每个单词及其文件路径添加到 MySQL 中的表中。因此,每个单词都会循环存储在每一列中,直到完成。它工作得很好。但是,当涉及到包含数千个单词的大型PDF文件时,建立索引表可能需要一些时间。换句话说,将每个单词保存到数据库中需要很长时间,因为提取单词的速度很快。
代码:
public class IndexTest {
public static void main(String[] args) throws Exception {
// write your code here
//String path ="D:\\Full Text Indexing\\testIndex\\bell2009a.pdf";
// HashSet<String> uniqueWords = new HashSet<>();
/*StopWatch stopwatch = new StopWatch();
stopwatch.start();*/
File folder = new File("D:\\PDF1");
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles) {
if (file.isFile()) {
HashSet<String> uniqueWords = new HashSet<>();
String path = "D:\\PDF1\\" + file.getName();
try (PDDocument document = PDDocument.load(new File(path))) {
if (!document.isEncrypted()) {
PDFTextStripper tStripper = new PDFTextStripper();
String pdfFileInText = tStripper.getText(document);
String lines[] = pdfFileInText.split("\\r?\\n");
for (String line : lines) {
String[] words = line.split(" ");
for (String word : words) {
uniqueWords.add(word);
}
}
// System.out.println(uniqueWords);
}
} catch (IOException e) {
System.err.println("Exception while trying to read pdf document - " + e);
}
Object[] words = uniqueWords.toArray();
String unique = uniqueWords.toString();
// System.out.println(words[1].toString());
for(int i = 1 ; i <= words.length - 1 ; i++ ) {
MysqlAccessIndex connection = new MysqlAccessIndex();
connection.readDataBase(path, words[i].toString());
}
System.out.println("Completed");
}
}
SQL连接代码:
public class MysqlAccessIndex {
public MysqlAccessIndex() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager
.getConnection("jdbc:mysql://126.32.3.178/fulltext_ltat?"
+ "user=root&password=root123");
// statement = connect.createStatement();
System.out.print("Connected");
}
public void readDataBase(String path,String word) throws Exception {
try {
statement = connect.createStatement();
System.out.print("Connected");
preparedStatement = connect
.prepareStatement("insert IGNORE into fulltext_ltat.test_text values (?, ?) ");
preparedStatement.setString(1, path);
preparedStatement.setString(2, word);
preparedStatement.executeUpdate();
// resultSet = statement
//.executeQuery("select * from fulltext_ltat.index_detail");
// writeResultSet(resultSet);
} catch (Exception e) {
throw e;
} finally {
close();
}
}
有什么建议可以改善或优化性能问题吗?