Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.40 KB | None | 0 0
  1. def flattenSchema(schema: StructType, prefix: String = null) : Array[Column] = {
  2. schema.fields.flatMap(f => {
  3. val colName = if (prefix == null) f.name else (prefix + "." + f.name)
  4.  
  5. f.dataType match {
  6. case st: StructType => flattenSchema(st, colName)
  7. case _ => Array(col(colName).alias(colName))
  8. }
  9. })
  10. }
  11.  
  12. var flattenedDf = json.select(flattenSchema(json.schema):_*)
  13. display(flattenedDf)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement