User Defined Functions - Scala

This notebook contains examples of a UDF and how to register them for use in Spark SQL.

Register the function as a UDF

val squared = (s: Int) => {
  s * s
}
sqlContext.udf.register("square", squared)

Call the UDF in Spark SQL

sqlContext.range(1, 20).registerTempTable("test")
%sql select id, square(id) as id_squared from test