Scala API中的
在Spark 3.0.1、Scala 2.12中,我得到了以下内容:
import spark.implicits._
import org.apache.spark.sql.functions._
Seq(("1987.01.01"))
.toDF("Input")
.select(
col("Input"),
to_date(col("Input"), "yyyy.M.d").as("format"),
length(col("Input")),
substring(col("Input"),1, length(col("Input")).cast("int")+0 )
).show()
command-1067744798817014:7: error: type mismatch;
found : org.apache.spark.sql.Column
required: Int
substring(col("Input"),1, length(col("Input")).cast("int")+0 )
^
所以我想我弄错了";长度";函数,通过隐式导入还是其他什么?
这适用于
Seq(("1987.01.01"))
.toDF("Input")
.select(
col("Input"),
to_date(col("Input"), "yyyy.M.d").as("format"),
length(col("Input"))
).show()
+----------+----------+-------------+
| Input| format|length(Input)|
+----------+----------+-------------+
|1987.01.01|1987-01-01| 10|
+----------+----------+-------------+
substring
方法只接受第二个和第三个参数的整数。如果要传递列,则需要使用expr
来使用Spark SQL APIsubstring
方法:
Seq(("1987.01.01"))
.toDF("Input")
.select(
col("Input"),
to_date(col("Input"), "yyyy.M.d").as("format"),
length(col("Input")),
expr("substring(Input, 1, length(Input) + 0)")
).show()
+----------+----------+-------------+----------------------------------------+
| Input| format|length(Input)|substring(Input, 1, (length(Input) + 0))|
+----------+----------+-------------+----------------------------------------+
|1987.01.01|1987-01-01| 10| 1987.01.01|
+----------+----------+-------------+----------------------------------------+