Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a data frame to a Teradata table using RJDBC

Tags:

r

teradata

rjdbc

After establishing a connection as explained here, I tried to write a very simple data frame (trythis, shown below) to a table called gh_test_20141105 in a database called p_cia_t. First, I tried

> conn <- getTdConnection(vdm='vivaldi')
> dbWriteTable(conn=conn,name=tbl,value=trythis)
Error in .verify.JDBC.result(s, "Unable to execute JDBC prepared statement ",  : 
  Unable to execute JDBC prepared statement INSERT INTO p_cia_t.gh_test_20141005 VALUES(?,?,?,?) ([Teradata Database] [TeraJDBC 14.10.00.17] [Error 3932] [SQLState 25000] Only an ET or null statement is legal after a DDL Statement.)

Next, I created an empty table in Teradata:

create table p_cia_t.gh_test_20141105 (eenie integer, meenie integer, minie integer, moe integer);

My next two attempts to write this data frame failed in contradictory ways:

    > dbWriteTable(conn=conn,name=tbl,value=trythis,append=T)
Error in .local(conn, statement, ...) : 
  execute JDBC update query failed in dbSendUpdate ([Teradata Database] [TeraJDBC 14.10.00.17] [Error 3803] [SQLState 42S01] Table 'gh_test_20141105' already exists.)
> dbWriteTable(conn=conn,name=tbl,value=trythis,append=T,overwrite=F)
Error in .local(conn, name, value, ...) : 
  Cannot append to a non-existing table `p_cia_t.gh_test_20141105'
> trythis
  eenie meenie minie moe
1     1      4     7  10
2     2      5     8  11
3     3      6     9  12
> conn
An object of class "JDBCConnection"
Slot "jc":
[1] "Java-Object{com.teradata.jdbc.jdk6.JDK6_SQL_Connection@2f242b11}"

Slot "identifier.quote":
[1] NA

> tbl
[1] "p_cia_t.gh_test_20141105"

Why would append=T object that a table already exists, and how can adding overwrite=F change the objection to "non-existing table?" Neither makes any sense.

I would greatly appreciate any working example of successful writing of a data frame to a Teradata table, whether pre-existing or not, with or without appending.

> sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] RJDBC_0.2-4      rJava_0.9-6      DBI_0.3.1        dplyr_0.3.0.2    scales_0.2.4     ggplot2_1.0.0    reshape2_1.4    
[8] RODBC_1.3-10     data.table_1.9.4

loaded via a namespace (and not attached):
 [1] assertthat_0.1   chron_2.3-45     colorspace_1.2-4 digest_0.6.4     grid_3.1.2       gtable_0.1.2     magrittr_1.0.1  
 [8] MASS_7.3-35      munsell_0.4.2    parallel_3.1.2   plyr_1.8.1       proto_0.3-10     Rcpp_0.11.3      stringr_0.6.2   
[15] tools_3.1.2   
like image 400
Gabi Avatar asked Nov 05 '14 23:11

Gabi


1 Answers

I think this is due to a bug in Teradata's JDBC driver. As explained here, the reason is that

The problem is that in dbWriteTable RJDBC disables autocommit (and enables it again at the end), creates the table and then tries to insert into it in the same transaction. Firebird does not allow inserts into a table that is created in the same transaction.

This page seems to offer a solution but I think fundamentally we may have to wait until TD fixes the bug...

My temporary solution is to turn to ODBC (small table) or call fastload directly by system(“fastload < your_fastload_script”) (big table)…

like image 107
Liyun Chen Avatar answered Oct 30 '22 23:10

Liyun Chen