有没有一种方法可以使JDBC准备好的语句直接从R数据帧读取?

时间:2019-04-24 16:00:54

标签: r dataframe jdbc teradata rjdbc

我正在尝试使用RJDBC使用FastLoad实用程序从R数据帧读取到Teadata中的表中。是否可以编写一条准备好的语句并使用.jcall直接从数据帧读取?

我已经指出/尝试过的一些事情,但是根据我的判断,这些事情似乎并不能直接从数据框中读取:

http://developer.teradata.com/blog/amarek/2013/11/how-to-use-jdbc-preparedstatement-batch-insert-with-r-0

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)

1 个答案:

答案 0 :(得分:0)

在最后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)