Building a StructType from a dataframe in pyspark

后端 未结 4 1136
后悔当初
后悔当初 2021-02-04 06:45

I am new spark and python and facing this difficulty of building a schema from a metadata file that can be applied to my data file. Scenario: Metadata File for the Data file(csv

4条回答
  •  天涯浪人
    2021-02-04 07:24

    Below steps can be followed to change the Datatype Objects

    data_schema=[
        StructField("age", IntegerType(), True),
        StructField("name", StringType(), True)
    ]
    
    
    
    final_struct=StructType(fields=data_schema)
    
    df=spark.read.json('/home/abcde/Python-and-Spark-for-Big-Data-master/Spark_DataFrames/people.json', schema=final_struct)
    
    
    
    df.printSchema()
    
    root
     |-- age: integer (nullable = true)
     |-- name: string (nullable = true)
    

提交回复
热议问题