Apache spark 如何使用多种类型的对象数组为Spark模式构建正确的StructType?
我所拥有的结构:Apache spark 如何使用多种类型的对象数组为Spark模式构建正确的StructType?,apache-spark,apache-spark-sql,Apache Spark,Apache Spark Sql,我所拥有的结构: { "person":"Jhon", [ {"model":"golf","mark":"vw","fuel":"d","year":2000}, {"model":"yamaha r1","year":1998,&quo
{
"person":"Jhon",
[
{"model":"golf","mark":"vw","fuel":"d","year":2000},
{"model":"yamaha r1","year":1998,"abs":true}
]
}
//
StructType(
StructField("person", StringPrimitive, false),
ArrayType( /* ???? */ ) // Should be possible to define several structures
)
所以,这个想法很简单。有些人有自己的交通工具,这种交通工具可以是:CarEntity、MotorcycleEntity、BicycleEntity、AirplaneEntity等等
第二种情况是,我每个人只收到一个运输实体。Smth是这样的:
{"person":"Jhon",{"model":"yamaha r1","year":1998,"abs":true}}
//
StructType(
StructField("person", StringPrimitive, false),
StructField( /* ???? */ ) // Should be possible to define several structures
)
在这种情况下,传输可以是前面提到的类型之一
构建StructType的正确方法是什么
如何定义多种类型的
ArrayType
或多种StructType
类型?如果您使用的是Spark 1.x,您可以尝试使用UDT获得相同的结果(如本文所述)。但是对于Spark 2.x,此功能已被删除。此功能有效吗json@SomeshwarKale,这是一个示例,但为了更好地理解,我做了一些更改。我有Spark 2.4,正如我在Spark-catalyst_2.11:2.4.6中看到的,UserDefinedType
存在。@rudyverven您知道如何使用UDT吗?