无法将 spark 数据集收集为列表或地图,没有适用的构造函数错误

Unable to collect spark dataset as list or map with No applicable constructor error

我有一个class

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Store {

    private Double probability;

    private String store;

}

和json文件

{"probability":"0.26","store":"abc/s3"}
{"probability":"0.57","store":"abc/s1"}

我尝试将其作为数据集读取并将其转换为地图。读取数据集是成功的,并且能够使用 spark sql 命令对其进行操作,还能够使用 show() 等

查看数据集
Dataset<Store> ds = ss.read().json(path).as(Encoders.bean(Store.class));
Map<String, Double> storeMap = ds.collectAsList().stream()
                .collect(Collectors.toMap(Store::getStore, Store::getProbability));

但是转换为地图失败并出现错误。此错误在命令 ds.collectAsList() 本身

No applicable constructor/method found for actual parameters 

"org.apache.spark.unsafe.types.UTF8String"; 
candidates are: 
"public static java.lang.Double java.lang.Double.valueOf(java.lang.String) throws java.lang.NumberFormatException",
"public static java.lang.Double java.lang.Double.valueOf(double)"

我做错了什么?

问题不在您的代码中,而在您的 JSON 数据中。应该是

{"probability":0.26,"store":"abc/s3"}
{"probability":0.57,"store":"abc/s1"}

或将private Double probability;改为private String probability;