0
我想知道DataFrame中的列是否屬於StructType。我有DataFrame的架構。我嘗試使用下面的代碼如何檢查dataFrame中的列是否屬於StructType
df.schema.apply(1) match {
case StringType => // Do Something
case ? => // How to check if 1st column is of StructType
}
例如,考慮一下這種情況:
val personStructType =
StructType(
StructField("name", StringType, nullable = true, metadata = new MetadataBuilder().putBoolean("isPrimary", false).build) ::
StructField("age", IntegerType, nullable = true, metadata = new MetadataBuilder().putBoolean("isPrimary", false).build) ::
StructField("gender", StringType, nullable = true, metadata = new MetadataBuilder().putBoolean("isPrimary", false).build) ::
Nil
)
val idStructType =
StructType(
StructField("domain", StringType, nullable = true, metadata = new MetadataBuilder().putBoolean("isPrimary", false).build) ::
StructField("id", StringType, nullable = true, metadata = new MetadataBuilder().putBoolean("isPrimary", false).build) ::
Nil
)
val schema =
StructType(
StructField("a", StringType, nullable = true, new MetadataBuilder().putBoolean("isPrimary", true).build) ::
StructField("person", personStructType, nullable = true, metadata = new MetadataBuilder().putBoolean("isPrimary", false).build) ::
StructField("identifier", idStructType, nullable = true, metadata = new MetadataBuilder().putBoolean("isPrimary", false).build) ::
Nil
)
val a0 = schema.apply(0).dataType
a0 == StringType // Result is true
val a1 = schema.apply(1).dataType
a1 == StructType // Result is false
因爲A1是StructType(StructField(name,StringType,true), StructField(age,IntegerType,true), StructField(gender,StringType,true))
我怎麼知道如果A1是StructType的?
Thanks @Alexey與case st:StructType匹配 – sachinjain024