spark streaming怎么将计算结果写入oracle
2016-03-08
展开全部
你试试,看这样可以不!
object Test {
case class Person(words: String,number:Int)
def main(args: Array[String]) {
val sparkConf = new SparkConf().setAppName("TextStream")
val sc = new SparkContext(sparkConf)
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance()
val conn = DriverManager.getConnection("jdbc:oracle:thin:@host/oracledb", "user", "password")
var ps: PreparedStatement = null
val ssc = new StreamingContext(sparkConf, Seconds(6))
// Create the FileInputDStream on the directory and use the
val lines = ssc.textFileStream("/user/input/")
val words = lines.flatMap(_.split(","))
val wordCounts = words.map(x => (x, 1))
// wordCounts.print()
wordCounts.foreachRDD((rdd:RDD[(String,Int)])=>{
rdd.foreach(lines=>{
ps = conn.prepareStatement("insert into testtables values(?,?)")
ps.setString(1, lines._1)
ps.setInt(2, lines._2)
ps.executeUpdate()
})
})
}
}
object Test {
case class Person(words: String,number:Int)
def main(args: Array[String]) {
val sparkConf = new SparkConf().setAppName("TextStream")
val sc = new SparkContext(sparkConf)
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance()
val conn = DriverManager.getConnection("jdbc:oracle:thin:@host/oracledb", "user", "password")
var ps: PreparedStatement = null
val ssc = new StreamingContext(sparkConf, Seconds(6))
// Create the FileInputDStream on the directory and use the
val lines = ssc.textFileStream("/user/input/")
val words = lines.flatMap(_.split(","))
val wordCounts = words.map(x => (x, 1))
// wordCounts.print()
wordCounts.foreachRDD((rdd:RDD[(String,Int)])=>{
rdd.foreach(lines=>{
ps = conn.prepareStatement("insert into testtables values(?,?)")
ps.setString(1, lines._1)
ps.setInt(2, lines._2)
ps.executeUpdate()
})
})
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询