在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()将模式打印为树,但我需要重用该模式,并将其定义为上面,这样我就可以使用以前从另一个数据源推断出来的模式读取数据源。
发布于 2019-02-03 13:06:43
是的是可能的。使用DataFrame.schema property
schema将此DataFrame的架构作为pyspark.sql.types.StructType.返回。df.schema StructType(StructField(年龄,IntegerType,真),StructField(姓名,StringType,真)) 新版本1.3。
发布于 2020-02-09 20:06:07
下面的代码将为您提供一个格式良好的已知dataframe的表格模式定义。当您有大量的列时非常有用&在这些列中,编辑非常繁琐。然后,您现在可以将其应用到新的dataframe &手动编辑任何您可能希望相应的列。
from pyspark.sql.types import StructType
schema = [i for i in df.schema] 然后,从这里开始,您将得到新的模式:
NewSchema = StructType(schema)发布于 2020-12-14 15:58:21
如果您正在从PySpark中寻找DDL字符串:
df: DataFrame = spark.read.load('LOCATION')
schema_json = df.schema.json()
ddl = spark.sparkContext._jvm.org.apache.spark.sql.types.DataType.fromJson(schema_json).toDDL()https://stackoverflow.com/questions/54503014
复制相似问题