Create spark dataframe schema from json schema representation

后端 未结 2 482
不思量自难忘°
不思量自难忘° 2020-12-04 16:40

Is there a way to serialize a dataframe schema to json and deserialize it later on?

The use case is simple: I have a json configuration file which contains the sche

2条回答
  •  不思量自难忘°
    2020-12-04 17:35

    There are two steps for this: Creating the json from an existing dataframe and creating the schema from the previously saved json string.

    Creating the string from an existing dataframe

        val schema = df.schema
        val jsonString = schema.json
    

    create a schema from json

        import org.apache.spark.sql.types.{DataType, StructType}
        val newSchema = DataType.fromJson(jsonString).asInstanceOf[StructType]
    

提交回复
热议问题