我想将输出数据导入mysql数据库,但是出现以下错误,我不会将数组转换为所需的字符串类型,可以帮帮我吗?
val Array(trainingData, testData) = msgDF.randomSplit(Array(0.9, 0.1))
val pipeline = new Pipeline().setStages(Array(labelIndexer, word2Vec, mlpc, labelConverter))
val model = pipeline.fit(trainingData)
val predictionResultDF = model.transform(testData)
val rows = predictionResultDF.select("song", "label", "predictedLabel")
val df = rows.registerTempTable("song_classify")
val sqlcommand = "select * from song_classify"
val prop = new java.util.Properties
prop.setProperty("user", "root")
prop.setProperty("password", "123")
sqlContext.sql(sqlcommand)
.write.mode(SaveMode.Append).jdbc("jdbc:mysql://localhost:3306/yuncun", "song_classify", prop)
sc.stop
Run Code Online (Sandbox Code Playgroud)
这是控制台输出
Exception in thread "main" java.lang.IllegalArgumentException: Can't get JDBC type for array<string>
at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$$anonfun$org$apache$spark$sql$execution$datasources$jdbc$JdbcUtils$$getJdbcType$2.apply(JdbcUtils.scala:148)
at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$$anonfun$org$apache$spark$sql$execution$datasources$jdbc$JdbcUtils$$getJdbcType$2.apply(JdbcUtils.scala:148)
at scala.Option.getOrElse(Option.scala:121)
Run Code Online (Sandbox Code Playgroud)
我想在mysql数据库中存储以下数据
+---------+-----+--------------+
| song|label|predictedLabel|
+---------+-----+--------------+
| [????]| 1| 2|
| [?????]| 1| 2|
| [????]| 1| 2|
| [?????]| 1| 2|
|[???????]| 2| 2|
| [?????]| 2| 2|
| [??]| 2| 2|
| [??]| 2| 2|
| [????]| 2| 2|
| [??]| 2| 2|
| [????]| 2| 2|
| [????]| 2| 2|
| [???]| 2| 2|
| [??]| 2| 2|
| [????]| 2| 2|
+---------+-----+--------------+
Run Code Online (Sandbox Code Playgroud)
但是第一列是数组,所以程序报错
你能帮我提出一个改变计划吗?谢谢
在写入数据库之前,您需要删除columnswitharray类型。
您可以创建一个string用于列类型以逗号分隔array为
val datafrme = ??
import org.apache.spark.sql.functions._
dataframe.withColumn("song", concat_ws(",", $"song"))
// then write to database
.write.mode(SaveMode.Append).jdbc("url", "song_classify", prop)
Run Code Online (Sandbox Code Playgroud)
concat_ws创建一个字符串,其中包含array提供的分隔符中的值。
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
4713 次 |
| 最近记录: |