当前位置:网站首页>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')";
}
边栏推荐
- Game between apifox and other interface development tools
- Example analysis of corrplot related heat map beautification in R language
- Kubernetes集群部署
- Generate the last login user account report of the computer through SCCM SQL
- ArrayList源码解析
- 华为机器学习服务语音识别功能,让应用绘“声”绘色
- How to use JWT authentication in thinkphp6
- Vs2017 setting function Chinese Notes
- Buddha bless you that there will never be a bug
- 请教一个问题。adbhi支持保留一个ID最新100条数据库,类似这样的操作吗
猜你喜欢

How to protect biological privacy in the AI era? Overview of the latest "privacy enhancement technology in biometrics" of the Autonomous University of Madrid, comprehensively detailing the biometric p

Interprétation de la thèse (SR - gnn) Shift Robust GNNS: Overcoming the Limits of Localized Graph Training Data

微信小程序轮播图怎么自定义光标位置

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

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

R语言corrplot相关热图美化实例分析

一次 MySQL 误操作导致的事故,高可用都不顶不住!

The agile way? Is agile development really out of date?

Volcano becomes spark default batch scheduler

An accident caused by a MySQL misoperation cannot be withstood by High Availability!
随机推荐
Development of NFT dual currency pledge liquidity mining system
Necessary fault handling system for enterprise network administrator
60 个神级 VS Code 插件!!
Pingcap was selected as the "voice of customers" of Gartner cloud database in 2022, and won the highest score of "outstanding performer"
Application scenarios of channel of go question bank · 11
Unityshader world coordinates do not change with the model
cdc sql表里面的datetime要用什么类型替换
starring V6平台开发接出点流程
请教一个问题。adbhi支持保留一个ID最新100条数据库,类似这样的操作吗
php OSS文件讀取和寫入文件,workerman生成臨時文件並輸出瀏覽器下載
制造业项目MDM主数据项目实施心得
Some small requirements for SQL Engine for domestic database manufacturers
请问一下2.2.0版本支持动态新增mysql同步表吗
【计算讲谈社】第三讲:如何提出关键问题?
R for Data Science (note) -- data transformation (select basic use)
Will the CDC read out of order when I use SQL
Apifox与其他接口开发工具的博弈
Buddha bless you that there will never be a bug
R language 4.1.0 software installation package and installation tutorial
Working for 6 years with a monthly salary of 3W and a history of striving for one PM