读写文件
背景及木:现有数据1000w单表,为压力测试准备1亿条数据。
步骤:
1.将1000w条记录,除ID外都导入到多个文件中:
i filename ( i<span >while i<span ><<span >251<span >DO
<span >SET filename<span >=CONCAT(<span >'<span >flog<span >',i,<span >'<span >.txt<span >'<span >);<span >SET <span >@STMT :<span >=CONCAT("<span >select<span >
xx
,xx
,.... <span >into outfile <span >'<span >temp/",filename,"<span >' lines terminated <span >by <span >'<span >\r\n<span >' <span >from f_log
<span >WHERE ID<span >>= ",<span >40000<span >(i<span >-<span >1)," <span >AND ID<span >< ",<span >40000<span ><span >i);<span >PREPARE STMT <span >FROM <span >@STMT<span >;
<span >EXECUTE<span > STMT;
<span >set i<span >=i<span >+<span >1<span >;
<span >end <span >while<span >;
<span >END<span >;
<span >//<span >DEliMITER
CALL createManytable();
2. 将上述多个文件合并到同一个文件,并且在第一列加入ID列:
main(String[] args) i=10000000 step=40000= file("E:/data/f_log_data.txt"( k=1;k<251;k++= file("E:/data/temp/f_log_"+k+".txt"==readfile(file,i+steP*}</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">static</span> StringBuffer read<a href="https://m.jb51.cc/tag/file/" target="_blank" >file</a>(<a href="https://m.jb51.cc/tag/file/" target="_blank" >file</a> <a href="https://m.jb51.cc/tag/file/" target="_blank" >file</a>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> start) <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">throws</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> IOException{ StringBuffer sb</span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> StringBuffer(); BufferedReader reader</span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span> BufferedReader(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> <a href="https://m.jb51.cc/tag/file/" target="_blank" >file</a>Reader(<a href="https://m.jb51.cc/tag/file/" target="_blank" >file</a>)); String <a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne</span>=""<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">while</span>(<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne != <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">n<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>l</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">){ <a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne </span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> reader.read<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne(); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span>(<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne == <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">n<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>l</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">){ </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">break</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span>(<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne.trim().equalsIg<a href="https://www.jb51.cc/tag/nor/" target="_blank" >nor</a>eCase(""<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">)){ </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">continue</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; } start</span>++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; sb.append(start</span>+"\t"+<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne.trim()+"\r\n"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); } reader.close(); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">return</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> sb;}</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">static</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span> write<a href="https://m.jb51.cc/tag/file/" target="_blank" >file</a>(<a href="https://m.jb51.cc/tag/file/" target="_blank" >file</a> <a href="https://m.jb51.cc/tag/file/" target="_blank" >file</a>,StringBuffer sb) <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">throws</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> IOException{ Buffere<a href="https://m.jb51.cc/tag/DW/" target="_blank" >DW</a>riter writer </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span> Buffere<a href="https://m.jb51.cc/tag/DW/" target="_blank" >DW</a>riter(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span> <a href="https://m.jb51.cc/tag/file/" target="_blank" >file</a>Writer(<a href="https://m.jb51.cc/tag/file/" target="_blank" >file</a>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">true</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">)); writer.write(sb.toString()); writer.close();}</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span> write<a href="https://m.jb51.cc/tag/file/" target="_blank" >file</a>11() <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">throws</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> IOException{ </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> T<a href="https://www.jb51.cc/tag/odo/" target="_blank" >odo</a> <a href="https://m.jb51.cc/tag/auto/" target="_blank" >auto</a>-generated method stub</span> Buffere<a href="https://m.jb51.cc/tag/DW/" target="_blank" >DW</a>riter writer = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span> Buffere<a href="https://m.jb51.cc/tag/DW/" target="_blank" >DW</a>riter(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span> <a href="https://m.jb51.cc/tag/file/" target="_blank" >file</a>Writer(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span> <a href="https://m.jb51.cc/tag/file/" target="_blank" >file</a>("D:/driver/data.txt"),<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">true</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">)); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">for</span>(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> i=0;i<1000000;i++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">){ </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span>(i%10==0<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">){ writer.write(</span>"赵"+(i/10)+"\t"+ (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span>)(Math.random()*100)+"\n"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); }</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span>(i%10==1<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">){ writer.write(</span>"钱"+(i/10)+"\t"+ (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span>)(Math.random()*100)+"\n"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span>(i%10==2<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">){ writer.write(</span>"孙"+(i/10)+"\t"+ (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span>)(Math.random()*100)+"\n"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); }</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span>(i%10==3<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">){ writer.write(</span>"李"+(i/10)+"\t"+ (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span>)(Math.random()*100)+"\n"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span>(i%10==4<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">){ writer.write(</span>"郑"+(i/10)+"\t"+ (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span>)(Math.random()*100)+"\n"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); }</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span>(i%10==5<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">){ writer.write(</span>"吴"+(i/10)+"\t"+ (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span>)(Math.random()*100)+"\n"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span>(i%10==6<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">){ writer.write(</span>"周"+(i/10)+"\t"+ (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span>)(Math.random()*100)+"\n"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); }</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span>(i%10==7<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">){ writer.write(</span>"王"+(i/10)+"\t"+ (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span>)(Math.random()*100)+"\n"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span>(i%10==8<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">){ writer.write(</span>"张"+(i/10)+"\t"+ (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span>)(Math.random()*100)+"\n"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); }</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span>(i%10==9<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">){ writer.write(</span>"刘"+(i/10)+"\t"+ (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span>)(Math.random()*100)+"\n"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); } } writer.close();}</span></pre>
3. 将合并后的文件导入到数据表中:
data local infile`xx`,.........................
注意事项:开始考虑使用存储过程来逐步导入到数据表中,但load data命令不能在存储过程中使用。
另外,数据的合并也可以以shell脚本完成,但习惯使用java了,因此以java来完成,显得比较复杂。不过,可以随便复习一下java的读写文件,有算不错的经历。
Q&A
时间问题:生成1亿条数据(在有索引的情况下),用时3个小时。如果使用insert语句,估计会疯掉!
总结以上是内存溢出为你收集整理的mysql 造1亿条记录的单表--大数据表全部内容,希望文章能够帮你解决mysql 造1亿条记录的单表--大数据表所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)