Java一次性查询几十万几百万数据解决办法

  Java查询一次性查询几十万,几百万数据解决办法。

  很早的时候写工具用的一个办法,当时是用来把百万数据打包成rar文件。

  所以用了个笨办法。 希望高手指导一下,有什么好方法没有啊。

  1、先批量查出所有数据,例子中是一万条一批。

  2、在查出数据之后把每次的数据按一定规则存入本地文件。

  3、获取数据时,通过批次读取,获得大批量数据。

  以下是查询数据库。按批次查询

  

    publicstaticvoidgetMonthDataList(){ ResultSetrs=null; Statementstat=null; Connectionconn=null; List<DataBean>list=newArrayList<DataBean>(); try{ conn=createConnection(); if(conn!=null){ SimpleDateFormatsdf=newSimpleDateFormat(“yyyy-MM-dd”); SimpleDateFormattimesdf=newSimpleDateFormat(“yyyy-MM-ddHH:mm:ss”); StringnowDate=sdf.format(newDate()); Config.lasttimetext=timesdf.format(newDate()); StringlastDate=sdf.format(CreateData.addDaysForDate(newDate(),30)); stat=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); intlastrow=0; intdatanum=0; Stringcountsql=”SELECTcount(a.id)FROMtrip_special_flighta”+ “wherea.dpt_date>=to_date(‘”+nowDate+”‘,’yyyy-mm-dd’)”+ “anda.dpt_date<=to_date(‘”+lastDate+”‘,’yyyy-mm-dd’)andrownum>”+lastrow+”orderbya.get_timedesc”; rs=stat.executeQuery(countsql); while(rs.next()){ datanum=rs.getInt(1); } intonerun=10000; intrunnum=datanum%onerun==0?(datanum/onerun):(datanum/onerun)+1; for(intr=0;r<runnum;r++){ System.out.println(“getMonthDataList–“+datanum+”开始查询第”+(r+1)+”批数据”); Stringsql=”SELECT*FROM(SELECTrownumrn,a.dpt_code,a.arr_code,a.dpt_date,a.airways,a.flight,”+ “a.cabin,a.priceFROMtrip_special_flighta”+ “wherea.dpt_date>=to_date(‘”+nowDate+”‘,’yyyy-mm-dd’)”+ “anda.dpt_date<=to_date(‘”+lastDate+”‘,’yyyy-mm-dd’)orderbyrownumasc)WHERErn>”+lastrow; stat.setMaxRows(onerun); stat.setFetchSize(1000); rs=stat.executeQuery(sql); Stringtext=””; inti=1; while(rs.next()){ text+=rs.getString(2)+”|”+rs.getString(3)+”|”+rs.getDate(4)+”|”+rs.getString(5)+”|”+rs.getString(6)+”|”+rs.getString(7)+”|”+rs.getString(8)+”||”; if(i%1000==0){ FileUtil.appendToFile(Config.tempdatafile,text); text=””; } i++; } if(text.length()>10){ FileUtil.appendToFile(Config.tempdatafile,text); } lastrow+=onerun; } } }catch(Exceptione){ e.printStackTrace(); }finally{ closeAll(rs,stat,conn); } }

  —–java一次性查询几十万,几百万数据解决办法

  存入临时文件之后,再用读取大量数据文件方法。

  设置缓存大小BUFFER_SIZE ,Config.tempdatafile是文件地址。

  

    packagecom.yjf.util; importjava.io.File; importjava.io.RandomAccessFile; importjava.nio.MappedByteBuffer; importjava.nio.channels.FileChannel; publicclassTest{ publicstaticvoidmain(String[]args)throwsException{ finalintBUFFER_SIZE=0x300000;//缓冲区为3M Filef=newFile(Config.tempdatafile); //来源博客;intlen=0; Longstart=System.currentTimeMillis(); for(intz=8;z>0;z–){ MappedByteBufferinputBuffer=newRandomAccessFile(f,”r”) .getChannel().map(FileChannel.MapMode.READ_ONLY, f.length()*(z-1)/8,f.length()*1/8); byte[]dst=newbyte[BUFFER_SIZE];//每次读出3M的内容 for(intoffset=0;offset<inputBuffer.capacity();offset+=BUFFER_SIZE){ if(inputBuffer.capacity()-offset>=BUFFER_SIZE){ for(inti=0;i<BUFFER_SIZE;i++) dst[i]=inputBuffer.get(offset+i); }else{ for(inti=0;i<inputBuffer.capacity()-offset;i++) dst[i]=inputBuffer.get(offset+i); } intlength=(inputBuffer.capacity()%BUFFER_SIZE==0)?BUFFER_SIZE :inputBuffer.capacity()%BUFFER_SIZE; len+=newString(dst,0,length).length(); System.out.println(newString(dst,0,length).length()+”-“+(z-1)+”-“+(8-z+1)); } } System.out.println(len); longend=System.currentTimeMillis(); System.out.println(“读取文件文件花费:”+(end-start)+”毫秒”); } }

  读取大量数据文件方法。

莫找借口失败,只找理由成功。(不为失败找理由,要为成功找方法

Java一次性查询几十万几百万数据解决办法

相关文章:

你感兴趣的文章:

标签云: