Search code examples
scalajdbcapache-sparkimpala

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


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!


Solution

  • 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)