2016-09-19 57 views
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的?

回答

1

當你寫a1 == StructTypecase StructType,你與稱爲StructType,這是StructType的同伴對象比較。

你需要來匹配,而不是類型:case struct: StructType(或case StructType(fields)),就像你寫case x: String,而不是case String

+0

Thanks @Alexey與case st:StructType匹配 – sachinjain024

相關問題