当前位置:网站首页>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')";
}
边栏推荐
- Drawing DEM with GEE gracefully
- Database index can improve query efficiency. Ask what will improve, what is the difference between inapplicable index and index use, and what will happen.
- Based on STM32F103 0.96 inch OLED LCD driver (IIC communication)
- Fabric 账本数据块结构解析(一):如何解析账本中的智能合约交易数据
- How to customize cursor position in wechat applet rotation chart
- Full link service tracking implementation scheme
- [R tidyverse] use of select verb
- Development of NFT dual currency pledge liquidity mining system
- Php OSS file read and write file, workerman Generate Temporary file and Output Browser Download
- R语言 4.1.0软件安装包和安装教程
猜你喜欢

PHP OSS file reads and writes files, and workman generates temporary files and outputs them to the browser for download

【Go语言刷题篇】Go从0到入门4:切片的高级用法、初级复习与Map入门学习

How to use R package ggtreeextra to draw evolution tree

Module V

Game between apifox and other interface development tools

60 divine vs Code plug-ins!!

Zadig + cave Iast: let safety dissolve in continuous delivery

IBPS开源表单设计器有什么功能?

工作6年,月薪3W,1名PM的奋斗史

SaltStack State状态文件配置实例
随机推荐
Dataworks development ODPs SQL development production environment automatic completion of ProjectName
At present, only CDC monitors Mysql to get the data of new columns. Sqlserver can't, can it
Volcano becomes spark default batch scheduler
MySQL binlog data source configuration document, please share
TKDE2022:基于知识增强采样的对话推荐系统
60 divine vs Code plug-ins!!
【Go语言刷题篇】Go从0到入门4:切片的高级用法、初级复习与Map入门学习
R语言 4.1.0软件安装包和安装教程
【计算讲谈社】第三讲:如何提出关键问题?
Starring V6 platform development take out point process
Xiaodi class massive data processing business short chain platform
Download steps of STM32 firmware library
Power supply noise analysis
我链接mysql 报这个错 是啥意思呀?
NFT pledge liquidity mining system development technology
R for Data Science (notes) -- data transformation (used by filter)
Ls common parameters
Northwestern Polytechnic University attacked by hackers? Two factor authentication changes the situation!
Tkde2022: Dialogue recommendation system based on knowledge enhanced sampling
物联网?快来看 Arduino 上云啦