如何将一个函数应用到数据框星火一列? [英] How to apply a function to a column of a Spark DataFrame?
本文介绍了如何将一个函数应用到数据框星火一列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我们有一个数据框星火
Let's assume that we have a Spark DataFrame
df.getClass
Class[_ <: org.apache.spark.sql.DataFrame] = class org.apache.spark.sql.DataFrame
与下面的模式
df.printSchema
root
|-- rawFV: string (nullable = true)
|-- tk: array (nullable = true)
| |-- element: string (containsNull = true)
鉴于 TK
列的每一行是一个字符串数组,如何写Scala的函数,将返回每行中的元素个数?
Given that each row of the tk
column is an array of strings, how to write a Scala function that will return the number of elements in each row?
推荐答案
您不必编写自定义函数,因为有一条是:
You don't have to write a custom function because there is one:
import org.apache.spark.sql.functions.size
df.select(size($"tk"))
如果你真的想你可以写一个 UDF
:
If you really want you can write an udf
:
import org.apache.spark.sql.functions.udf
val size_ = udf((xs: Seq[String]) => xs.size)
甚至是自定义创建前pression但真的没有一点在
or even create custom a expression but there is really no point in that.
这篇关于如何将一个函数应用到数据框星火一列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文