Questions tagged [rjdbc]

RJDBC - Allows the use of JDBC to access databases from R

About

RJDBC is an package providing a database interfaces using . This allows the use of any in R through the JDBC interface. The only requirement is working and a JDBC driver for the database engine to be accessed. RJDBC uses the interface which is also used by the packages , , , and .

Example

library(RJDBC)

drv <- JDBC("com.mysql.jdbc.Driver",
           "/etc/jdbc/mysql-connector-java-3.1.14-bin.jar",
           identifier.quote="`")

conn <- dbConnect(drv, "jdbc:mysql://localhost/test", "user", "pwd")

Loads a JDBC driver for MySQL (adjust the path to the driver's JAR file as necessary) and connects to the local database "test". The connection handle conn is used for all subsequent operations.

(Note: Windows users can use drv<-JDBC("sun.jdbc.odbc.JdbcOdbcDriver") to use Sun's JDBC to ODBC Bridge with "jdbc:odbc:..." URL.)

In order to allow more complex names for tables and columns, it is a good idea to set identifier.quote to the quote character supported by the database for quoting identifiers. The default (NA) does not quote identifiers, but this limits the names that can be used, affecting dbReadTable and dbWriteTable.

dbListTables(conn)
data(iris)
dbWriteTable(conn, "iris", iris, overwrite=TRUE)
dbGetQuery(conn, "select count(*) from iris")
d <- dbReadTable(conn, "iris")

RJDBC supports prepared statements and argument substitution, so it is possible to run queries like:

dbGetQuery(conn, "select count(*) from iris where Species=?", "setosa")

Note that the life time of a connection, result set, driver etc. is determined by the lifetime of the corresponding R object. Once the R handle goes out of scope (or if removed explicitly by rm) and is garbage-collected in R, the corresponding connection or result set is closed and released. This is important for databases that have limited resources (like Oracle) - you may need to add gc() by hand to force garbage collection if there could be many open objects. The only exception are drivers which stay registered in the JDBC even after the corresponding R object is released as there is currently no way to unload a JDBC driver (in RJDBC).

Type Handling

Type-handling is a rather complex issue, especially with JDBC as different databases support different data types. RJDBC attempts to simplify this issue by internally converting all data types to either character or numeric values. When retrieving results, all known numeric types are converted to R's numeric representation and all other types are treated as characters. When assigning parameters in parametrized queries, numeric, integer and character are the types used. Convenience methods like dbReadTable and dbWriteTable can only use the most basic SQL types, because they don't know what DBMS will be used. Therefore dbWriteTable uses only INTEGER, DOUBLE PRECISION or VARCHAR(255) to create the table. For all other types you'll have to use DBML statements directly.

License

RJDBC is released under v2.

Repositories

Other resources

Related tags

200 questions
1
vote
0 answers

Unable to load RJDBC package in RStudio

I have successfully installed the RJDBC in RStudio. install.packages("RJDBC") trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/RJDBC_0.2-7.zip' Content type 'application/zip' length 70232 bytes (68 KB) downloaded 68 KB package ‘RJDBC’…
Abhishek Singh
  • 10,243
  • 22
  • 74
  • 108
1
vote
1 answer

Read a View created from a procedure in SAP HANA from R

I have schema in SAP HANA by the name "HYZ_ProcurementToSales" and View "V_HYZ_P25_Market_Market_Orders" which is created from a procedure, I am trying to extract the view in the R server version 1.0.153. The code I am using…
Ashmin Kaul
  • 860
  • 2
  • 12
  • 37
1
vote
0 answers

Integration of R and SAP HANA using RJDBC

I am trying to connect R-Studio Server with Sap Hana to fetch a view and perform analyses. I am basically using a citrix environment so I don't have access to the C: drive. Rather I have the hdbclient folder in sap stored in z: drive. The code I am…
Ashmin Kaul
  • 860
  • 2
  • 12
  • 37
1
vote
1 answer

Issue connecting R with Teradata

I'm having issues connecting R with Teradata. library("RJDBC") drv = JDBC("com.teradata.jdbc.TeraDriver";"C:\\...\\terajdbc4.jar, C:\\...\\tdgssconfig.jar") conn = dbConnect(drv,"jdbc:teradata://dbserver","user1","pwd1") When I try…
Barbara
  • 1,118
  • 2
  • 11
  • 34
1
vote
1 answer

.jfindclass error on R Shiny Server

I have my shinyapp deployed on Shiny Server. I use RJDBC::JDBC() method to connect to Redshift DB. RedshiftJDBC42-1.2.1.1001.jar file is in the folder called "driver" which is in the same folder as app.R file, the argument 'driverClass' is…
Julia
  • 11
  • 4
1
vote
0 answers

R code to increase Teradata JDBC driver maximum byte length

I'm trying to export some fairly large geometry objects into teradata from R, using bulk insert through RJDBC. The problem is some of the objects are over 64000 characters: Error in .jcall(ps, "V", "addBatch") : java.sql.SQLException: [Teradata…
Alexvonrass
  • 330
  • 2
  • 12
1
vote
1 answer

RJDBC: dbReadTable not working

When I upgrade DBI from 0.5.1 to 0.6, whenever I call dbReadTable: mv = dbReadTable(conn, "MOVIES") driver = JDBC("oracle.jdbc.OracleDriver", classPath = class_path)n conn = dbConnect(driver, myhost, username, password) It…
Zhiya
  • 610
  • 2
  • 7
  • 22
1
vote
2 answers

RJDBC dbGetQuery() ERROR to create external table HIVE

I encounter this problem: the DB call only creates a table, it has problem of retrieving JDBC result set. Error in .verify.JDBC.result(r, "Unable to retrieve JDBC result set for Calls: dbGetQuery ... dbSendQuery -> dbSendQuery -> .local ->…
Mary Li
  • 11
  • 2
1
vote
0 answers

Write R DataFrame to Teradata using RJDBC

We want to insert a dataframe into a table in teradata. so we connect to the database drv = JDBC("com.teradata.jdbc.TeraDriver","C:\\Users\\~\\TeradataJDBCDDriver\\terajdb c4.jar;C:\\Users\\~\\TeradataJDBCDDriver\\tdgssconfig.jar") conn =…
UserX
  • 115
  • 1
  • 1
  • 6
1
vote
1 answer

dbwriteTable incompatible with other schemas

I am not sure if this question has surfaced before but i am not able to write data to a different other than mine using dbwriteTable() even if i have write access to others schema. Below are the things i have tried dbWriteTable(conn=,name=…
1
vote
2 answers

create / drop view / table using rjdbc

I am trying to automate an analytic process using rjdbc. I am looking for a way to create and drop a view from within R, using the rjdbc package.
doron
  • 454
  • 6
  • 10
1
vote
1 answer

RDJDBC::dbConnect failing to connect to HiveServer2 (kerberos +sasl)

I am trying to connect to Hive2 using RJDBC but it failing with "GSS initiate failed". However same things working fine using beeline client. Any idea what may have caused different behavior when running both on same node with same credentials? drv…
iSKC
  • 11
  • 2
1
vote
3 answers

R, Call a SQL Server stored procedure with RJDBC

I would like to call a stored procedure from a function in R. See my code below. unfortunately this code only generates a dataframe without values in it. I would like to fix this with RJDBC&DBI, since there seems to be a problem with RODBC. …
M. Kooi
  • 245
  • 3
  • 17
1
vote
0 answers

RJDBC works fine on windows but not on centos

I am trying to connect to Oracle using RJDBC. My R code works fine on windows environment, When I put it on centos VM, I am facing error. JAVA_HOME is properly set. Required packages RJDBC, rJava are also installed properly. Here is my code:…
JAY G
  • 553
  • 2
  • 12
  • 21
1
vote
1 answer

R using RJDBC not writing data to Hive table

RJDBC connecting to Hive fine and also reading the data from Hive. But it is not writing data to Hive using --> dbWriteTable. see below- options(java.parameters = "-Xmx8g") library(DBI) library(rJava) library(RJDBC) cp <-…