在电光的子串中使用LENGTH函数 [英] use length function in substring in spark
本文介绍了在电光的子串中使用LENGTH函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
DataFrame
中的子字符串函数内的LENGTH函数
但它给出错误
val substrDF = testDF.withColumn("newcol", substring($"col", 1, length($"col")-1))
下面是错误
error: type mismatch;
found : org.apache.spark.sql.Column
required: Int
我使用的是2.1。
推荐答案
可以使用函数"expr":
val data = List("first", "second", "third")
val df = sparkContext.parallelize(data).toDF("value")
val result = df.withColumn("cutted", expr("substring(value, 1, length(value)-1)"))
result.show(false)
输出:
+------+------+
|value |cutted|
+------+------+
|first |firs |
|second|secon |
|third |thir |
+------+------+
这篇关于在电光的子串中使用LENGTH函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文