Forked from isogram/create_empty_dataframe_pyspark.py
Created
September 19, 2022 16:40
-
-
Save vaquarkhan/00bb89c2b0d3d61ac55ab0d7a1d4b676 to your computer and use it in GitHub Desktop.
Pyspark Create Empty Dataframe
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
| from pyspark.sql.types import StringType, FloatType, StructField, StructType | |
| from pyspark.sql import SparkSession, SQLContext, Row | |
| import pyspark | |
| # spark initialization | |
| spark_context = pyspark.SparkContext.getOrCreate() | |
| spark_session = SparkSession(spark_context) \ | |
| .builder \ | |
| .enableHiveSupport() \ | |
| .getOrCreate() | |
| sqlContext = SQLContext(spark_context) | |
| field = [ | |
| StructField("FIELDNAME_1",StringType(), True), | |
| StructField("FIELDNAME_2", FloatType(), True), | |
| StructField("FIELDNAME_3", StringType(), True) | |
| ] | |
| schema = StructType(field) | |
| df = sqlContext.createDataFrame(spark_context.emptyRDD(), schema) | |
| df.printSchema() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment