Spark Java中无法为自定义Row生成对应Schema的问题求助
解决方案
1. 匹配数据结构的Schema定义
根据你的代码逻辑,对应的Spark Schema需要严格匹配Row的嵌套层级,以下是Java实现代码:
import org.apache.spark.sql.types.*; StructType schema = new StructType() .add("geoMap", new StructType() .add("geom", new StructType() .add("type", DataTypes.StringType) .add("coordinates", DataTypes.createArrayType(DataTypes.createArrayType(DataTypes.DoubleType))) ) ) .add("cf", DataTypes.IntegerType) .add("frc", DataTypes.IntegerType);
2. 修正代码中的类型错误
你的代码存在一处泛型声明错误:HashMap<String, String> mapObj的泛型约束不匹配实际存储的值,coordinates对应的是List<List<Double>>而非String,需修改为:
// 原错误代码 // HashMap<String, String> mapObj = new HashMap<String, String>(); // 修正后 HashMap<String, Object> mapObj = new HashMap<String, Object>(); mapObj.put("type", "LineString"); mapObj.put("coordinates", latLng);
3. 结构说明
geoMap是嵌套结构体,内部包含geom字段;geom结构体包含type(字符串类型)和coordinates(二维Double数组类型,对应代码中的List<List<Double>>);cf和frc为整数类型,与代码中提取的Integer类型完全匹配。
内容的提问来源于stack exchange,提问作者Ajit Sharma
相关产品推荐
相关产品推荐

