qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Java一次性查詢幾十萬 幾百萬數據解決辦法

            Java查詢一次性查詢幾十萬,幾百萬數據解決辦法。

            很早的時候寫工具用的一個辦法,當時是用來把百萬數據打包成rar文件。

            所以用了個笨辦法。 希望高手指導一下,有什么好方法沒有啊。

            1、先批量查出所有數據,例子中是一萬條一批。

            2、在查出數據之后把每次的數據按一定規則存入本地文件。

            3、獲取數據時,通過批次讀取,獲得大批量數據。

            以下是查詢數據庫。按批次查詢

          1. public static void  getMonthDataList() {  
          2.         ResultSet rs = null;  
          3.         Statement stat = null;  
          4.         Connection conn = null;  
          5.         List<DataBean> list = new ArrayList<DataBean>();  
          6.         try {  
          7.             conn = createConnection();  
          8.             if(conn!=null){  
          9.                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
          10.                 SimpleDateFormat timesdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
          11.                 String nowDate = sdf.format(new Date());  
          12.                 Config.lasttimetext = timesdf.format(new Date());  
          13.                 String lastDate = sdf.format(CreateData.addDaysForDate(new Date(), 30));  
          14.                 stat = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);  
          15.                 int lastrow = 0;  
          16.                 int datanum = 0;  
          17.                 String countsql = "SELECT count(a.id) FROM trip_special_flight a" +  
          18.                 " where a.dpt_date >= to_date('"+nowDate+"','yyyy-mm-dd') " +  
          19.                 "and a.dpt_date <= to_date('"+lastDate+"','yyyy-mm-dd') and rownum>"+lastrow+" order by a.get_time  desc";  
          20.                 rs = stat.executeQuery(countsql);  
          21.                 while (rs.next()) {  
          22.                     datanum = rs.getInt(1);  
          23.                 }  
          24.                 int onerun = 10000;  
          25.                 int runnum = datanum%onerun==0?(datanum/onerun):(datanum/onerun)+1;  
          26.                 for(int r =0;r<runnum;r++){  
          27.                     System.out.println("getMonthDataList--"+datanum+" 開始查詢第"+(r+1)+"批數據");  
          28.                     String sql = "SELECT * FROM  (SELECT rownum rn, a.dpt_code, a.arr_code,a.dpt_date,a.airways,a.flight," +  
          29.                     "a.cabin,a.price FROM trip_special_flight a" +  
          30.                     " where a.dpt_date >= to_date('"+nowDate+"','yyyy-mm-dd') " +  
          31.                     "and a.dpt_date <= to_date('"+lastDate+"','yyyy-mm-dd')  order by rownum  asc) WHERE rn > "+lastrow;  
          32.                     stat.setMaxRows(onerun);  
          33.                     stat.setFetchSize(1000);  
          34.                     rs = stat.executeQuery(sql);  
          35.                     String text = "";  
          36.                     int i = 1;  
          37.                     while (rs.next()) {  
          38.                         text += rs.getString(2)+"|"+rs.getString(3)+"|"+rs.getDate(4)+"|"+rs.getString(5)+"|"+rs.getString(6)+"|"+rs.getString(7)+"|"+rs.getString(8)+"||";  
          39.                         if(i%1000==0){  
          40.                             FileUtil.appendToFile(Config.tempdatafile, text);  
          41.                             text = "";  
          42.                         }  
          43.                         i++;  
          44.                     }  
          45.                     if(text.length()>10){  
          46.                         FileUtil.appendToFile(Config.tempdatafile, text);  
          47.                     }  
          48.                     lastrow+=onerun;  
          49.                 }  
          50.             }  
          51.         } catch (Exception e) {  
          52.             e.printStackTrace();  
          53.         } finally {  
          54.             closeAll(rs, stat, conn);  
          55.         }  

          56.     }
           -----java一次性查詢幾十萬,幾百萬數據解決辦法

            存入臨時文件之后,再用讀取大量數據文件方法。

            設置緩存大小BUFFER_SIZE ,Config.tempdatafile是文件地址。

          1. package com.yjf.util;  
          2. import java.io.File;  
          3. import java.io.RandomAccessFile;  
          4. import java.nio.MappedByteBuffer;  
          5. import java.nio.channels.FileChannel;  
          6.  
          7. public class Test {  
          8.     public static void main(String[] args) throws Exception {  
          9.         final int BUFFER_SIZE = 0x300000; // 緩沖區為3M  
          10.         File f = new File(Config.tempdatafile);  
          11.  //  來源博客http://yijianfengvip.blog.163.com/blog/static/175273432201191354043148/  
          12.         int len = 0;  
          13.         Long start = System.currentTimeMillis();  
          14.         for (int z = 8; z >0; z--) {  
          15.             MappedByteBuffer inputBuffer = new RandomAccessFile(f, "r")  
          16.                     .getChannel().map(FileChannel.MapMode.READ_ONLY,  
          17.                             f.length() * (z-1) / 8, f.length() * 1 / 8);  
          18.             byte[] dst = new byte[BUFFER_SIZE];// 每次讀出3M的內容  
          19.             for (int offset = 0; offset < inputBuffer.capacity(); offset += BUFFER_SIZE) {  
          20.                 if (inputBuffer.capacity() - offset >= BUFFER_SIZE) {  
          21.                     for (int i = 0; i < BUFFER_SIZE; i++)  
          22.                         dst[i] = inputBuffer.get(offset + i);  
          23.                 } else {  
          24.                     for (int i = 0; i < inputBuffer.capacity() - offset; i++)  
          25.                         dst[i] = inputBuffer.get(offset + i);  
          26.                 }  
          27.                 int length = (inputBuffer.capacity() % BUFFER_SIZE == 0) ? BUFFER_SIZE  
          28.                         : inputBuffer.capacity() % BUFFER_SIZE;  
          29.                 len += new String(dst, 0, length).length();  
          30.                 System.out.println(new String(dst, 0, length).length()+"-"+(z-1)+"-"+(8-z+1));  
          31.             }  
          32.         }  
          33.         System.out.println(len);  
          34.         long end = System.currentTimeMillis();  
          35.         System.out.println("讀取文件文件花費:" + (end - start) + "毫秒");  
          36.     }  
          37.  
          38. }

            讀取大量數據文件方法。

          posted on 2012-02-29 09:34 順其自然EVO 閱讀(214) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2012年2月>
          2930311234
          567891011
          12131415161718
          19202122232425
          26272829123
          45678910

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 家居| 应用必备| 广汉市| 蕲春县| 浮梁县| 普格县| 井研县| 瑞昌市| 洱源县| 河南省| 城口县| 玉溪市| 海晏县| 延庆县| 乐昌市| 贡嘎县| 邯郸县| 邯郸市| 吉木萨尔县| 乐山市| 凌云县| 武隆县| 洮南市| 偏关县| 吴忠市| 贵港市| 江陵县| 牙克石市| 苍南县| 鄂托克旗| 墨玉县| 绵竹市| 尚志市| 武宣县| 朝阳县| 远安县| 班戈县| 商洛市| 化德县| 邵武市| 兴安县|