Calling JDBC to impala/hive from within a spark job and creating a table

user1189851 picture user1189851 · Oct 29, 2014 · Viewed 10.8k times · Source

I am trying to write a spark job in scala that would open a jdbc connection with Impala and let me create a table and perform other operations.

How do I do this? Any example would be of great help. Thank you!

Answer

Ian picture Ian · Aug 18, 2015
val JDBCDriver = "com.cloudera.impala.jdbc41.Driver"
val ConnectionURL = "jdbc:impala://url.server.net:21050/default;auth=noSasl"

Class.forName(JDBCDriver).newInstance
val con = DriverManager.getConnection(ConnectionURL)
val stmt = con.createStatement()
val rs = stmt.executeQuery(query)

val resultSetList = Iterator.continually((rs.next(), rs)).takeWhile(_._1).map(r => {
    getRowFromResultSet(r._2) // (ResultSet) => (spark.sql.Row)
}).toList

sc.parallelize(resultSetList)