我尝试使用PostgreSQL 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
的调用来解决。