有没有办法制作直接从 R 数据帧读取的 JDBC 准备好的语句?
Is there a way to make a JDBC prepared statement that reads from R dataframe directly?
我正在尝试使用 RJDBC 使用 FastLoad 实用程序从 R 数据帧读取到 Teadata 中的 table。是否可以编写准备好的语句并使用 .jcall 直接从数据帧读取?
有些东西我有 noted/tried,但似乎不是直接从数据帧读取的,据我所知:
Teradata-jdbc: What's the point of using Fastload if java has memory limitations?
http://developer.teradata.com/connectivity/articles/speed-up-your-jdbcodbc-applications
https://downloads.teradata.com/blog/ulrich/2013/11/a-wider-test-case-on-r-jdbc-fastload
更新....Parfait 的以下建议对我有用:
library(RJDBC)
con <- dbConnect(... connection details ...)
dbSendUpdate (con, "Drop Table Dl_ho_test.Iris_R")
dbSendUpdate (con, "Create Multiset Table Dl_Ho_Test.Iris_R (
Sepal_Length float
, Sepal_Width float
, Petal_Length float
, Petal_Width float
, Species varchar(10)
) No Primary Index;"
)
## def functions
myinsert <- function(col1, col2, col3, col4, col5){
.jcall(ps, "V", "setDouble", as.integer(1), col1)
.jcall(ps, "V", "setDouble", as.integer(2), col2)
.jcall(ps, "V", "setDouble", as.integer(3), col3)
.jcall(ps, "V", "setDouble", as.integer(4), col4)
.jcall(ps, "V", "setString", as.integer(5), as.character(col5))
.jcall(ps, "V", "addBatch")
}
## prepare
ps = .jcall(con@jc, "Ljava/sql/PreparedStatement;", "prepareStatement", "insert into Dl_Ho_Test.Iris_R(?,?,?,?,?)")
## batch insert
for(n in 1:nrow(iris)) {
myinsert(iris$Sepal.Length[n], iris$Sepal.Width[n], iris$Petal.Length[n], iris$Petal.Width[n], iris$Species[n])
}
## apply & commit
.jcall(ps, "[I", "executeBatch")
dbCommit(con)
.jcall(ps, "V", "close")
.jcall(con@jc, "V", "setAutoCommit", TRUE)
继上次 link 之后,考虑保留函数形式并循环遍历数据框的行:
## def functions
myinsert <- function(col1, col2, col3){
.jcall(ps, "V", "setInt", 1, col1)
.jcall(ps, "V", "setInt", 2, col2)
.jcall(ps, "V", "setString", 3, col3)
.jcall(ps, "V", "addBatch")
}
## prepare
ps = .jcall(con@jc, "Ljava/sql/PreparedStatement;", "prepareStatement",
"insert into Some_Test_Table(?,?,?)")
## batch insert
for(n in 1:nrow(my.data.frame)) {
myinsert(my.data.frame$col1[n], my.data.frame$col2[n], my.data.frame$col3[n])
}
## apply & commit
.jcall(ps, "[I", "executeBatch")
dbCommit(con)
.jcall(ps, "V", "close")
.jcall(conn@jc, "V", "setAutoCommit", TRUE)
我正在尝试使用 RJDBC 使用 FastLoad 实用程序从 R 数据帧读取到 Teadata 中的 table。是否可以编写准备好的语句并使用 .jcall 直接从数据帧读取?
有些东西我有 noted/tried,但似乎不是直接从数据帧读取的,据我所知:
Teradata-jdbc: What's the point of using Fastload if java has memory limitations?
http://developer.teradata.com/connectivity/articles/speed-up-your-jdbcodbc-applications
https://downloads.teradata.com/blog/ulrich/2013/11/a-wider-test-case-on-r-jdbc-fastload
更新....Parfait 的以下建议对我有用:
library(RJDBC)
con <- dbConnect(... connection details ...)
dbSendUpdate (con, "Drop Table Dl_ho_test.Iris_R")
dbSendUpdate (con, "Create Multiset Table Dl_Ho_Test.Iris_R (
Sepal_Length float
, Sepal_Width float
, Petal_Length float
, Petal_Width float
, Species varchar(10)
) No Primary Index;"
)
## def functions
myinsert <- function(col1, col2, col3, col4, col5){
.jcall(ps, "V", "setDouble", as.integer(1), col1)
.jcall(ps, "V", "setDouble", as.integer(2), col2)
.jcall(ps, "V", "setDouble", as.integer(3), col3)
.jcall(ps, "V", "setDouble", as.integer(4), col4)
.jcall(ps, "V", "setString", as.integer(5), as.character(col5))
.jcall(ps, "V", "addBatch")
}
## prepare
ps = .jcall(con@jc, "Ljava/sql/PreparedStatement;", "prepareStatement", "insert into Dl_Ho_Test.Iris_R(?,?,?,?,?)")
## batch insert
for(n in 1:nrow(iris)) {
myinsert(iris$Sepal.Length[n], iris$Sepal.Width[n], iris$Petal.Length[n], iris$Petal.Width[n], iris$Species[n])
}
## apply & commit
.jcall(ps, "[I", "executeBatch")
dbCommit(con)
.jcall(ps, "V", "close")
.jcall(con@jc, "V", "setAutoCommit", TRUE)
继上次 link 之后,考虑保留函数形式并循环遍历数据框的行:
## def functions
myinsert <- function(col1, col2, col3){
.jcall(ps, "V", "setInt", 1, col1)
.jcall(ps, "V", "setInt", 2, col2)
.jcall(ps, "V", "setString", 3, col3)
.jcall(ps, "V", "addBatch")
}
## prepare
ps = .jcall(con@jc, "Ljava/sql/PreparedStatement;", "prepareStatement",
"insert into Some_Test_Table(?,?,?)")
## batch insert
for(n in 1:nrow(my.data.frame)) {
myinsert(my.data.frame$col1[n], my.data.frame$col2[n], my.data.frame$col3[n])
}
## apply & commit
.jcall(ps, "[I", "executeBatch")
dbCommit(con)
.jcall(ps, "V", "close")
.jcall(conn@jc, "V", "setAutoCommit", TRUE)