Java 如何正确连接PostgreSQL副本的读写器?
我正在尝试使用PostgreSQLJava 如何正确连接PostgreSQL副本的读写器?,java,postgresql,jdbc,Java,Postgresql,Jdbc,我正在尝试使用PostgreSQLCOPY命令导入数据,而不创建临时文件。初始数据的格式需要转换,因此我有如下内容: void itemsToCsv(String filename, Writer writer) { /* Start processing */ CSVPrinter printer = new CSVPrinter(writer, CSVFormat.DEFAULT) for(Array[String] row: processedFile) { print
COPY
命令导入数据,而不创建临时文件。初始数据的格式需要转换,因此我有如下内容:
void itemsToCsv(String filename, Writer writer) {
/* Start processing */
CSVPrinter printer = new CSVPrinter(writer, CSVFormat.DEFAULT)
for(Array[String] row: processedFile) {
printer.printRecord(row);
}
}
现在问题来了。我在考虑使用类似这样的代码
CopyManager copyMgr = (BaseConnection) conn.getCopyAPI();
Writer w = new PipedWriter();
Reader r = new PipedReader(w);
CopyIn cpin = copyMgr.copyIn("COPY items_import (id, name)FROM STDIN CSV", r);
itemsToCsv("items_in_weird_format.xml", w)
据我所知,copyIn
方法将被阻塞,因为还没有什么可读取的内容。是否有一些聪明的方法将读者和作者连接在一起,而不必求助于CopyIn.writeToCopy
方法
UPD。通过在线程中包装对itemsToCsv
的调用来解决