Is there a common method to change nullable property for all elements of any specified StructType? it might be nested StructType.
I saw @eliasah marked it as duplicate with Spark Dataframe column nullable property change. But they are different, as it cannot solve hierarchy/nested StructType, that answer is only for one level.
for example:
root
|-- user_id: string (nullable = false)
|-- name: string (nullable = false)
|-- system_process: array (nullable = false)
| |-- element: struct (containsNull = false)
| | |-- timestamp: long (nullable = false)
| | |-- process: string (nullable = false)
|-- type: string (nullable = false)
|-- user_process: array (nullable = false)
| |-- element: struct (containsNull = false)
| | |-- timestamp: long (nullable = false)
| | |-- process: string (nullable = false)
I want to change nullalbe to true to all elements, the result should be :
root
|-- user_id: string (nullable = true)
|-- name: string (nullable = true)
|-- system_process: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- timestamp: long (nullable = true)
| | |-- process: string (nullable = true)
|-- type: string (nullable = true)
|-- user_process: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- timestamp: long (nullable = true)
| | |-- process: string (nullable = true)
Attached is a sample of JSON schema of StructType for convenience testing:
val jsonSchema="""{"type":"struct","fields":[{"name":"user_id","type":"string","nullable":false,"metadata":{}},{"name":"name","type":"string","nullable":false,"metadata":{}},{"name":"system_process","type":{"type":"array","elementType":{"type":"struct","fields":[{"name":"timestamp","type":"long","nullable":false,"metadata":{}},{"name":"process_id","type":"string","nullable":false,"metadata":{}}]},"containsNull":false},"nullable":false,"metadata":{}},{"name":"type","type":"string","nullable":false,"metadata":{}},{"name":"user_process","type":{"type":"array","elementType":{"type":"struct","fields":[{"name":"timestamp","type":"long","nullable":false,"metadata":{}},{"name":"process_id","type":"string","nullable":false,"metadata":{}}]},"containsNull":false},"nullable":false,"metadata":{}}]}"""
DataType.fromJson(jsonSchema).asInstanceOf[StructType].printTreeString()