当前位置:网站首页>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')";
}
边栏推荐
- Northwestern Polytechnic University attacked by hackers? Two factor authentication changes the situation!
- BSS应用程序云原生部署的8大挑战
- 模块五
- 佛祖保佑 永无BUG
- Fabric 账本数据块结构解析(一):如何解析账本中的智能合约交易数据
- Xiaobai, let me ask you guys, is MySQL binlog extracted by CDC in strict order
- The script implements the automated deployment of raid0
- Real time rendering: the difference between real-time, offline, cloud rendering and hybrid rendering
- Ask a question. Adbhi supports the retention of 100 databases with the latest IDs. Is this an operation like this
- flink cdc全量读mysql老是报这个错怎么处理
猜你喜欢
Using alicloud RDS for SQL Server Performance insight to optimize database load - first understanding of performance insight
Why is the executor thread pool framework introduced
A detailed explanation of the implementation principle of go Distributed Link Tracking
Unity mobile game performance optimization spectrum CPU time-consuming optimization divided by engine modules
System design idea of time traceability
php OSS文件讀取和寫入文件,workerman生成臨時文件並輸出瀏覽器下載
技术实现 | Apache Doris 冷热数据存储(一)
Application practice | massive data, second level analysis! Flink+doris build a real-time data warehouse scheme
Kubernetes cluster deployment
Vs2017 setting function Chinese Notes
随机推荐
Tkde2022: Dialogue recommendation system based on knowledge enhanced sampling
Necessary fault handling system for enterprise network administrator
The difference between the lazy man mode and the hungry man mode
Kubernetes cluster deployment
Multi cloud mode is not a "master key"
工作6年,月薪3W,1名PM的奋斗史
Intel and Microsoft give full play to the potential energy of edge cloud collaboration to promote the large-scale deployment of AI
R language 4.1.0 software installation package and installation tutorial
Zadig + 洞态 IAST:让安全溶于持续交付
Fabric ledger data block structure analysis (I): how to analyze the smart contract transaction data in the ledger
一文理解OpenStack网络
Power supply noise analysis
thinkphp6中怎么使用jwt认证
System design idea of time traceability
mysql binlog 数据源配置文档麻烦分享一下
Capacitive inching touch switch module control (stm32f103c8t6)
cdc sql表里面的datetime要用什么类型替换
一文理解OpenStack网络
Technology implementation | Apache Doris cold and hot data storage (I)
Mq-2 smoke concentration sensor (STM32F103)