当前位置:网站首页>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')";
}
边栏推荐
- Huawei machine learning service speech recognition function enables applications to paint "sound" and color
- 假如,程序员面试的时候说真话
- [computer talk club] Lecture 3: how to raise key issues?
- Understanding openstack network
- 8 challenges of BSS application cloud native deployment
- php OSS文件读取和写入文件,workerman生成临时文件并输出浏览器下载
- Experience of MDM master data project implementation for manufacturing projects
- Test drive citus 11.0 beta (official blog)
- Xiaobai, let me ask you guys, is MySQL binlog extracted by CDC in strict order
- Programmers spend most of their time not writing code, but...
猜你喜欢

High dimension low code: component rendering sub component

R for Data Science (note) -- data transformation (select basic use)

php OSS文件读取和写入文件,workerman生成临时文件并输出浏览器下载

Northwestern Polytechnic University attacked by hackers? Two factor authentication changes the situation!

Vs2017 add header file path method

Geoscience remote sensing data collection online

60 divine vs Code plug-ins!!

怎么使用R包ggtreeExtra绘制进化树

网络安全审查办公室对知网启动网络安全审查

Experience of MDM master data project implementation for manufacturing projects
随机推荐
SaltStack State状态文件配置实例
对国产数据库厂商提几个关于SQL引擎的小需求
小滴课堂海量数据处理商用短链平台大课
Experience of MDM master data project implementation for manufacturing projects
A detailed explanation of the implementation principle of go Distributed Link Tracking
Introduction to smart contract security audit delegatecall (2)
Volcano成Spark默认batch调度器
Php OSS file read and write file, workerman Generate Temporary file and Output Browser Download
What do I mean when I link Mysql to report this error?
一次 MySQL 误操作导致的事故,高可用都不顶不住!
The script implements the automated deployment of raid0
制造业项目MDM主数据项目实施心得
60 divine vs Code plug-ins!!
Download steps of STM32 firmware library
MySQL binlog data source configuration document, please share
Buddha bless you that there will never be a bug
Internet of things? Come and see Arduino on the cloud
[R tidyverse] use of select verb
R for Data Science (note) -- data transformation (select basic use)
通过SCCM SQL生成计算机上一次登录用户账户报告