if else 的 Spark scala udf 错误
Spark scala udf error for if else
我正在尝试使用函数 getTIme 为 spark scala udf 定义 udf,但我收到错误 error: illegal start of declaration
。语法中可能有什么错误并返回日期,如果存在解析异常而不是返回空值,则将某些字符串作为 error
发送
def getTime=udf((x:String) : java.sql.Timestamp => {
if (x.toString() == "") return null
else { val format = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss");
val d = format.parse(x.toString());
val t = new Timestamp(d.getTime()); return t
}})
谢谢!
您应该使用 Scala 数据类型,而不是 Java 数据类型。它会是这样的:
def getTime(x: String): Timestamp = {
//your code here
}
您可以通过这种方式轻松完成:
def getTimeFunction(timeAsString: String): java.sql.Timestamp = {
if (timeAsString.isEmpty)
null
else {
val format = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss")
val date = format.parse(timeAsString.toString())
val time = new Timestamp(date.getTime())
time
}
}
val getTimeUdf = udf(getTimeFunction _)
然后相应地使用此 getTimeUdf。 !
udf 的 return 类型是派生的,不应指定。将第一行代码改为:
def getTime=udf((x:String) => {
// your code
}
这应该可以消除错误。
以下是以函数式风格编写并使用 Scala 结构的完整工作代码:
val data: Seq[String] = Seq("", null, "2017-01-15 10:18:30")
val ds = spark.createDataset(data).as[String]
import java.text.SimpleDateFormat
import java.sql.Timestamp
val fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
// ********HERE is the udf completely re-written: **********
val f = udf((input: String) => {
Option(input).filter(_.nonEmpty).map(str => new Timestamp(fmt.parse(str).getTime)).orNull
})
val ds2 = ds.withColumn("parsedTimestamp", f($"value"))
输出如下:
+-------------------+--------------------+
| value| parsedTimestamp|
+-------------------+--------------------+
| | null|
| null| null|
|2017-01-15 10:18:30|2017-01-15 10:18:...|
+-------------------+--------------------+
我正在尝试使用函数 getTIme 为 spark scala udf 定义 udf,但我收到错误 error: illegal start of declaration
。语法中可能有什么错误并返回日期,如果存在解析异常而不是返回空值,则将某些字符串作为 error
def getTime=udf((x:String) : java.sql.Timestamp => {
if (x.toString() == "") return null
else { val format = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss");
val d = format.parse(x.toString());
val t = new Timestamp(d.getTime()); return t
}})
谢谢!
您应该使用 Scala 数据类型,而不是 Java 数据类型。它会是这样的:
def getTime(x: String): Timestamp = {
//your code here
}
您可以通过这种方式轻松完成:
def getTimeFunction(timeAsString: String): java.sql.Timestamp = {
if (timeAsString.isEmpty)
null
else {
val format = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss")
val date = format.parse(timeAsString.toString())
val time = new Timestamp(date.getTime())
time
}
}
val getTimeUdf = udf(getTimeFunction _)
然后相应地使用此 getTimeUdf。 !
udf 的 return 类型是派生的,不应指定。将第一行代码改为:
def getTime=udf((x:String) => {
// your code
}
这应该可以消除错误。
以下是以函数式风格编写并使用 Scala 结构的完整工作代码:
val data: Seq[String] = Seq("", null, "2017-01-15 10:18:30")
val ds = spark.createDataset(data).as[String]
import java.text.SimpleDateFormat
import java.sql.Timestamp
val fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
// ********HERE is the udf completely re-written: **********
val f = udf((input: String) => {
Option(input).filter(_.nonEmpty).map(str => new Timestamp(fmt.parse(str).getTime)).orNull
})
val ds2 = ds.withColumn("parsedTimestamp", f($"value"))
输出如下:
+-------------------+--------------------+
| value| parsedTimestamp|
+-------------------+--------------------+
| | null|
| null| null|
|2017-01-15 10:18:30|2017-01-15 10:18:...|
+-------------------+--------------------+