Skip to content

Instantly share code, notes, and snippets.

@niroj-office
Last active November 20, 2020 03:58
Show Gist options
  • Save niroj-office/6658aabcdb51d80c602bd59c9c7cff6d to your computer and use it in GitHub Desktop.
Save niroj-office/6658aabcdb51d80c602bd59c9c7cff6d to your computer and use it in GitHub Desktop.
Implicit Scala & Spark UDF to covert a string to CamelCase
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)
@niroj-office
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment