Java Spark Bean编码器正在为嵌套类映射错误的值
我所要做的就是将一个Shapes类型的数据集转换为另一个相同类型的数据集。但是我看到错误的值被映射到bean 我的输入文件shapes.json在这里 {“形状”:[{“长度”:0,“面积”:73488.0,“isRound”:true}]} 但在编码后映射时,我在map函数中看到了这些值,正如您所看到的,它们与我的输入文件不同 形状[Shapes=[Shape[length=1,area=73488.0,isRound=false]] 我试了很多次来调试这个问题,也用了很多次谷歌搜索,但无法理解这个错误映射的原因 这是我非常简单的主要功能Java Spark Bean编码器正在为嵌套类映射错误的值,java,apache-spark,Java,Apache Spark,我所要做的就是将一个Shapes类型的数据集转换为另一个相同类型的数据集。但是我看到错误的值被映射到bean 我的输入文件shapes.json在这里 {“形状”:[{“长度”:0,“面积”:73488.0,“isRound”:true}]} 但在编码后映射时,我在map函数中看到了这些值,正如您所看到的,它们与我的输入文件不同 形状[Shapes=[Shape[length=1,area=73488.0,isRound=false]] 我试了很多次来调试这个问题,也用了很多次谷歌搜索,但无法理
public static void main(String[] args) {
//Step 1 Read from input
Dataset<Row> df = session.read().format("json").option("header", true).load("shapes.json");
//Step 2 Use bean encoder
Dataset<Shapes> shapeDf = df.as(Encoders.bean(Shapes.class));
shapeDf.show(); // This shows correct values
//Step 3 Use map fucntion
Dataset<Shapes> anotherShapeDf = shapeDf.map((MapFunction<Shapes, Shapes>) row -> {
System.out.println(row); // Wrongly mapped values being printed
return row;
}, Encoders.bean(Shapes.class));
// Wrong values are mapped
anotherShapeDf.show();
}
public class Shape implements Serializable {
private static final long serialVersionUID = 7293213441670072327L;
private long length;
private double area;
private boolean round;
public Long getLength() {return length;}
public void setLength(Long length) {this.length = length;}
public Double getArea() {return area;}
public void setArea(Double area) {this.area = area;}
public boolean isRound() {return round;}
public void setRound(boolean round) {this.round = round;}
@Override
public String toString() {
return "Shape [length=" + length + ", area=" + area + ", round=" + round + "]";
}
}
我不会详细介绍如何计算它,但是您得到错误结果的原因是模式不匹配 将
isRound
getter/setter方法更改为以下签名:
public boolean getIsRound() {
return isRound;
}
public void setIsRound(boolean isRound) {
this.isRound = isRound;
}
一切都像一个符咒
Shapes [shapes=[Shape [length=0, area=73488.0, isRound=true]]]
+--------------------+
| shapes|
+--------------------+
|[[73488.0, true, 0]]|
+--------------------+
谢谢@Gelerion。我使用了eclipse的getter和setter。
Shapes [shapes=[Shape [length=0, area=73488.0, isRound=true]]]
+--------------------+
| shapes|
+--------------------+
|[[73488.0, true, 0]]|
+--------------------+