1 回答

TA貢獻1818條經驗 獲得超3個贊
這是因為反序列化使用由 推斷出的架構上的結構匹配Encoder,并且由于 bean 類沒有自然結構,架構的字段按名稱排序。
所以如果你定義一個像你的 bean 類Entity,從 bean 推斷的模式Encoder將是
Encoders.bean(Storage.class).schema().printTreeString();
root
|-- storage: double (nullable = true)
|-- timestamp: timestamp (nullable = true)
不是
root
|-- timestamp: timestamp (nullable = true)
|-- storage: double (nullable = true)
這是應該使用的架構Dataset。換句話說,架構定義為:
StructType schema = Encoders.bean(Entity.class).schema();
或者
StructType schema = StructType.fromDDL(
"broker_name string, order integer, server_name string, " +
"storages array<struct<storage: double, timestamp: timestamp>>"
);
將是有效的,并且可以用于testData直接加載:
Dataset<Entity> ds = spark.read()
.option("multiline", "true")
.schema(schema)
.json("testData.json")
.as(Encoders.bean(Entity.class));
而您當前的架構,相當于:
StructType valid = StructType.fromDDL(
"broker_name string, order integer, server_name string, " +
"storages array<struct<timestamp: timestamp, storage: double>>"
);
不是,盡管它可以與 JSON 閱讀器一起使用,它(與 相比Encoders)按名稱匹配數據。
可以說,這種行為應該被報告為一個錯誤——直觀地說,不應該有Encoder轉儲與其自己的加載邏輯不兼容的數據的情況。
添加回答
舉報