import java.sql.{Connection, DriverManager, PreparedStatement, ResultSet}
import Utils.SparkUtils
import org.apache.spark.SparkContext
import org.apache.spark.rdd.RDD
/*
* 一个分区处理一次操作
* 在获取JDBC连接的时候 可以减少连接次数
* 占用内存比较大 ,有可能提升内存溢出的风险*/
object Demo04 {
def main(args: Array[String]): Unit = {
val sc: SparkContext =SparkUtils.getSparkContext()
val rdd: RDD[Int] =sc.makeRDD(List(10,20,30,40),2)
val rdd1 =rdd.mapPartitions(mp=>{
val conn: Connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mq01","root","123456")
val ps: PreparedStatement =conn.prepareStatement("select * from dept where DEPTNO=?")
val names : Iterator[String] =for(elem<-mp) yield {
ps.setInt(1,elem)
var name=""
val set: ResultSet =ps.executeQuery()
if(set.next()){
val DNAME: String =set.getString("DNAME")
name=DNAME
}
set.close()
name
}
names
})
rdd1.foreach(println)
sc.stop()
}
}
Jdbc连接
最新推荐文章于 2024-11-07 19:20:36 发布