Spark parquet nested value flatten

☆樱花仙子☆ 提交于 2019-12-25 06:49:56

问题


I have parquet file. I loaded using Spark.And one of the value is nested key,value pairs. How do I flatten?

df.printSchema
root
|-- location: string (nullable = true)
|-- properties: string (nullable = true)


texas,{"key":{"key1":"value1","key2":"value2"}}

thanks,


回答1:


You can use explode on your dataframe and pass it a function that reads the JSON column using scala4s. Scala4s has easy parsing API, for your case it will look like:

val list = for {
  JArray(keys) <- parse(json) \\ "key"
  json @ JObject(key) <- keys
  JField("key1", JString(key1)) <- key
  JField("key2", JString(key2)) <- key
} yield {
  Seq(key1, key2)
}

This flattens your dataframe.

If you also want to add column for key, you can use withColumn after explode(keep the key also in the new column).



来源:https://stackoverflow.com/questions/37003083/spark-parquet-nested-value-flatten

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!