scala - Spark:如何将列的 ArrayType 中的单列收集到不同的数组?
问题描述
我有一个名为requests
ArrayType 的列和其中的一些字段,code
例如value
等
StructField(requests,ArrayType(StructType(StructField(code,IntegerType,true), StructField(value,DoubleType,true) .....)
所以诸如此类的东西[[1, 5.0....], [2, 0, ....]]
。
我如何只收集code
数组中的字段以便得到公正[1,2....]
?我对 . 内的其他领域不感兴趣requests
。
我尝试使用array_zip
但没有帮助:
val result = df.withColumn("new_col", arrays_zip(col("requests.code")))
我必须使用explode
吗?或者这可能使用高阶函数吗?提前致谢!
解决方案
您可以code
通过访问数组中的字段直接获得一个值requests
数组:
val result = df.withColumn("new_col", col("requests")("code"))
或者通过使用列方法getItem
,getField
:
val result = df.withColumn("new_col", col("requests").getField("code"))
例子:
result.show(false)
//+----------------------------------------+------------+
//|requests |new_col |
//+----------------------------------------+------------+
//|[[1, 1.5], [2, 2.5], [3, 3.5], [4, 4.5]]|[1, 2, 3, 4]|
//+----------------------------------------+------------+
推荐阅读
- vue.js - vscode自动为vue html元素添加换行符
- kubernetes - Kubernetes API 服务器无法通过 IP 地址从外部访问
- webpack - 使用 webpack 包支持 html 中的 require("module-name")
- c++ - 将类方法作为 void 函数指针传递 (C++11)
- css - 如何在移动视图上调整背景颜色(具体案例)
- apache-nifi - Nifi:我可以对 json 文件值进行数学运算吗?
- python - 在 Python 中将数据透视为一行
- r - 如何用 sf 绘制 osm 文件?
- clockify - Clockify API 的问题,如何让估计再次工作
- objective-c - 钥匙串访问节目
作为对话框中的名称,而不是我创建密钥的名称