mah*_*pal 4 scala apache-spark
如何从spark中的嵌套结构类型中提取列名和数据类型
模式变得像这样:
(events,StructType(
StructField(beaconType,StringType,true),
StructField(beaconVersion,StringType,true),
StructField(client,StringType,true),
StructField(data,StructType(
StructField(ad,StructType(
StructField(adId,StringType,true)
)
)
)
Run Code Online (Sandbox Code Playgroud)
我想转换成下面的格式
Array[(String, String)] = Array(
(client,StringType),
(beaconType,StringType),
(beaconVersion,StringType),
(phase,StringType)
Run Code Online (Sandbox Code Playgroud)
你能帮忙吗
问题有点不清楚,但如果您正在寻找一种“扁平化”DataFrame 模式的方法(即获取所有非结构字段的数组),这里有一个:
def flatten(schema: StructType): Array[StructField] = schema.fields.flatMap { f =>
f.dataType match {
case struct: StructType => flatten(struct)
case _ => Array(f)
}
}
Run Code Online (Sandbox Code Playgroud)
例如:
val schema = StructType(Seq(StructField("events",
StructType(Seq(
StructField("beaconVersion", IntegerType, true),
StructField("client", StringType, true),
StructField("data", StructType(Seq(
StructField("ad", StructType(Seq(
StructField("adId", StringType, true)
)))
)))
)))
))
println(flatten(schema).toList)
// List(StructField(beaconVersion,IntegerType,true), StructField(client,StringType,true), StructField(adId,StringType,true))
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16200 次 |
最近记录: |