当前位置:网站首页>SQL export CSV data, unlimited number of entries

SQL export CSV data, unlimited number of entries

2022-06-24 19:34:00 God__ is__ a__ girl

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import com.yren.datasql.config.MyDataSource;
import com.yren.datasql.util.common.MyUtil;
import org.springframework.jdbc.core.JdbcTemplate;
import java.io.RandomAccessFile;
import java.util.List;
import java.util.Map;

public class ExportCSV {
    

    public static void main(String[] args) throws Exception {
    
        export("my_csv_file");
    }
    
    private static void export(String tb_name) throws Exception {
    
        JdbcTemplate jdbcTemplate = new JdbcTemplate(MyDataSource.getDataSource());
        TimeInterval timer = DateUtil.timer();
        DateTime start = DateUtil.parse("2020-01-01");
        DateTime end = start;
        while (true) {
    
            end = DateUtil.offset(start, DateField.DAY_OF_YEAR, 1);
            System.out.println(start + " - " + end);
            List<Map<String, Object>> list = jdbcTemplate.queryForList(sql.replace(":start", start.toString()).replace(":end", end.toString()));
            writeObjectToFile(tb_name, list);
            System.out.println(start + " - " + end + "  Has been carried out ...:" + list.size() + "  Data ..." + timer.interval() / 1000 + "  second ...");
            timer.intervalRestart();// It takes time to return , And reset the start time 
            start = end;
            if (start.isAfter(DateUtil.parseDate("2021-06-30"))) {
    
                return;
            }
        }
    }

    private static void writeObjectToFile(String tb_name, List<Map<String, Object>> datas) throws Exception {
    
        RandomAccessFile randomFile = null;
        String fileName = MyUtil.DESKTOP_PREFIX + tb_name + ".csv";
        try {
    
            //  Open a random access file stream , Read and write 
            randomFile = new RandomAccessFile(fileName, "rw");
            //  file length , Number of bytes 
            if (randomFile.length() == 0) {
    
                randomFile.write(StrUtil.join(",", datas.get(0).keySet()).getBytes(CharsetUtil.CHARSET_GBK));
                randomFile.writeBytes("\r\n");
            }
            //  Move the write file pointer to the end of the file .
            long fileLength = randomFile.length();
            randomFile.seek(fileLength);
            for (Map<String, Object> data : datas) {
    
                String line = StrUtil.join(",", data.values()).replace("null", "");
                randomFile.write(line.getBytes(CharsetUtil.CHARSET_GBK));
                randomFile.writeBytes("\r\n");
            }
        } catch (Exception e) {
    
            e.printStackTrace();
        } finally {
    
            if (randomFile != null) {
    
                try {
    
                    randomFile.close();
                } catch (Exception e) {
    
                    e.printStackTrace();
                }
            }
        }
    }
    
    final static String sql = "select * from user u where u.birth between to_date(':start', 'yyyy-mm-dd hh24:mi:ss') and to_date(':end', 'yyyy-mm-dd hh24:mi:ss')";
}
原网站

版权声明
本文为[God__ is__ a__ girl]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211330506439.html