[英]How to build right StructType for Spark schema with array of several type of objects?
[英]How to define a spark Schema for a List of objects i.e. StructType?
我在我的項目中使用帶有 Java 8 的 spark-sql-2.3.1v。 我正在嘗試定義一個模式來從 Kafka 流中解碼我的消息消耗。
我有
class Company{
String companyName;
Integer companyId;
}
我定義為
public static final StructType companySchema = new StructType(
.add("companyName", DataTypes.StringType)
.add("companyId", DataTypes.IntegerType);
但消息定義為
class Message{
private List<Company> companyList;
private String messageId;
}
我試圖定義為
public static final StructType messageSchema = new StructType()
.add("companyList", List(companySchema)),true)
.add("messageId", DataTypes.StringType);
這是在列表中給出錯誤。 那么這個模式應該如何定義呢?
下面工作。
public static final StructType messageSchema = new StructType()
.add("companyList", DataTypes.createArrayType(companySchema, false),false)
.add("messageId", DataTypes.StringType);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.