spark 插入數據到mysql時遇到的問題 org.apache.spark.SparkException: Task not serializable

報錯問題:
Exception in thread "main" org.apache.spark.SparkException: Task not serializable
Caused by: java.io.NotSerializableException: org.apache.commons.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper

出錯的代碼:
def saveMonthToMysql(everymonth_avg:RDD[(String, Float, String)])={
  DBs.setup()
  DB.localTx(implicit session =>{
    everymonth_avg.foreach(r=>{
      SQL("insert into price_month(name, avgprice, uploaddate) values(?,?,?)")
        .bind(r._1,r._2,r._3)
        .update()
        .apply()
    })
  }
  )
}
 
猜想緣由多是傳入RDD了
解決方法:
新建一個scala class  saveMonthToMysql放到裏面而且將傳入的參數改爲
(String, Float, String)
而不是
RDD[(String, Float, String)])
object Save {
  DBs.setup()
def saveMonthToMysql(everymonth_avg:(String, Float, String))={
    DB.localTx(implicit session =>{
      SQL("insert into price_month_copy1(name, avgprice, uploaddate) values(?,?,?)")
        .bind(everymonth_avg._1,everymonth_avg._2,everymonth_avg._3)
        .update()
        .apply()
    }
    )
  }
  }

使用的時候用rdd調用就能夠java

 
everymonth_avg.foreach(x=>{
      Save.saveMonthToMysql(x)
    })
相關文章
相關標籤/搜索