Load few columns from file to Oracle
Previous attempt: Tried to load the file into a data frame, but RStudio started to hang.
library(ROracle)
con <- dbConnect(drv, username ="<dbuser>", password = "<dbpassword>", dbname="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=<host>)(Port=<port>))(CONNECT_DATA=(SERVICE_NAME=<db_service>)))")
dbWriteTable(con,"<TABLENAME>",data.frame(gsub("\\s*,$", "",as.matrix(
read.fwf(
"C:\\Users\\<username>\\<filename>", widths = c(18,31,31,31,12,14,11,6,26,15,1), comment.char =
"",stringsAsFactors = FALSE, skip = 1
)[,c(1,7)]
))),append = TRUE)
Comments on the Solution:- using ROracle
- read.fwf outputs a data frame
- as.matrix converts the dataframe into a matrix for gsub
- gsub outputs a matrix
- data.frame converts the matrix into a data frame that is then written to Oracle
- [,c(1,7)] is used with read.fwf to select only columns 1 and 7 from the file
Comments
Post a Comment