import java.sql.{Connection, Driver, DriverManager, PreparedStatement, ResultSet}
import Utils.SparkUtils
import org.apache.spark.SparkContext
import org.apache.spark.rdd.RDD
object MapDemo01 {
def main(args: Array[String]): Unit = {
val sc: SparkContext = SparkUtils.getSparkContext()
val rdd1: RDD[Int] = sc.makeRDD(List(10,20,30,40),2)
// classOf[Driver]
val rdd2: RDD[String] =rdd1.map(uid=>{
val conn: Connection =DriverManager.getConnection("jdbc:mysql://localhost:3306/mq01","root","123456" )
val ps: PreparedStatement =conn.prepareStatement("select * from dept where DEPTNO= ? ")
ps.setInt(1,uid)
val set: ResultSet =ps.executeQuery()
var name: String =""
if(set.next()){
val DNAME: String =set.getString("DNAME")
name=DNAME
}
set.close()
ps.close()
conn.close()
name
})
rdd2.foreach(println)
sc.stop()
}
}
map算子
最新推荐文章于 2024-05-28 22:46:39 发布