使用 R PostGreSQL 将 table 导出到 Redshift 中的 CSV
export table to CSV in Redshift using R PostGreSQL
我从我的工作站远程使用 R 连接到 Redshift。
install.packages("RPostgreSQL")
library (RPostgreSQL)
drv <- dbDriver("PostgreSQL")
con1 <- dbConnect(drv, host="url", port="xxxx",
dbname="db_name", user="id", password="password")
dbGetInfo(con1)
然后我创建一个 table:
dbSendQuery(con1, "create table schema.table_name as select * from schema.table_name;")
现在我想将此 table 导出到我工作站上的 .csv 文件,如何操作?同样,我的工作站上没有安装 PostGres 数据库,只能使用 R 来访问它。
另外,这个 table 很大,4 列,1400 万行。
谢谢!
您需要将查询结果提取到本地对象中,然后将该对象转储到 CSV 文件中。类似于 res <- dbSendQuery(con1, "select * from schema.table_name"); dat <-dbFetch(res); readr::write_csv(dat, "~/output.csv")
的内容应该可以帮助您入门。
我发帖后想通了-分享..
system.time( fwrite(dbReadTable(con1, c("schema","table")), file="file.csv", sep=",", na="", row.names=FALSE, col.names=TRUE ))
听说 feather 更快?
这是 4300 万行 4 列,耗时 15 分钟。
我从我的工作站远程使用 R 连接到 Redshift。
install.packages("RPostgreSQL")
library (RPostgreSQL)
drv <- dbDriver("PostgreSQL")
con1 <- dbConnect(drv, host="url", port="xxxx",
dbname="db_name", user="id", password="password")
dbGetInfo(con1)
然后我创建一个 table:
dbSendQuery(con1, "create table schema.table_name as select * from schema.table_name;")
现在我想将此 table 导出到我工作站上的 .csv 文件,如何操作?同样,我的工作站上没有安装 PostGres 数据库,只能使用 R 来访问它。
另外,这个 table 很大,4 列,1400 万行。
谢谢!
您需要将查询结果提取到本地对象中,然后将该对象转储到 CSV 文件中。类似于 res <- dbSendQuery(con1, "select * from schema.table_name"); dat <-dbFetch(res); readr::write_csv(dat, "~/output.csv")
的内容应该可以帮助您入门。
我发帖后想通了-分享..
system.time( fwrite(dbReadTable(con1, c("schema","table")), file="file.csv", sep=",", na="", row.names=FALSE, col.names=TRUE ))
听说 feather 更快?
这是 4300 万行 4 列,耗时 15 分钟。