Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将dataframe列传递给scala函数_Scala_Apache Spark_User Defined Functions - Fatal编程技术网

如何将dataframe列传递给scala函数

如何将dataframe列传递给scala函数,scala,apache-spark,user-defined-functions,Scala,Apache Spark,User Defined Functions,我编写了一个scala函数,它将时间(HH:mm:ss.SSS)转换为秒。首先,它将忽略毫秒,只需要(HH:mm:ss)并转换为秒(int)。在火花壳中测试时,它工作良好 def hoursToSeconds(a: Any): Int = { val sec = a.toString.split('.') val fields = sec(0).split(':') val creationSeconds = fields(0).toInt*3600 + fields(1).toInt*6

我编写了一个scala函数,它将时间(HH:mm:ss.SSS)转换为秒。首先,它将忽略毫秒,只需要(HH:mm:ss)并转换为秒(int)。在火花壳中测试时,它工作良好

def hoursToSeconds(a: Any): Int = {
 val sec = a.toString.split('.')
 val fields = sec(0).split(':')
 val creationSeconds = fields(0).toInt*3600 + fields(1).toInt*60 + fields(2).toInt
 return creationSeconds
}

print(hoursToSeconds("03:51:21.2550000"))
13881
我需要将此函数传递给其中一个dataframe列(正在运行),我尝试使用withColumn方法,但得到错误类型不匹配,应为:column,actual String。任何帮助都将不胜感激,是否有一种方法可以将scala函数传递给udf,然后在df.withColumn中使用udf

df.printSchema
root
 |-- vin: string (nullable = true)
 |-- BeginOfDay: string (nullable = true)
 |-- Timezone: string (nullable = true)
 |-- Version: timestamp (nullable = true)
 |-- Running: string (nullable = true)
 |-- Idling: string (nullable = true)
 |-- Stopped: string (nullable = true)
 |-- dlLoadDate: string (nullable = false)
样本正在运行列值


您可以使用以下sytax为
hoursToSeconds
函数创建自定义项:

val hoursToSecUdf = udf(hoursToSeconds _)
此外,为了在特定列上使用它,可以使用以下sytax:

df.withColumn("TimeInSeconds",hoursToSecUdf(col("running")))

你能帮助并建议如何处理这个问题吗
df.withColumn("TimeInSeconds",hoursToSecUdf(col("running")))