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
0
votes
0 answers

Understand Intermittent SSL error from dbGetQuery

I can use some help understanding the following intermittent error we get w/ an R/shiny app deployed to rs-connect from RStudioPro. The calling code (which usually works) is RAW <- dbGetQuery(imp_connect, "SELECT * from XXXX.XXXXX") imp_connect…
bill rowe
  • 43
  • 5
0
votes
0 answers

How to call a S4 class obj in a function?

Please look at the below code: jdbcDriver <- JDBC(driverClass=Driver class path, classPath=JarFile location) jdbcConnection <- dbConnect(jdbcDriver, Driver, Username,Password) I am able to successfully establish a connection, with the above…
kgr
  • 23
  • 4
0
votes
0 answers

Why I am receiving a service request error when connecting R to Oracle

I am attempting to connect my R instance to my company's Oracle database. I had issues installing ROracle due to being on version 3.3.6 of R, so I settled on RJDBC instead. I downloaded the JDBC driver in a jar file. And stored this as my jdbcDriver…
0
votes
1 answer

In R, reference a .sql file in query to DB connection

I'm trying to more seamlessly integrate my use of R and SQL. The good news is I have the connection from R to db2 working well and have been using the RJDBC package to do so. I've also been using the dbGetQuery() function that includes the sql…
mr_puddles
  • 97
  • 7
0
votes
1 answer

RJDBC hive, connect failed

I followed multiples tutorials to try to connect to Hive with RJDBC, without sucess. Here is what I have: library(DBI) library(rJava) library(RJDBC) driver <- JDBC('org.apache.hive.jdbc.HiveDriver', classPath =…
BeGreen
  • 765
  • 1
  • 13
  • 39
0
votes
1 answer

R HANA sql query selecting certain column names is returning column names as dataframe values

When I run a sql query from HANA in R pulling all columns, the data returns correctly. sql <- "SELECT TOP 10 * FROM \"ccf-edw.self-service.DOIP::R_CA_B_DemandPlan\"( 'PLACEHOLDER' = ('$$IP_ExtractionWeekFrom$$', '201943'), …
jarichardson
  • 165
  • 8
0
votes
0 answers

Usage of r-dbwritetable for different oracle owner

I am not able to write data to a table part of the different owner (Oracle data base) using dbwriteTable() even after I have INSERT Grants. I have tried using the below option but, getting an error. When I tried checking the dbExistsTable(), it is…
Sravan
  • 1
  • 3
0
votes
1 answer

RJDBC not finding driver class

I am trying to access athena through RJDBCpackage, but I keep getting the following error: .jclassLoader()$setDebug(1L) drv <- JDBC(driverClass = "com.amazonaws.athena.jdbc.AthenaDriver", …
Felipe Alvarenga
  • 2,572
  • 1
  • 17
  • 36
0
votes
1 answer

Is there a way to make a JDBC prepared statement that reads from R dataframe directly?

I am trying to read from an R dataframe into a table in Teadata with the FastLoad utility, using RJDBC. Is it possible to write a prepared statement and use .jcall to read directly from dataframe? Some things I have noted/tried, but which do not…
Brent Gunderson
  • 173
  • 1
  • 8
0
votes
1 answer

R Error in .jfindClass(as.character(driverClass)[1]) : java.lang.ClassNotFoundException

Trying to set up a R JDBC connection to EMR hive instance on macOS. Getting the error: Error in .jfindClass(as.character(driverClass)[1]) : java.lang.ClassNotFoundException This is what I have so far: library(DBI) library(rJava) …
rockboy23
  • 85
  • 1
  • 9
0
votes
1 answer

Read SQL table faster in R

I am working on a R shiny project where I need to read SQL tables from my Shiny application. I have tried using RODBC and RJDBC packages for the same and have found RJDBC to be faster. But it is still taking a lot of time to read. Below is the code…
kawsleo
  • 560
  • 4
  • 23
0
votes
0 answers

Error when installing RJDBC / odbc packages on RStudio Server AWS

I am trying to install the RJDBC and odbc packages on my instance of RStudio server, which is running on AWS: > Sys.info() sysname release version …
Shinobi_Atobe
  • 1,793
  • 1
  • 18
  • 35
0
votes
0 answers

RcallMethod Error While Writing Data Frame to Oracle DB Using Parallel Approach

I am trying to write my dataframe to Oracle DB using RJDBC connection. I am trying to implement a parallel approach using foreach / parLappy. Here is my code Sys.setenv(JAVA_HOME='C:/Program Files/Java/jre1.8.0_181')…
boyaronur
  • 521
  • 6
  • 18
0
votes
1 answer

JVM Error While Writing Data Frame to Oracle Database using parLapply

I want to parallelize my data writing process. I am writing a data frame to Oracle Database. This data has 4 million rows and 8 columns. It takes 6.5 hours without parallelizing. When I try to go parallel, I get the error Error in…
boyaronur
  • 521
  • 6
  • 18
0
votes
0 answers

Unable to Export Data to from R to Vertica

It will be Great if anyone an help me out with COPY function syntax. I am trying to copy data from google Doc to vertica with the help of r (using google-sheets and RJDBC Package). I have exported the data from google sheet but unable to import in…