Created
April 29, 2020 13:15
-
-
Save napsternxg/7b5f1cd4af3178f235008e24ae6ec748 to your computer and use it in GitHub Desktop.
Print pyspark schema in indented format
This file contains 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 print_schema(schema, level=0): | |
for item in schema: | |
elementType = None | |
elementTypeName = "" | |
if hasattr(item.dataType, "elementType"): | |
elementType = item.dataType.elementType | |
elementTypeName = item.dataType.elementType.typeName() | |
#print("{}{}".format("\t"*level, item)) | |
print("{}{}\t{}\t{}\t{}".format( | |
"\t"*level, | |
item.name, | |
item.dataType.typeName(), | |
elementTypeName, | |
elementType | |
)) | |
if elementType is not None: | |
if item.dataType.elementType.typeName() == "struct": | |
print_schema(item.dataType.elementType.fields, level=level+1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment