Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to S3 Athena in R via JDBC

Tags:

r

amazon-s3

jdbc

I'm trying to connect with JDBC to Amazon's Athena. Using RJDBC library in R i have the following:

download.file('https://s3.amazonaws.com/athena-downloads/drivers/AthenaJDBC41-1.0.0.jar','AthenaJDBC41-1.0.0.jar' )

jdbcDriver <- JDBC(driverClass="com.amazonaws.athena.jdbc.AthenaDriver", 'AthenaJDBC41-1.0.0.jar',
                identifier.quote="'")

Then running with credentials :

jdbcConnection <- dbConnect(jdbcDriver, 'jdbc:awsathena://athena.us-east-1.amazonaws.com:443/',
              "s3_staging_dir URL", "s3://testbucket/","
              "USERNAME"," USERKEY","PASSWORD","PASSWORDKEY" )

but i keep on getting this error:

Error in .jcall(drv@jdrv, "Ljava/sql/Connection;", "connect", as.character(url)[1],  : 
  java.sql.SQLException: property s3_staging_dir must be set

I tried setting the s3_staging_dr in the connection call but it did not work.

Any guidance would be greatly appreciated.

like image 425
Dynalytics Avatar asked Mar 10 '23 12:03

Dynalytics


2 Answers

library(RJDBC)

URL <- 'https://s3.amazonaws.com/athena-downloads/drivers/AthenaJDBC41-1.0.0.jar'
fil <- basename(URL)
if (!file.exists(fil)) download.file(URL, fil)

drv <- JDBC(driverClass="com.amazonaws.athena.jdbc.AthenaDriver", fil, identifier.quote="'")

con <- jdbcConnection <- dbConnect(drv, 'jdbc:awsathena://athena.us-east-1.amazonaws.com:443/',
                                   s3_staging_dir="s3://yourbucket",
                                   user=Sys.getenv("ATHENA_USER"),
                                   password=Sys.getenv("ATHENA_PASSWORD"))  


dbListTables(con)
## [1] "elb_logs"

Put your access key and seekrit in .Renviron (in the obviously named env vars), restart R and try the above (with a bucket of yours that is accessible.

dbGetQuery(con, "SELECT * FROM sampledb.elb_logs LIMIT 10") %>% 
    dplyr::glimpse()
## Observations: 10
## Variables: 16
## $ timestamp             <chr> "2014-09-27T00:00:25.424956Z", "2014-09-27T00:00:56.439218Z", "2014-09-27T00:01:27.441734Z", "2014-09-27T00:01:58.366715Z", "2014-09-27T00:02:29.446363Z", "2014-09-2...
## $ elbname               <chr> "lb-demo", "lb-demo", "lb-demo", "lb-demo", "lb-demo", "lb-demo", "lb-demo", "lb-demo", "lb-demo", "lb-demo"
## $ requestip             <chr> "241.230.198.83", "252.26.60.51", "250.244.20.109", "247.59.58.167", "254.64.224.54", "245.195.140.77", "245.195.140.77", "243.71.49.173", "240.139.5.14", "251.192.4...
## $ requestport           <dbl> 27026, 27026, 27026, 27026, 27026, 27026, 27026, 27026, 27026, 27026
## $ backendip             <chr> "251.192.40.76", "249.89.116.3", "251.111.156.171", "251.139.91.156", "251.111.156.171", "254.64.224.54", "254.64.224.54", "250.244.20.109", "247.65.176.249", "250.2...
## $ backendport           <dbl> 443, 8888, 8888, 8888, 8000, 8888, 8888, 8888, 8888, 8888
## $ requestprocessingtime <dbl> 9.1e-05, 9.4e-05, 8.4e-05, 9.7e-05, 9.1e-05, 9.3e-05, 9.4e-05, 8.3e-05, 9.0e-05, 9.0e-05
## $ backendprocessingtime <dbl> 0.046598, 0.038973, 0.047054, 0.039845, 0.061461, 0.037791, 0.047035, 0.048792, 0.045724, 0.029918
## $ clientresponsetime    <dbl> 4.9e-05, 4.7e-05, 4.9e-05, 4.9e-05, 4.0e-05, 7.7e-05, 7.5e-05, 7.3e-05, 4.0e-05, 6.7e-05
## $ elbresponsecode       <chr> "200", "200", "200", "200", "200", "200", "200", "200", "200", "200"
## $ backendresponsecode   <chr> "200", "200", "200", "200", "200", "400", "400", "200", "200", "200"
## $ receivedbytes         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
## $ sentbytes             <dbl> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
## $ requestverb           <chr> "GET", "GET", "GET", "GET", "GET", "GET", "GET", "GET", "GET", "GET"
## $ url                   <chr> "http://www.abcxyz.com:80/jobbrowser/?format=json&state=running&user=20g578y", "http://www.abcxyz.com:80/jobbrowser/?format=json&state=running&user=20g578y", "http:/...
## $ protocol              <chr> "HTTP/1.1", "HTTP/1.1", "HTTP/1.1", "HTTP/1.1", "HTTP/1.1", "HTTP/1.1", "HTTP/1.1", "HTTP/1.1", "HTTP/1.1", "HTTP/1.1"
like image 134
hrbrmstr Avatar answered Mar 13 '23 12:03

hrbrmstr


@Mike.Gahan

I ran into the same issue. I solved it by making sure my Java JDK was 8.0+ and use AthenaJDBC42-2.0.7.jar. You can download the via AthenaJDBC42-2.0.7.jar

You can check your current JDK version with:

java -version

Note: a lot of the instructions online said to use “com.amazonaws.athena.jdbc.AthenaDriver” for the driverClass. I was not able to get it to work with that. Instead I tried “com.simba.athena.jdbc.Driver” and I was able to connect to the JDBC.

The code below is what I used to get the connector to run.

library(rJava)
library(RJDBC)
library(plyr)
library(dplyr)

drv <- JDBC(driverClass="com.simba.athena.jdbc.Driver", "AthenaJDBC42_2.0.7.jar", identifier.quote="'")

#connect to Athena using the driver, S3 working directory and credentials for Athena 
#replace ‘athenauser’ below with prefix you have set up for your S3 bucket
con <- jdbcConnection <- dbConnect(drv, 'jdbc:awsathena://athena.us-west-2.amazonaws.com:443/',
                                   s3_staging_dir="s3://xxxxx",
                                   user='xxxxx',
                                   password='xxxxxx')

# get a list of all tables currently in Athena 
dbListTables(con)
# run a sample query
dfelb=dbGetQuery(con, "SELECT * FROM sample limit 10")
head(dfelb,2)

Additionally, you can use the odbc r library as well. Using the Simba ODBC driver

You might need alien to install the RPM file:

sudo apt-get install alien
sudo alien -i simbaathena-1.0.5.1006-1.x86_64.rpm

Note: You must have iODBC 3.52.9, 3.52.10, 3.52.11, or 3.52.12 or unixODBC 2.3.2, 2.3.3, or 2.3.4 installed. I used IODBC with ubuntu 18.04 64bit and installed it via:

sudo apt-get install libxml2-dev

You can check if you installed the Simba Athena ODBC Driver is installed with:

dpkg -l | grep simbaathenaodbc

The code to use the odbc:

library(odbc)
library(tidyverse)

DBI::dbConnect(
  odbc::odbc(), 
  driver = "/opt/simba/athenaodbc/lib/64/libathenaodbc_sb64.so", 
  Schema = "default",
  AwsRegion = "us-west-2",
  AuthenticationType = "Default Credentials",
  S3OutputLocation = "s3://xxxx"
) -> con

(employee <- tbl(con, sql("SELECT * FROM test”)))
like image 45
rreg101 Avatar answered Mar 13 '23 12:03

rreg101