注意:
1、sql 是查询语句,此查询语句必须包含两处占位符?来作为分割数据库ResulSet的参数,例如:”select title, author from books where ? < = id and id <= ?”
2、lowerBound, upperBound, numPartitions 分别为第一、第二占位符,partition的个数
def createConnection(): Connection = {
Class.forName("com.mysql.jdbc.Driver").newInstance()
DriverManager.getConnection("jdbc:mysql://sxjdb01:3306/oozie","root","123456")
}
def extractValues(r: ResultSet): String = {
//
(r.getInt(1), r.getString(2))
(r.getString(1))
}
def main(args: Array[String]): Unit = {
System.setProperty("hadoop.home.dir","F:\spark_JiQun\hadoop-2.6.0")
val conf= new SparkConf().setAppName("the RDD").setMaster("local[2]")
val sc = new SparkContext(conf)
val data = new JdbcRDD(sc, createConnection, "SELECT WF_JOBS.`id` FROM WF_JOBS WHERE ID >= ? AND ID <= ?", lowerBound = 1, upperBound = 2, numPartitions = 2, mapRow = extractValues)
data.foreach(println)
sc.stop()
}
输出结果:1,2(理由->因为我extractValues只返回了一个)
最后
以上就是昏睡玉米最近收集整理的关于spark-JDBCRDD的全部内容,更多相关spark-JDBCRDD内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复