scala JDBC 方式连接 impala 代码以及pom.xml文件中的依赖

pom.xml依赖


   com.cloudera
   ImpalaJDBC41
   2.5.42



   org.apache.hive
   hive-jdbc
   1.2.1



   org.apache.hive
   hive-cli
   1.2.1




   org.apache.hive
   hive-metastore
   1.2.1



   org.apache.hive
   hive-common
   1.2.1



   org.apache.hadoop
   hadoop-common
   2.3.0



   org.apache.thrift
   libfb303
   0.9.0



   org.apache.thrift
   libthrift
   0.9.0



   commons-logging
   commons-logging
   1.1.3

注:pom 文件依赖应该还需要zookeeper但是我在其他pom文件中引用了,所以就没有体现。

代码:

import java.sql.{Connection, PreparedStatement, ResultSet}


object ImpalaConnectionJDBC{

  def getConnection()={
    var conn: Connection = null
    var ps: PreparedStatement  = null
    var rs: ResultSet  = null

    val JDBC_DRIVER: String = "com.cloudera.impala.jdbc41.Driver"
    val CONNECTION_URL: String = "jdbc:impala://x.x.x.x:21050"

    import java.sql.{DriverManager, SQLException}
    try {
      Class.forName(JDBC_DRIVER)
      conn = DriverManager.getConnection(CONNECTION_URL)
      ps = conn.prepareStatement("select * from cl")
      rs = ps.executeQuery
      while ( {
        rs.next
      }) System.out.println(rs.getString(1))
    } catch {
      case e: Exception =>
        e.printStackTrace()
    } finally try {
      if (rs != null){

        rs.close()
      }
      if (ps != null){

        ps.close()
      }
      if (conn != null){

        conn.close()
      }
    } catch {
      case e: SQLException =>
        e.printStackTrace()
    }
  }

  def main(args: Array[String]): Unit = {

    getConnection()

  }

}

 

你可能感兴趣的:(scala,impala,scala,pom,jdbc,impala)