我想replaceFirst()在 spark scala sql 中使用該函式。replaceFirst()或者是否可以在 spark scala 資料框中使用該函式?
如果不使用 UDF,這可能嗎?
我想做的功能是:
println("abcdefgbchijkl".replaceFirst("bc","**BC**"))
// a**BC**defgbchijkl
但是,DataFrame 的 Column Type 不能與 Function 一起應用:
var test0 = Seq("abcdefgbchijkl").toDF("col0")
test0
.select(col("col0").replaceFirst("bc","**BC**"))
.show(false)
/*
<console>:230: error: value replaceFirst is not a member of org.apache.spark.sql.Column
.select(col("col0").replaceFirst("bc","**BC**"))
*/
另外,我不知道如何以 SQL 形式使用它:
%sql
-- How to use replaceFirst()
select replaceFirst()
uj5u.com熱心網友回復:
替換第一次出現并不是我可以看到 Spark 開箱即用的支持,但可以通過組合一些函式來實作:
火花 >= 3.0.0
import org.apache.spark.sql.functions.{array_join, col, split}
val test0 = Seq("abcdefgbchijkl").toDF("col0") // replaced `var` with `val`
val stringToReplace = "bc"
val replacement = "**BC**"
test0
// create a temporary column, splitting the string by the first occurrence of `bc`
.withColumn("temp", split(col("col0"), stringToReplace, 2))
// recombine the strings before and after `bc` with the desired replacement
.withColumn("col0", array_join(col("temp"), replacement))
// we no longer need this `temp` column
.drop(col("temp"))
.show(false)
給出:
------------------
|col0 |
------------------
|a**BC**defgbchijkl|
------------------
對于(火花)SQL:
-- recombine the strings before and after `bc` with the desired replacement
SELECT tempr[0] || "**BC**" || tempr[1] AS col0
FROM (
-- create a temporary column, splitting the string by the first occurrence of `bc`
SELECT split(col0, "bc", 2) AS tempr
FROM (
SELECT 'abcdefgbchijkl' AS col0
)
)
Spark < 3.0.0(2020 年之前,使用 Spark 2.4.5 測驗)
val test0 = Seq("abcdefgbchijkl").toDF("col0")
val stringToReplace = "bc"
val replacement = "**BC**"
val splitFirst = udf { (s: String) => s.split(stringToReplace, 2) }
spark.udf.register("splitFirst", splitFirst) // if you're using Spark SQL
test0
// create a temporary column, splitting the string by the first occurrence of `bc`
.withColumn("temp", splitFirst(col("col0")))
// recombine the strings before and after `bc` with the desired replacement
.withColumn("col0", array_join(col("temp"), replacement))
// we no longer need this `temp` column
.drop(col("temp"))
.show(false)
給出:
------------------
|col0 |
------------------
|a**BC**defgbchijkl|
------------------
對于(火花)SQL:
-- recombine the strings before and after `bc` with the desired replacement
SELECT tempr[0] || "**BC**" || tempr[1] AS col0
FROM (
-- create a temporary column, splitting the string by the first occurrence of `bc`
SELECT splitFirst(col0) AS tempr -- `splitFirst` was registered above
FROM (
SELECT 'abcdefgbchijkl' AS col0
)
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/455003.html
