在 spark 的子字符串中使用长度函数
use length function in substring in spark
我正在尝试在 DataFrame
中的子字符串函数中使用长度函数
但它给出了错误
val substrDF = testDF.withColumn("newcol", substring($"col", 1, length($"col")-1))
错误如下
error: type mismatch;
found : org.apache.spark.sql.Column
required: Int
我正在使用 2.1。
你得到那个错误是因为你 substring
的签名是
def substring(str: Column, pos: Int, len: Int): Column
您传递的 len
参数是 Column
,应该是 Int
。
您可能希望实现一个简单的 UDF 来解决该问题。
val strTail = udf((str: String) => str.substring(1))
testDF.withColumn("newCol", strTail($"col"))
如果您只想删除字符串的最后一个字符,您也可以在没有 UDF 的情况下执行此操作。通过使用 regexp_replace
:
testDF.show
+---+----+
| id|name|
+---+----+
| 1|abcd|
| 2|qazx|
+---+----+
testDF.withColumn("newcol", regexp_replace($"name", ".$" , "") ).show
+---+----+------+
| id|name|newcol|
+---+----+------+
| 1|abcd| abc|
| 2|qazx| qaz|
+---+----+------+
函数"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 |
+------+------+
您也可以使用 $"COLUMN".substr
val substrDF = testDF.withColumn("newcol", $"col".substr(lit(1), length($"col")-1))
输出:
val testDF = sc.parallelize(List("first", "second", "third")).toDF("col")
val result = testDF.withColumn("newcol", $"col".substr(org.apache.spark.sql.functions.lit(1), length($"col")-1))
result.show(false)
+------+------+
|col |newcol|
+------+------+
|first |firs |
|second|secon |
|third |thir |
+------+------+
你必须使用 SUBSTR 函数来实现。
val substrDF = testDF.withColumn("newcol", 'col.substr(lit(1), length('col)-1))
第一个参数是要裁剪数据的位置,第二个参数是裁剪字段的长度。
(startPos: Int,len: Int)
我正在尝试在 DataFrame
中的子字符串函数中使用长度函数
但它给出了错误
val substrDF = testDF.withColumn("newcol", substring($"col", 1, length($"col")-1))
错误如下
error: type mismatch;
found : org.apache.spark.sql.Column
required: Int
我正在使用 2.1。
你得到那个错误是因为你 substring
的签名是
def substring(str: Column, pos: Int, len: Int): Column
您传递的 len
参数是 Column
,应该是 Int
。
您可能希望实现一个简单的 UDF 来解决该问题。
val strTail = udf((str: String) => str.substring(1))
testDF.withColumn("newCol", strTail($"col"))
如果您只想删除字符串的最后一个字符,您也可以在没有 UDF 的情况下执行此操作。通过使用 regexp_replace
:
testDF.show
+---+----+
| id|name|
+---+----+
| 1|abcd|
| 2|qazx|
+---+----+
testDF.withColumn("newcol", regexp_replace($"name", ".$" , "") ).show
+---+----+------+
| id|name|newcol|
+---+----+------+
| 1|abcd| abc|
| 2|qazx| qaz|
+---+----+------+
函数"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 |
+------+------+
您也可以使用 $"COLUMN".substr
val substrDF = testDF.withColumn("newcol", $"col".substr(lit(1), length($"col")-1))
输出:
val testDF = sc.parallelize(List("first", "second", "third")).toDF("col")
val result = testDF.withColumn("newcol", $"col".substr(org.apache.spark.sql.functions.lit(1), length($"col")-1))
result.show(false)
+------+------+
|col |newcol|
+------+------+
|first |firs |
|second|secon |
|third |thir |
+------+------+
你必须使用 SUBSTR 函数来实现。
val substrDF = testDF.withColumn("newcol", 'col.substr(lit(1), length('col)-1))
第一个参数是要裁剪数据的位置,第二个参数是裁剪字段的长度。 (startPos: Int,len: Int)