我有以下架构:
root
|-- id: string (nullable = true)
|-- text: string (nullable = true)
|-- user: string (nullable = true)
如何StructType从此字符串创建架构?
我知道我可以.schema()在我的数据集中使用方法,但我问是否可以从字符串创建模式。
我有以下架构:
root
|-- id: string (nullable = true)
|-- text: string (nullable = true)
|-- user: string (nullable = true)
如何StructType从此字符串创建架构?
我知道我可以.schema()在我的数据集中使用方法,但我问是否可以从字符串创建模式。
我需要这样的东西,我写了一个方法,它能够从字符串创建模式。但是您必须针对您的方案更改此方法。
val schemaString = "val1#Int val2#String val3#Int"
val schema = StructType(schemaString.split(" ").map(fieldNameTypeStr => {
val fieldNameType = fieldNameTypeStr.split("#")
val dataType: DataType = fieldNameType(1) match {
case "String" => StringType
case "Int" => IntegerType
/* other cases */
}
StructField(fieldNameType(0), dataType, true)
}))