Created
January 25, 2017 12:53
-
-
Save nickwhite917/62b8a12e5172a60ceede52c4efc9d691 to your computer and use it in GitHub Desktop.
Connect to Hive tables from R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ### Load the necessary libraries | |
| library("DBI") | |
| library("rJava") | |
| library("RJDBC") | |
| queryHive <- function(query){ | |
| classpath = c("/usr/lib/hive/lib/hive-jdbc.jar", "/usr/lib/hadoop/client/hadoop-common.jar", "/usr/lib/hive/lib/libthrift-0.9.2.jar", "/usr/lib/hive/lib/hive-service.jar", "/usr/lib/hive/lib/httpclient-4.2.5.jar", "/usr/lib/hive/lib/httpcore-4.2.5.jar", "/usr/lib/hive/lib/hive-jdbc-standalone.jar") | |
| .jinit(classpath=classpath) | |
| driver <- JDBC("org.apache.hive.jdbc.HiveDriver", "/usr/lib/hive/lib/hive-jdbc.jar", identifier.quote="`") | |
| connection <- dbConnect(driver, "jdbc:hive2://servername:10000/database", "username", "password") | |
| result <- dbGetQuery(connection, query) | |
| return(result) | |
| } | |
| #tables <- queryHive("show tables") | |
| #descriptionOfATable <- queryHive("describe users") | |
| result <- queryHive("select * from default.users where session_id != 'session_id' limit 100") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment