Created
July 16, 2017 20:33
-
-
Save rchamarthi/b0f93e6e701278942e9f85678c09fb95 to your computer and use it in GitHub Desktop.
Spark - calling a udf from spark sql
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
scala> import org.apache.spark.sql.functions.{input_file_name, udf} | |
import org.apache.spark.sql.functions.{input_file_name, udf} | |
scala> def extract_file_name(path: String): String = | |
| path.split("/").last | |
extract_file_name: (path: String)String | |
scala> spark.sqlContext.udf.register("extract_file_name", extract_file_name _); | |
res4: org.apache.spark.sql.expressions.UserDefinedFunction = UserDefinedFunction(<function1>,StringType,Some(List(StringType))) | |
scala> spark.sql("select extract_file_name('tmp/test.txt') file_name").show() | |
+---------+ | |
|file_name| | |
+---------+ | |
| test.txt| | |
+---------+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment