wymeka wymeka - 22 days ago 8
Scala Question

Scala - Spark - How to transform a dataframe containing one string column to a DF with columns with the rigth type?

I'm currently facing a problem that I am unable to solve.
I am using Spark 1.6.

I have a TEXT Dataframe with one column containing a String JSON with a lot of fields.
Some fields must be inferred to String, others to Array and some to Long, according to some schema I've inferred from a correct Json :

{"eventid":"3bc1c5d2-c10f-48d6-8b35-05db8665415c","email":"test@test.com","prices_vat":["20295930","20295930"]}


I only managed to transform it to a df with String columns of fields.
I wasn't able to transform it to the right type.

The wished schema is in df_schema.
The column "value" is containing the String JSON I need to parse.
Here's my code :

var b = sqlContext.createDataFrame(df_txt.rdd,df_schema)
val z= {
b.select( b.columns.map(c => get_json_object(b("value"), s"$$.$c").alias(c)): _*)
}
var c = sqlContext.createDataFrame(z.rdd,df_schema)
c.show(1)


I end up with this exception because the array in the field "prices_vat" is understood as a String and not as an Array like ine the df_schema :

org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 38.0 failed 1 times, most recent failure: Lost task 0.0 in stage 38.0 (TID 32, localhost): scala.MatchError: ["20295930","20295930"] (of class java.lang.String)
at org.apache.spark.sql.catalyst.CatalystTypeConverters$ArrayConverter.toCatalystImpl(CatalystTypeConverters.scala:159)
at org.apache.spark.sql.catalyst.CatalystTypeConverters$ArrayConverter.toCatalystImpl(CatalystTypeConverters.scala:153)
at org.apache.spark.sql.catalyst.CatalystTypeConverters$CatalystTypeConverter.toCatalyst(CatalystTypeConverters.scala:102)
at org.apache.spark.sql.catalyst.CatalystTypeConverters$StructConverter.toCatalystImpl(CatalystTypeConverters.scala:260)
at org.apache.spark.sql.catalyst.CatalystTypeConverters$StructConverter.toCatalystImpl(CatalystTypeConverters.scala:250)
at org.apache.spark.sql.catalyst.CatalystTypeConverters$CatalystTypeConverter.toCatalyst(CatalystTypeConverters.scala:102)
at org.apache.spark.sql.catalyst.CatalystTypeConverters$$anonfun$createToCatalystConverter$2.apply(CatalystTypeConverters.scala:401)
at org.apache.spark.sql.SQLContext$$anonfun$6.apply(SQLContext.scala:492)
at org.apache.spark.sql.SQLContext$$anonfun$6.apply(SQLContext.scala:492)
at scala.collection.Iterator$$anon$11.next(Iterator.scala:328)
at scala.collection.Iterator$$anon$11.next(Iterator.scala:328)
at scala.collection.Iterator$$anon$10.next(Iterator.scala:312)
at scala.collection.Iterator$class.foreach(Iterator.scala:727)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1157)
at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:48)
at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:103)
at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:47)
at scala.collection.TraversableOnce$class.to(TraversableOnce.scala:273)
at scala.collection.AbstractIterator.to(Iterator.scala:1157)
at scala.collection.TraversableOnce$class.toBuffer(TraversableOnce.scala:265)
at scala.collection.AbstractIterator.toBuffer(Iterator.scala:1157)
at scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:252)
at scala.collection.AbstractIterator.toArray(Iterator.scala:1157)
at org.apache.spark.sql.execution.SparkPlan$$anonfun$5.apply(SparkPlan.scala:212)
at org.apache.spark.sql.execution.SparkPlan$$anonfun$5.apply(SparkPlan.scala:212)
at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1858)
at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1858)
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:66)
at org.apache.spark.scheduler.Task.run(Task.scala:89)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:213)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)


Please help me !

Answer

Fortunately Spark has some built functionality for handling JSON data:

scala> val jsonRDD = sc.parallelize(
     |      """{"eventid":"3bc1c5d2-c10f-48d6-8b35-05db8665415c","email":"test@test.com","prices_vat":["20295930","20295930"]}""" :: Nil)
jsonRDD: org.apache.spark.rdd.RDD[String] = ParallelCollectionRDD[8] at parallelize at <console>:27

scala> val df = sqlContext.read.json(jsonRDD)
df: org.apache.spark.sql.DataFrame = [email: string, eventid: string, prices_vat: array<string>]

scala> df.show
+-------------+--------------------+--------------------+
|        email|             eventid|          prices_vat|
+-------------+--------------------+--------------------+
|test@test.com|3bc1c5d2-c10f-48d...|[20295930, 20295930]|
+-------------+--------------------+--------------------+


scala> df.printSchema
root
 |-- email: string (nullable = true)
 |-- eventid: string (nullable = true)
 |-- prices_vat: array (nullable = true)
 |    |-- element: string (containsNull = true)

Also note that if you want Spark to recognize those numbers in the prices_vat field they should be formatted accordingly:

scala> val jsonRDD2 = sc.parallelize(
     |      """{"eventid":"3bc1c5d2-c10f-48d6-8b35-05db8665415c","email":"test@test.com","prices_vat":[20295930,20295930]}""" :: Nil)
jsonRDD2: org.apache.spark.rdd.RDD[String] = ParallelCollectionRDD[18] at parallelize at <console>:27

scala> val df2 = sqlContext.read.json(jsonRDD2)
df2: org.apache.spark.sql.DataFrame = [email: string, eventid: string, prices_vat: array<bigint>]

scala> df2.show
+-------------+--------------------+--------------------+
|        email|             eventid|          prices_vat|
+-------------+--------------------+--------------------+
|test@test.com|3bc1c5d2-c10f-48d...|[20295930, 20295930]|
+-------------+--------------------+--------------------+


scala> df2.printSchema
root
 |-- email: string (nullable = true)
 |-- eventid: string (nullable = true)
 |-- prices_vat: array (nullable = true)
 |    |-- element: long (containsNull = true)

If you have the json in a DataFrame already you can do something like this:

scala> import org.apache.spark.sql.Row
import org.apache.spark.sql.Row

scala> val df = sc.parallelize(
     |      """{"eventid":"3bc1c5d2-c10f-48d6-8b35-05db8665415c","email":"test@test.com","prices_vat":[20295930,20295930]}""" :: Nil).toDF("json")
df: org.apache.spark.sql.DataFrame = [json: string]

scala> df.show
+--------------------+
|                json|
+--------------------+
|{"eventid":"3bc1c...|
+--------------------+


scala> val rdd = df.rdd.map{case Row(json: String) => json}
rdd: org.apache.spark.rdd.RDD[String] = MapPartitionsRDD[43] at map at <console>:30

scala> val outDF = sqlContext.read.json(rdd)
outDF: org.apache.spark.sql.DataFrame = [email: string, eventid: string, prices_vat: array<bigint>]

scala> outDF.show
+-------------+--------------------+--------------------+
|        email|             eventid|          prices_vat|
+-------------+--------------------+--------------------+
|test@test.com|3bc1c5d2-c10f-48d...|[20295930, 20295930]|
+-------------+--------------------+--------------------+
Comments