单条数据插入合成为批量插入

简单的修改一下表名就OK。合成速度挺快的,1分钟的事。

批量插入好处:速度优化了一些,插入200万条数据从1个多小时变成5分钟内,

用法:公司库要把数据搬到另一个表里面,

import java.io.*;

public class testGetlocalhost {

        private static final int BREACHER=1000;

        public static void main(String[] args) {

                 //需要获取的文本的路径,这里改成你的路径

                readFileByLines("C:\\Users\\23853\\Desktop\\zx_gas_data_hour.sql");

        }

        public static void readFileByLines(String fileName) {

                //表名,改成你的表名

                String table="zx_gas_data_hour";

                //拿到文本

                File file = new File(fileName);

                 BufferedReader reader = null;

                try {        

                        reader = new BufferedReader(new FileReader(file));

                        String tempString = null;

                        String insertString="";

                        int line = 1;

                        // 一次读一行,读入null时文件结束

                        //自动读取行数

                        while ((tempString = reader.readLine()) != null) {

                                //获取指定下标的内容

                                String values=tempString.substring(tempString.indexOf("("),tempString.indexOf(")")+1);

                        //每BREACHER行就封装一次数据,这里是1000

                        if(line!=0&&line%BREACHER==0){

                                String insert="INSERT INTO "+table+" VALUES "+insertString+values+";";

                                populate(insert);

                                insertString="";

                                 line++;

                        }else{

                        insertString=insertString+values+",";

                         line++;}

}

                        //剩下的合在一起

                        String end="INSERT INTO "+table+" VALUES                         "+insertString.substring(0,insertString.length()-1)+";";

                        populate(end);

                        reader.close();

                        } catch (Exception e) {

                                 e.printStackTrace();

                        } finally {

                                if (reader != null) {

                                        try { reader.close(); }

                                        catch (IOException e1) { e1.printStackTrace(); }

                                }

                                }

                        }

public static void populate(String insert){

        try {

                //将集合数据写入磁盘,这里改成你要合成的文件对应放的路径

                String newString ="C:\\Users\\23853\\Desktop\\newTest.sql";

                //new FileWriter默认是覆盖原本的内容,在参数里面加了true就是不覆盖

                FileWriter fw = new FileWriter(newString,true);

                fw.write(insert);

                fw.close();

        } catch (Exception e) {

}

} }

你可能感兴趣的:(java,intellij-idea)