是“加载数据” MySQL 和 Java 可能没有文件(即在内存中)吗?

发布于 2024-09-17 21:39:13 字数 277 浏览 6 评论 0原文

我正在优化将约 10TB 数据导入 MySQL 数据库的过程。目前,我可以在当前笔记本电脑上在大约 14 分钟内导入 2.9GB(+0.8GB 索引)。该过程包括读取数据文件(Oracle“.dat”导出)、解析数据、将数据写入 CSV 文件并对其执行“LOAD DATA LOCAL”sql 命令。

是否可以提高导入速度(无需更改硬件)?有没有办法去掉将文件写入文件系统并让MySQL再次读取的步骤。是否可以将内存中的数据直接传输到 MySQL(例如,通过 JDBC 驱动程序)?

预先非常感谢, 约尔格.

I'm in the process of optimizing an import of ~10TB of Data into a MySQL database. Currently, I can import 2.9GB (+0.8GB index) in about 14 minutes on a current laptop. The process includes reading a data file (Oracle ".dat" export), parsing the data, writing the data into a CSV file and executing the "LOAD DATA LOCAL" sql command on it.

Is it possible to increase the import speed (without hardware changes)? Is there a way to remove the step of writing a file to the file system and letting MySQL read it again. Is it possible to stream the data in memory directly to MySQL (e.g., via the JDBC driver)?

Many thanks in advance,
Joerg.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琉璃繁缕 2024-09-24 21:39:13

似乎从 MySQL Connector/J JDBC 驱动程序版本 5.1.3 开始,您可以使用 com.mysql.jdbc.Statement.setLocalInfileInputStream() 方法,在 Java 代码内部,用于“管道”内存格式化字符串/文本到“LOAD DATA INFILE”调用。这意味着您不必必须写出临时文件并从内存中重新读回。请参考:

http://dev .mysql.com/doc/refman/5.1/en/connector-j-reference-implementation-notes.html(页面底部)

此过程也概述于帖子

http://jeffrick.com/2010/03/23/bulk-insert-into-a-mysql-database

O 'reilly 制作了 涵盖 MySQL/JDBC 性能宝石的 PDF ,指的是这个。

还提到了它与 Hadoop 的用法(高级Java 主题)。

希望这一切都有帮助。

干杯

丰富

It seems that from MySQL Connector/J JDBC driver version 5.1.3 onwards, you can hook up an InputStream reference, using com.mysql.jdbc.Statement.setLocalInfileInputStream() method, internally within your Java code, to 'pipe' your in-memory formatted string/text to the 'LOAD DATA INFILE' call. This means you do not have to write out and re-read a temporary file back from memory. Please refer to:

http://dev.mysql.com/doc/refman/5.1/en/connector-j-reference-implementation-notes.html (bottom of page)

The process is also outlined in this post:

http://jeffrick.com/2010/03/23/bulk-insert-into-a-mysql-database

O'reilly produced a PDF covering MySQL/JDBC performance gems, which refers to this.

There is also mention of it's usage with Hadoop (advanced Java topic).

Hope this all helps.

Cheers

Rich

度的依靠╰つ 2024-09-24 21:39:13

实际的工作代码很难获得,所以这里有一些:

@Test
public void bulkInsert() throws SQLException {
    try(com.mysql.jdbc.Connection conn = (com.mysql.jdbc.Connection) dao.getDataSource().getConnection()) {

        conn.setAllowLoadLocalInfile(true);

        try(com.mysql.jdbc.Statement stmt = (com.mysql.jdbc.Statement) conn.createStatement()) {

            stmt.execute("create temporary table BasicDbTest_1 (phone integer)");

            String data = "8675309\n";
            stmt.setLocalInfileInputStream(new ByteArrayInputStream(data.getBytes()));

            stmt.execute("load data local infile '' into table BasicDbTest_1");

            try(ResultSet rs = stmt.executeQuery("select phone from BasicDbTest_1")) {
                Assert.assertTrue(rs.next());
                Assert.assertEquals(rs.getInt(1), 8675309);                 
            }
        }
    }
}

Actual working code for this was hard to come by, so here's some:

@Test
public void bulkInsert() throws SQLException {
    try(com.mysql.jdbc.Connection conn = (com.mysql.jdbc.Connection) dao.getDataSource().getConnection()) {

        conn.setAllowLoadLocalInfile(true);

        try(com.mysql.jdbc.Statement stmt = (com.mysql.jdbc.Statement) conn.createStatement()) {

            stmt.execute("create temporary table BasicDbTest_1 (phone integer)");

            String data = "8675309\n";
            stmt.setLocalInfileInputStream(new ByteArrayInputStream(data.getBytes()));

            stmt.execute("load data local infile '' into table BasicDbTest_1");

            try(ResultSet rs = stmt.executeQuery("select phone from BasicDbTest_1")) {
                Assert.assertTrue(rs.next());
                Assert.assertEquals(rs.getInt(1), 8675309);                 
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文