在 PySpark 中,我不想硬編碼模式定義,我想從下面的變數中派生模式。
mySchema=[("id","IntegerType()", True),
("name","StringType()", True),
("InsertDate","TimestampType()", True)
]
result = mySchema.map(lambda l: StructField(l[0],l[1],l[2]))
我如何實作這個邏輯來生成structTypeSchemafrom mySchema?
預期輸出:
structTypeSchema = StructType(fields=[
StructField("id", IntegerType(), True),
StructField("name", StringType(), True),
StructField("InsertDate",TimestampType(), True)])
uj5u.com熱心網友回復:
您可以嘗試以下方法:
from pyspark.sql import types as T
structTypeSchema = T.StructType(
[T.StructField(f[0], eval(f'T.{f[1]}'), f[2]) for f in mySchema]
)
或者
from pyspark.sql.types import *
structTypeSchema = StructType(
[StructField(f[0], eval(f[1]), f[2]) for f in mySchema]
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/486899.html
