首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >有没有一种方法可以让JDBC预准备语句直接从R dataframe读取?

有没有一种方法可以让JDBC预准备语句直接从R dataframe读取?
EN

Stack Overflow用户
提问于 2019-04-25 00:00:54
回答 1查看 269关注 0票数 0

我正在尝试使用FastLoad实用程序,使用RJDBC将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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-25 00:54:24

在最后一个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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55834207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档