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
2 answers

R and cassandra connection error

library(RJDBC) cassdrv <- JDBC("org.apache.cassandra.cql.jdbc.CassandraDriver", list.files("/home/beyhan/Downloads/jars/",pattern="jar$",full.names=T)) casscon <- dbConnect(cassdrv,…
Beyhan Gul
  • 1,191
  • 1
  • 15
  • 25
1
vote
1 answer

Connecting to hive (kerberoes enabled) with R rJDBC package from Rstudio windows

the following issue is coming when trying to connect Hive 2 (kerberoes authenticat is enabled) using R rjdbc. used simba driver to connect to hive. hiveConnection <- dbConnect(hiveJDBC,…
1
vote
0 answers

RJDBC: Connect oracle from R

I'm trying to connect to oracle DB from R in Mac (El Capitan) using RJDBC connector . I downloaded the driver, ojdbc6.jar from Oracle Driver. In my R, I setup $ Sys.setenv(JAVA_HOME='/usr/libexec/java_home') $ options(java.parameters="-Xmx2g") …
Hash
  • 11
  • 2
1
vote
1 answer

RJDBC issues: Error in .jfindClass(as.character(driverClass)[1]) : class not found

I have been for a while reading about this issue, at github and here at stackoverflow. However, I don't seem to find the right fix to my problem. Here is the process of what I did: a while ago I started using Spark and I had a Java related error…
Javier
  • 1,530
  • 4
  • 21
  • 48
1
vote
0 answers

R Quickest way to import large MSSQL data tables

I am looking for the utmost quickest way to import an MSSQL data table into R. I currently use: ch <- odbcConnect("Table1", uid="***", pwd="***") system.time(TransactionFrame <- sqlFetch(ch, "V_Transactions_Extended")) odbcClose(ch) There are +/- 5…
gmarais
  • 1,801
  • 4
  • 16
  • 32
1
vote
2 answers

connect to Remote Hive Server from R using RJDBC/RHive

I'm using RJDBC 0.2-5 to connect to Hive in Rstudio. My server has hadoop-2.4.1 and hive-0.14. I follow the below mention steps to connect to Hive. library(DBI) library(rJava) library(RJDBC) .jinit(parameters="-DrJava.debug=true") drv <-…
user2538041
  • 61
  • 1
  • 7
1
vote
2 answers

RJDBC driver with crate do not find class

I am trying to create a RJDBC driver for crate. Whenever I ran jdbcDriver <- JDBC(driverClass = 'io.crate.client.jdbc.CrateDriver', '../../private/tmp/crate-jdbc-standalone-1.0.4.jar') I got: Fehler (Error) in…
Robert Kirsten
  • 474
  • 2
  • 5
  • 12
1
vote
0 answers

RQuantLib OSX installation issue

followed the steps below to install QuantLib on OSX and it went smoothly: http://quantlib.org/install/macosx.shtml Then trying to install RQuantLib. First download the source, unzipped and executed the following command from Terminal, with the error…
valuenaut
  • 303
  • 2
  • 4
  • 13
1
vote
2 answers

RH2: Connect to H2 Database in R

I tried to connect with RH2 to a H2 (1.4.181) Database. I've tried different things, like reinstalling R, and fixing potential problems with rJava on Ubuntu. However following error does not disappear: > library("RH2") Loading required package:…
Tobi
  • 11
  • 1
1
vote
3 answers

R programming: RODBC and dataframe

Having trouble understanding how to pass a dataframe's column value to the query? The query works fine if I pass the value directly. The values are stored in df$number. The tables names are changed for sharing purposes: dataframe <-…
Blake S.
  • 401
  • 1
  • 4
  • 10
1
vote
1 answer

executing library('RJDBC') on R console shows java 6 installation message

I'm trying to access a mysql database through R console using RJDBC in a mac os X system, but R console crash just after type in " > library('RJDBC') ". Immediately shows up a message indicating is necessary install the old Java 6 mac os version. I…
Joseratts
  • 97
  • 1
  • 9
1
vote
1 answer

RJDBC wrongly reading bigintegers from database table

I am retrieving a column containing big integers from a data base into R (using RJDBCs dbGetQuery method). For a test case, one can consider the following numbers 1000522010609612 1000522010609613 1000522010609614 1000522010609615…
acc
  • 35
  • 4
1
vote
1 answer

rJava fails while invoking RJDBC library from OpenCPU API

Goal : To use RJDBC with OpenCPU AJAX Post call. Use case : I need to connect with Apache phoenix database and get some data. And on that data I am supposed to do analytics. So to connect with Phoenix i am using rjdbc(phoenix doesn't support any…
Amith
  • 1,424
  • 1
  • 10
  • 22
1
vote
1 answer

Unable to connect to Oracle database via RJDBC

I get an error when I try to make a connection to Oracle11G DB. My output is as below. Please advise >Sys.setenv(JAVA_HOME='C:/Program Files/Java/jdk1.6.0_45') > options(java.parameters="-Xmx2g") > library(rJava) > .jinit() [1] 0 >…
crkatz
  • 101
  • 5
1
vote
2 answers

How could R use RJDBC to connect to Hive?

I'm using hadoop-2.2.0 and hive-0.12. I followed the following steps to try to connect to Hive in Rstudio: library("DBI") library("rJava") library("RJDBC") for(l in list.files('/PATH/TO/hive/lib/')){…
flyer
  • 9,280
  • 11
  • 46
  • 62