【发布时间】:2018-10-26 02:46:46
【问题描述】:
我正在 MYSQL 中的一个表上构建一个索引表(反转文件)。它的工作方式是从文件中提取所有单词并将它们存储到哈希集,然后将单词一一插入到我的数据库表中。
它工作得很好,我知道倒排文件确实需要一些时间来建立索引表。我正在尝试优化表的索引时间,并且正在考虑使用多线程。它会加快性能吗?
但是,我不太确定如何将它与我当前的程序集成,因为我是多线程的新手。
代码:
public static void main(String[] args) throws Exception {
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();
MysqlAccessIndex connection = new MysqlAccessIndex();
for(int i = 1 ; i <= words.length - 1 ; i++ ) {
connection.readDataBase(path, words[i].toString());
}
System.out.println("Completed");
}
}
MySQL 连接:
public class MysqlAccessIndex {
public Connection connect = null;
public Statement statement = null;
public PreparedStatement preparedStatement = null;
public ResultSet resultSet = null;
public void connect() throws Exception {
// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager
.getConnection("jdbc:mysql://126.32.3.20/fulltext_ltat?"
+ "user=root&password=root");
// Statements allow to issue SQL queries to the database
statement = connect.createStatement();
System.out.print("Connected");
}
public MysqlAccessIndex() throws Exception {
connect();
}
public void readDataBase(String path,String word) throws Exception {
try {
// Result set get the result of the SQL query
// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager
.getConnection("jdbc:mysql://126.32.3.20/fulltext_ltat?"
+ "user=root&password=root");
// Statements allow to issue SQL queries to the database
statement = connect.createStatement();
System.out.print("Connected");
// Result set get the result of the SQL query
preparedStatement = connect
.prepareStatement("insert IGNORE into fulltext_ltat.indextable values (default,?, ?) ");
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();
}
}
我将不胜感激。
【问题讨论】:
-
如果你的 Java 程序可以被分解成更小的部分,每个部分都可以独立运行,那么它可能会受益于多线程。是这样吗?
-
@TimBiegeleisen 如果不分解成更小的部分,创建几个线程来运行我的 main 方法会有所帮助吗?
-
它会工作还是帮助?不是同一个问题。
-
在循环中的代码
readDataBase中,您每次都在连接 - 这将非常慢, - 考虑使用 DBCP -
一个提示:您可以立即开始向数据库添加单词,您无需等待文本剥离器完成,只需将单词传递给另一个线程,并保持恒定的数据库连接在另一个线程上。