Forked from stefanthoss/export-pyspark-schema-to-json.py
Created
September 23, 2020 04:22
-
-
Save germayneng/1bd1dddc981453396aa09d18632f6cff to your computer and use it in GitHub Desktop.
Export/import a PySpark schema to/from a JSON file
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 json | |
from pyspark.sql.types import * | |
# Define the schema | |
schema = StructType( | |
[StructField("name", StringType(), True), StructField("age", IntegerType(), True)] | |
) | |
# Write the schema | |
with open("schema.json", "w") as f: | |
json.dump(schema.jsonValue(), f) | |
# Read the schema | |
with open("schema.json") as f: | |
new_schema = StructType.fromJson(json.load(f)) | |
print(new_schema.simpleString()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment