在 PySpark 中,您可以定义一个架构并使用此预定义架构读取数据源,例如。例如:
Schema = StructType([ StructField("temperature", DoubleType(), True),
StructField("temperature_unit", StringType(), True),
StructField("humidity", DoubleType(), True),
StructField("humidity_unit", StringType(), True),
StructField("pressure", DoubleType(), True),
StructField("pressure_unit", StringType(), True)
])
对于某些数据源,可以从数据源推断架构并获取具有此架构定义的数据帧。
是否可以从之前已推断出数据的数据帧中获取架构定义(以上述形式)?
df.printSchema()
将模式打印为树,但我需要重用该模式,并按上面的方式定义它,这样我就可以使用该模式读取已经被之前从另一个数据源推断出。
最佳答案
是的,这是可能的。使用DataFrame.schema
property
schema
Returns the schema of this DataFrame as a pyspark.sql.types.StructType.
>>> df.schema StructType(List(StructField(age,IntegerType,true),StructField(name,StringType,true)))
New in version 1.3.
关于apache-spark - 如何从 PySpark 中的数据帧获取架构定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54503014/