apache-spark - 从 Spark sql scala Row 中提取嵌套结构
问题描述
我将数据存储在df
具有以下火花模式的变量中。
root
|-- id: string (nullable = true)
|-- mid: integer (nullable = true)
|-- relationships: struct (nullable = true)
| |-- cmg: array (nullable = true)
| | |-- element: struct (containsNull = true)
| | | |-- cid: string (nullable = true)
| | | |-- state: struct (nullable = true)
| | | | |-- value: string (nullable = true)
我想运行一个 map 函数,在其中我在 variable 上运行一些逻辑state
,value
然后运行进一步的 reduce 操作。
我的代码如下。
df.map(mapFunc, encoder).write().format("parquet")
.option("path", "....")
.mode(SaveMode.Overwrite).save();
MapFunction<Row, String> mapFunc = (MapFunction<Row, String>) value -> {
String id = value.getAs("id").toString();
int mid = value.getAs("mid");
Relationships relationships = value.getAs("relationships");
return id + ", " + mid + ", " + relationships.getCmgList().toString();
};
public class Relationships {
@Getter
@Setter
private List<CMG> cmgList;
}
class CMG {
@Getter
private String cid;
@Getter
private State state;
}
class State {
@Getter
private String value;
}
当我运行火花作业时,作业失败说
Caused by: java.lang.ClassCastException: org.apache.spark.sql.catalyst.expressions.GenericRowWithSchema cannot be cast to com.snapshot.spark.parquet.relationship.Relationships
在线Relationships relationships = value.getAs("relationships");
如何提取存储在relationships
列中的值(最好在对象中)。
解决方案
推荐阅读
- python-3.x - InvalidArgumentError:维度 0 的切片索引 5 超出范围。[Op:StridedSlice] 名称:strided_slice/
- r - 使用 facet.by 后如何更改图形的顺序
- sql - 插入零行需要 2 分钟
- reactjs - 如何使用上下文是一个不是组件的反应类
- onclick - 在 antd modal 中的 onclick 事件上调用自定义钩子 useApi 的正确方法
- c - SERCOM (USART) 驱动程序,用于 ATSAMD21 的连续读写
- typescript - 如何在 TypeScript 中索引到对象类型?
- mqtt - 无法建立到 Mqtt 的连接
- python - 如何使用整个表来标记另一个表以准备训练/测试数据集以用于监督学习?
- javascript - 关闭 iFrame 后是否可以访问(控制台)记录的对象?