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

开发 后端
Java查询一次性查询几十万,几百万数据解决办法。很早的时候写工具用的一个办法,当时是用来把百万数据打包成rar文件。所以用了个笨办法。 希望高手指导一下,有什么好方法没有啊。

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

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

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

  1. 先批量查出所有数据,例子中是一万条一批。
  2. 在查出数据之后把每次的数据按一定规则存入本地文件。
  3. 获取数据时,通过批次读取,获得大批量数据。此方法参见:http://yijianfengvip.blog.163.com/blog/static/175273432201191354043148/

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

  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.  
  57.     } 

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

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

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

来源博客 http://yijianfengvip.blog.163.com/blog/static/175273432201191354043148/

  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.  

读取大量数据文件方法。

原文链接:http://blog.csdn.net/yjflinchong/article/details/7287648

【编辑推荐】

  1. 有可能挑战Java优势的四种技术
  2. Think in Java之斐波那契数列
  3. Java的poi技术读取和导入Excel
  4. Java SE 6生命将在今年11月终结
  5. Jodd 3.3.2发布 Java常用工具包
责任编辑:林师授 来源: yjflinchong的博客
相关推荐

2024-02-28 08:18:13

Java日志项目

2022-06-29 08:22:05

NFTWeb3元宇宙

2012-02-01 16:48:54

后门Putty

2010-11-24 16:32:50

2019-11-27 10:29:40

CDNzipf缓存

2013-04-17 09:16:37

2014-08-04 14:38:25

LinuxToken

2019-08-06 09:21:45

2021-08-12 09:48:21

Webpack Loa工具Webpack

2023-09-26 07:11:15

KubernetesJob节点

2012-06-28 09:53:11

2021-07-18 07:42:21

间谍软件漏洞网络攻击

2012-09-18 15:04:31

Office 2013微软

2009-12-25 14:46:53

Windows 7文件关联

2014-03-06 15:16:18

安全管理linux安全

2018-11-01 14:30:09

Redis数据库面试题

2021-06-14 09:31:42

数据泄漏勒索攻击网络攻击

2020-07-08 15:30:29

Java面试题代码

2013-04-26 16:05:44

2021-07-26 09:56:19

AI 数据人工智能
点赞
收藏

51CTO技术栈公众号