![](/img/trans.png)
[英]How to define a spark Schema for a List of objects i.e. StructType?
[英]How to build right StructType for Spark schema with array of several type of objects?
我所擁有的結構:
{
"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 等等...
第二種情況是我每個人只收到一個運輸實體。 像這樣:
{"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,此功能已被刪除。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.