用Java更快地将txt文件读入mySQL数据库

时间:2015-03-24 04:48:36

标签: java mysql performance readfile text-parsing

我正在尝试读取超过17 000个文件(每个文件包含100到23 000行)并将数据解析为mysql数据库。问题是这样做太慢了,我不知道瓶颈在哪里。

private void readFile() { 
    PreparedStatement prepStatement = null;

    String queryInsItem = "INSERT IGNORE INTO item VALUES(?)";

    String queryInsUser = "INSERT IGNORE INTO user VALUES(?)";

    String queryInsRating = "INSERT IGNORE INTO rating VALUES(?,?,?,?)";

    try {
        int x = 1;
        int itemID = 0;
        int userID = 0;
        int rating = 0;
        java.util.Date date = null;
        java.sql.Date sqlDate = null;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
        String line = null;

        conn.setAutoCommit(false);
        System.out.println("Loading...");
          File dir = new File("src/bigdata/training_set/");
          File[] directoryListing = dir.listFiles();
          if (directoryListing != null) {
            for (File itemFile : directoryListing) {
                in = new BufferedReader(new FileReader(itemFile));
                line = in.readLine();
                itemID = Integer.parseInt(line.substring(0, line.length()-1));
                userID = 0;
                rating = 0;
                date = null;
                sqlDate = null;

                // Add to item table
                prepStatement = conn.prepareStatement(queryInsItem);
                prepStatement.setInt(1, itemID);
                prepStatement.executeUpdate();
                conn.commit();
                prepStatement.close();


                while ((line = in.readLine()) != null) {
                    // Splits the line to corresponding variables
                    userID = Integer.parseInt(line.substring(0, line.indexOf(",")));
                    rating = Integer.parseInt(line.substring(line.indexOf(",")+1, line.lastIndexOf(",")));
                    date= dateFormat.parse(line.substring(line.lastIndexOf(",")+1, line.length()));

                    sqlDate = new java.sql.Date(date.getTime());

                    // Add to user table
                    prepStatement = conn.prepareStatement(queryInsUser);
                    prepStatement.setInt(1, userID);
                    prepStatement.executeUpdate();
                    conn.commit();
                    prepStatement.close();

                    // Add to rating table
                    prepStatement = conn.prepareStatement(queryInsRating);
                    prepStatement.setInt(1, userID);
                    prepStatement.setInt(2, itemID);
                    prepStatement.setInt(3, rating);
                    prepStatement.setDate(4, sqlDate);
                    prepStatement.executeUpdate();
                    conn.commit();
                    prepStatement.close();

                }
                in.close();
                System.out.println("File " + x++ +" done.");
            }
          }


    } catch (IOException | ParseException | SQLException e) {e.printStackTrace();}

    System.out.println("Done.");
}

我首先尝试先str.split这些行,然后将其更改为indexOf / lastIndexOf,但是19486077中提到的人没有明显改善。提到同一线程中的其他人使用Threads,但在我的情况下这是一种正确的方法吗?

以下是原始数据的片段:

5317:
2354291,3,2005-07-05
185150,2,2005-07-05
868399,3,2005-07-05

以上意思是:

[item_id]:
[user_id],[rating],[date]
[user_id],[rating],[date]
[user_id],[rating],[date]

1 个答案:

答案 0 :(得分:2)

  • 如果您对这些表格AUTO_INCREMENT PRIMARY KEY,请注意INSERT IGNORE会像疯了一样烧掉ID。
  • “批量”插入。如果你收集100-1000行,用这些行构建一个INSERT,然后执行该语句,INSERTion将以10倍的速度运行。
  • 不要尝试一次批量处理23,000行,您可能会遇到一些问题(很难预测会出现什么问题)。
  • 另一方面,如果您可以针对这些文件执行LOAD DATA,则可以删除所有解析代码!它的运行速度至少与批量插入一样快。