Last active
November 20, 2020 03:58
-
-
Save niroj-office/6658aabcdb51d80c602bd59c9c7cff6d to your computer and use it in GitHub Desktop.
Implicit Scala & Spark UDF to covert a string to CamelCase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implicit class CustomStringUtils(s: String) { | |
def toCamelCase = if (s == null) "" else { | |
if (s.trim.isEmpty) s else s.split("[\\s+]").map{ word => | |
val first = Character.toUpperCase(word(0)) | |
val rest = word.replaceAll("^.(.*)", "$1").toLowerCase | |
s"$first$rest" | |
}.reduce(_+" "+_) | |
} | |
} | |
import org.apache.spark.sql.functions.udf | |
val toCamelCase = udf((str: String) => str.toCamelCase) |
Author
niroj-office
commented
May 26, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment