This article shows how to use Treasure Data with the R language by using the RPresto package.
install.packages(c("RPresto", "devtools", "dplyr", "dbplyr", "ggplot2"))
devtools::install_github("treasure-data/RTD")
devtools::install_github("crowding/msgpack-r")In cases where RODBC lib is used for R Language, use RJDBC lib instead. RJDBC allows you to configure a generic Trino/Presto JDBC Driver. You'll be able to access Treasure Data Trino from R Language.
You can use the endpoints below to access this feature. You can point to Presto JDBC for RPresto and API might be used by RTD. Learn more about Treasure Data Sites and Endpoints.
You can query with the following examples. Assuming there is a ‘flights’ table in ‘test’ database, you then need to set an environment variable <TD_API_KEY> for your TD API key.
To use a different region, replace host with the desired region.
library(RPresto)
library(dplyr)
db <- src_presto(
host="https://api-presto.treasuredata.com",
port=443,
user=Sys.getenv("TD_API_KEY"),
schema='test',
catalog='td-presto'
)
flights_tbl <- tbl(db, 'flights')
# filter by departure delay and show result
flights_tbl %>% filter(dep_delay == 2)library(DBI)
con <- dbConnect(
RPresto::Presto(),
host="https://api-presto.treasuredata.com",
port=443,
user=Sys.getenv("TD_API_KEY"),
schema='test',
catalog='td-presto'
)
# write your query with dbGetQuery function
flights_preview <- dbGetQuery(con, 'SELECT year, month, day, dep_time, dep_delay, carrier, flight from flights limit 10')
# show query result
flights_previewView an example notebook.