Mysql的批量导入

Posted lys-lyy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql的批量导入相关的知识,希望对你有一定的参考价值。

1. 下面的是java代码的实现
技术图片
package cn.enjoy;


import org.junit.Test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;


public class InsertDemo 

    private static String user = "root";
    private static String pass = "123456";
    private static String URL = "jdbc:mysql://127.0.0.1:3306/test";


    @Test
    public void test1() throws  Exception
        BufferedReader br = new BufferedReader(new FileReader("D:\\\\product_info.sql"));
        Connection conn = DriverManager.getConnection(URL , user, pass);

        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        br.lines().forEach(sql->
            try 
                PreparedStatement ps = conn.prepareStatement(sql);
                ps.executeUpdate();
                ps.close();
             catch (Exception e) 
                e.printStackTrace();
            
        );
        br.close();
        conn.close();

        LocalDateTime now2 = LocalDateTime.now();
        System.out.println(now2);
    


    int i=0;
    @Test
    public void test2() throws  Exception
        BufferedReader br = new BufferedReader(new FileReader("D:\\\\product_info.sql"));
        Connection conn = DriverManager.getConnection(URL , user, pass);

        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        conn.setAutoCommit(false);

        br.lines().forEach(sql->
            try 
                PreparedStatement ps = conn.prepareStatement(sql);
               ps.addBatch();
                if((i%2000)!=0 && i<=2097152) 
                    i++;
                else 
                    ps.executeBatch();
                    conn.commit();
                    i=0;
                

             catch (Exception e) 
                e.printStackTrace();
            
        );
        br.close();
        conn.close();

        LocalDateTime now2 = LocalDateTime.now();
        System.out.println(now2);
    


    String str = "INSERT INTO `product_info` VALUES ";

    @Test
    public void test3() throws  Exception
        BufferedReader br = new BufferedReader(new FileReader("D:\\\\product_info.sql"));
        Connection conn = DriverManager.getConnection(URL , user, pass);

        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        conn.setAutoCommit(false);

        br.lines().forEach(sql->
            try 
                str =str + sql.split("VALUES")[1].replace(";",",");

                if((i%2000)!=0 && i<=2097152) 
                    i++;
                else 
                    i++;
                    str= str.substring(0,str.length()-1);
                   // System.out.println(str);
                    PreparedStatement ps = conn.prepareStatement(str);
                    ps.executeUpdate();
                    str = "INSERT INTO `product_info` VALUES ";
                    conn.commit();
                

             catch (Exception e) 
                e.printStackTrace();
            
        );
        br.close();
        conn.close();

        LocalDateTime now2 = LocalDateTime.now();
        System.out.println(now2);

    
View Code

技术图片

2. SQL实现 

 

LOAD DATA INFLIE;
使用LOAD DATA INFLIE ,比一般的insert语句快20倍

select * into OUTFILE D:\\\\product.txt from product_info

load data INFILE D:\\\\product.txt into table product_info

 

以上是关于Mysql的批量导入的主要内容,如果未能解决你的问题,请参考以下文章

数据分析之MySQL——批量导入数据

mysql 批量导入数据

java大批量数据导入(MySQL)

MySQL批量导入Excel数据

如何把csv文件批量导入到mysql数据库

mysql 如何提高批量导入的速度