如何正确地为PostgreSQL复制读者和作者COPY?

时间:2015-02-16 10:59:53

标签: java postgresql jdbc

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

0 个答案:

没有答案