java.lang.IllegalArgumentException:无法获取array< string>的JDBC类型. [英] java.lang.IllegalArgumentException: Can't get JDBC type for array<string>

查看:559
本文介绍了java.lang.IllegalArgumentException:无法获取array< string>的JDBC类型.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将输出数据导入到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

这是控制台输出

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)

我想将以下数据存储在mysql数据库中

I want to store the following data in the mysql database

+---------+-----+--------------+
|     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|
+---------+-----+--------------+

但是第一列是一个数组,因此程序出现错误

您能帮我提出变更计划吗?谢谢

推荐答案

在写入数据库之前,需要删除array类型的columns.

You need to remove the columns with array type before writing to the databases.

您可以创建一个string,用逗号分隔列类型arraystring

You can create a string with comma separated for the column type array as

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)

concat_ws使用提供的定界符在array中创建值作为字符串.

concat_ws creates a string the values in an array with delimiter provided.

希望这会有所帮助!

这篇关于java.lang.IllegalArgumentException:无法获取array&lt; string&gt;的JDBC类型.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆