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, install the RJDBC Package as shown below:

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

Your Locale Endpoint

Where you plan to run Treasure Data and this feature affects how you access the feature. Access the feature described in this article from the same domain that you access the Treasure Data console.

Region Endpoint Console
US Region api.treasuredata.com console.treasuredata.com
Tokyo Region api.treasuredata.co.jp console.treasuredata.co.jp

Issuing Queries

To issue queries, 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, change type from presto to hive.

NOTE: RJDBC is required to use Username and Passowrd for authentication. Currently, APIKEY is not supported.


Last modified: Jan 18 2018 08:46:12 UTC

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