Skip to content

Instantly share code, notes, and snippets.

@nickpoorman
Created September 5, 2019 20:19
Show Gist options
  • Save nickpoorman/96033a7325d613cb6c222b05caa8b1f0 to your computer and use it in GitHub Desktop.
Save nickpoorman/96033a7325d613cb6c222b05caa8b1f0 to your computer and use it in GitHub Desktop.
Scale Flatten DataFrame - what explode should be
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