Last active
          February 20, 2020 17:40 
        
      - 
      
- 
        Save ebuildy/736ddb7160b587f6405c88c556287a17 to your computer and use it in GitHub Desktop. 
    Flatten Apache Spark Data Frame
  
        
  
    
      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
    
  
  
    
  | import org.apache.spark.sql.Column | |
| import org.apache.spark.sql.types.StructType | |
| import org.apache.spark.sql.functions.col | |
| 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)) | |
| } | |
| }) | |
| } | |
| val flattenedSchema = flattenSchema(df.schema) | |
| val renamedCols = flattenedSchema.map(name => col(name.toString()).as(name.toString().replace(".","_"))) | |
| val flatDF = df.select(renamedCols:_*) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment