def takeArray(c: Column): Column = {
when(size(c) === lit(4), c.getItem(2)).
when(size(c) === lit(2), c.getItem(1))
when(size(c) === lit(0), lit(0)).otherwise(lit(-100))
}
df.withColumn("new_col", takeArray(col("id")))
.select( col("id"),size(col("id")), col("new_col")).show()
函数takearray-根据数组的长度,它将获取索引和返回值。
+------------+--------+-------+
| id|size(id)|new_col|
+------------+--------+-------+
|[1, 2, 3, 4]| 4| -100|
| [3, 4]| 2| -100|
| []| 0| 0|
+------------+--------+-------+
更新时间:
添加架构
root
|-- id: array (nullable = false)
| |-- element: integer (containsNull = false)
我的情绪失控了,这是错误的。第一行id列大小为4,它应该与第一个when子句匹配,并且应该返回“2”。。但它正在返回-100。你知道吗?为什么我会得到奇怪的答案?
例2:
import org.apache.spark.sql.Column
def lifeStage(col: Column): Column = {
when(col < 13, "child")
.when(col >= 13 && col <= 18, "teenager")
.when(col > 18, "adult")
}
val df = Seq(10, 15, 25).toDF("age")
df
.withColumn(
"life_stage",
lifeStage(col("age"))
)
.show()
+---+----------+
|age|life_stage|
+---+----------+
| 10| child|
| 15| teenager|
| 25| adult|
+---+----------+
解决方案:点(.)问题。缺少添加when子句
1条答案
按热度按时间kmb7vmvb1#
你在第二个条件后缺了一个点。请检查下面的代码。