1

I'd like to save a JSON to a table in MySQL.

After a bit of reading I found out that the path to load the data into mysql is json->dataframe->mysql.

{"name":"Johny","hobbies":["swiming","cooking"]}
{"name":"James","hobbies":["baseketball","fishing"]}
{"name":"Tom","hobbies":["singing","football"]}

I read the json file be using below command:

val df = sqlContext.read.json("test.json")
df.show()
df.printSchema()

and output:

+--------------------+-----+                                                    
|             hobbies| name|
+--------------------+-----+
|  [swiming, cooking]|Johny|
|[baseketball, fis...|James|
| [singing, football]|  Tom|
+--------------------+-----+

root
 |-- hobbies: array (nullable = true)
 |    |-- element: string (containsNull = true)
 |-- name: string (nullable = true)

When using below command:

df.registerTempTable("mytable")
sqlContext.
  sql("SELECT * FROM mytable").
  write.
  mode(SaveMode.Append).
  jdbc(url,"jsontest",prop)

I am getting below error:

java.lang.IllegalArgumentException: Can't get JDBC type for array

How can I convert the array of strings to one string like swiming, cooking in the DataFrame?

1
  • What Spark version do you use? Commented Jun 28, 2017 at 4:04

2 Answers 2

4

Convert the array of string to a string using a simple udf

 import org.apache.spark.sql.functions._

 val value = udf((arr: Seq[String]) => arr.mkString(","))

 val newDf = df.withColumn("hobbies", value($"hobbies"))

Or you can also use the concat_ws function as as said by Jacek

df.withColumn("hobbies", concat_ws(col("hobbies")))

Output:

+--------------------+-----+                                                    
|             hobbies| name|
+--------------------+-----+
|  swiming, cooking  |Johny|
|baseketball, fishing|James|
| singing, football  |  Tom|
+--------------------+-----+

Then save the newDF as

newDF.write.mode(SaveMode.Append).jdbc(url,"jsontest",prop)
Sign up to request clarification or add additional context in comments.

6 Comments

What's $"hobbies".mkString?
Thanks Shankar Koiralafor you reply.but I get the error.Error:(45, 41) value $ is not a member of StringContext val newDF = df.withColumn("hobbies",$"hobbies".mkString)
Thanks for your time anyway.
I can't build it successfully.
I import the functions.but I get the error.Error:(47, 48) value $ is not a member of StringContext val newDf = df.withColumn("hobbies", value($"hobbies"))
|
4

How can I convert the array of strings to one string like swiming, cooking in the DataFrame?

You should use the built-in concat_ws function.

concat_ws(sep: String, exprs: Column*): Column Concatenates multiple input string columns together into a single string column, using the given separator.

A solution would then be as follows.

val hobbies = Seq(
  (Array("swiming","cooking"), "Johny"),
  (Array("baseketball","fishing"), "James"),
  (Array("singing","football"), "Tom")
).toDF("hobbies", "name")

val solution = hobbies.select(concat_ws(",", $"hobbies") as "hobbies", $"name")
scala> solution.show
+-------------------+-----+
|            hobbies| name|
+-------------------+-----+
|    swiming,cooking|Johny|
|baseketball,fishing|James|
|   singing,football|  Tom|
+-------------------+-----+

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.