R Language with JDBC Driver

This article shows how to use Treasure Data with the R language by using the RJDBC package.

Table of Contents

Tutorial Video

Here’s a video to show you how to connect to Treasure Data from R.

Download

You can download the Treasure Data JDBC driver from the link below:

Untitled-3
This driver only works with Treasure Data. It does not work in other environments such as your local Hadoop / Hive cluster.

Installing the RJDBC Package

First, please install the RJDBC Package as shown below:

$ R
> install.packages("RJDBC",dep=TRUE)

Issuing Queries

To issue queries, please use the RJDBC package together with the Treasure Data JDBC driver.

library(RJDBC)
drv <- JDBC("com.treasuredata.jdbc.TreasureDataDriver",
            "/path/to/td-jdbc-<td-jdbc.version>-jar-with-dependencies.jar",
            identifier.quote="`")
conn <- dbConnect(drv,
                  "jdbc:td://api.treasuredata.com/testdb;type=presto;useSSL=true",
                  "YOUR_USER_NAME_HERE",
                  "YOUR_PASSWORD_HERE")
dbListTables(conn)
dbGetQuery(conn, "SELECT COUNT(1) AS cnt FROM www_access")

If your account is only Batch(Hive) plan, please change type from presto to hive.


Last modified: Jul 07 2016 19:00:56 UTC

If this article is incorrect or outdated, or omits critical information, please let us know. For all other issues, please see our support channels.