Finally, I found a solution. I can use the trick to save data in MySQL using Spark SQL. The trick is to create a new DataFrame and then save it. Here is an example:
def saveImage(image: Image): Unit = {
val df = sqlContext.createDataFrame {
sc.parallelize(
Image(
name = image.name,
data = image.data
) :: Nil
)
}
JdbcUtils.saveTable(df, url, "images", props)
}
And the model will look like this:
case class Image(
id : Option[Int] = None,
name : String,
data : Array[Byte]
)
source
share