从模式字符串Java-Spark创建JSON模式 [英] Create JSON schema from schema string Java-Spark
本文介绍了从模式字符串Java-Spark创建JSON模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下架构:
root
|-- id: string (nullable = true)
|-- text: string (nullable = true)
|-- user: string (nullable = true)
如何从此字符串创建 StructType
模式?
How can I create StructType
schema from this String?
我知道我可以在数据集中使用 .schema()
方法,但是我问是否可以从字符串创建模式.
I know that I can use .schema()
method in my Dataset but I asking If its possible to create Schema from string.
推荐答案
我需要类似的东西,因此我编写了一个方法,该方法可以从字符串创建模式.但是您必须针对自己的情况更改此方法.
I was need something like that and i wrote a method, this able to creating the schema from string. But you must change this method for your scenario.
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)
}))
这篇关于从模式字符串Java-Spark创建JSON模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文