有没有办法制作直接从R数据帧读取的JDBC预处理语句?

问题描述 投票:0回答:1

我试图使用RODBC从使用FastLoad实用程序的R数据帧读入Teradata中的表。是否可以编写预准备语句并使用.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)
r dataframe jdbc teradata rjdbc
1个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.