Created
September 5, 2019 20:19
-
-
Save nickpoorman/96033a7325d613cb6c222b05caa8b1f0 to your computer and use it in GitHub Desktop.
Scale Flatten DataFrame - what explode should be
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def flattenSchema(schema: StructType, prefix: String = null) : Array[Column] = { | |
schema.fields.flatMap(f => { | |
val colName = if (prefix == null) f.name else (prefix + "." + f.name) | |
f.dataType match { | |
case st: StructType => flattenSchema(st, colName) | |
case _ => Array(col(colName).alias(colName)) | |
} | |
}) | |
} | |
var flattenedDf = json.select(flattenSchema(json.schema):_*) | |
display(flattenedDf) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment