从R将大型CSV文件加载到PostgreSQL时出现错误。

15 浏览
0 Comments

从R将大型CSV文件加载到PostgreSQL时出现错误。

我有大约10,000个csv文件,我正在试图将它们导入到Postgres中。每个csv文件的大小在500MB到1GB之间。我首先将每个文件作为数据框导入到R中,因为我必须对原始数据进行一些预处理(如过滤掉一些行并添加一些附加列)。然后,我使用dbWriteTable将数据写入postgres。

status = try(dbWriteTable(con, name = "my_table", value = my.df, row.names = FALSE, append = TRUE,overwrite= FALSE))

在同一个R脚本中,我还将一些基本数据(文件索引)写入其他表格

qry = paste0("insert into file_list (file_name) values ('",filename,"')")
dbSendQuery(con,qry)

R脚本通常运行良好,但经常出现以下消息而停止工作

Error in postgresqlExecStatement(conn, statement, ...): RS-DBI driver: 
(could not run statement: server closed the connection unexpectedly\n\tThis
probably means the server terminated abnormally\n\tbefore or while
processing the request.\n)\n

很少也会出现以下消息

Error in postgresqlCopyInDataframe(new.con, value) : RS-DBI driver:
(PQputCopyData failed: server closed the connection unexpectedly
This probably means the server terminated abnormally before or while 
processing the request.)
Error in postgresqlNewConnection(drv, ...) : RS-DBI driver: (could not
connect my_db@my_address on dbname "mydb") Error in
!dbPreExists : invalid argument type

我不确定是什么原因导致了这些消息以及如何避免这些问题。这是因为与服务器失去连接还是因为文件太大?任何帮助将不胜感激。

我使用的是R 3.3.1(64位),Windows 7(64位,8GB RAM)和Postgres 9.2。

谢谢。

0