While working with Spark RDD via sparklyr, I would like to wrap some of the common transformations to pass them more convientntly to mutate
syntax.
Example
For instance, while working with a data with the following timestamps:
2000-01-01 00:00:00.0
2000-02-02 00:00:00.0
I can convert those to a more useful YYYY-MM-dd
format using the syntax:
mutate(nice_date= from_unixtime(unix_timestamp(bad_timestamp), 'YYYY-MM-dd'))
Challenge
As I do it frequently, I would like to wrap the from_unixtime(unix_timestamp(bad_timestamp), 'YYYY-MM-dd'))
call and use syntax:
mutate(nice_date = from_unix_to_nice(bad_date))
Conventional approach would suggest writing a function:
from_unix_to_nice<- function(x) {
from_unixtime(unix_timestamp(x), 'YYYY-MM-dd')
}
Problem
When applied the function fails:
> Error: org.apache.spark.sql.AnalysisException: undefined function
> from_unix_to_nice; line 2 pos 62 at
> org.apache.spark.sql.hive.HiveFunctionRegistry$$anonfun$lookupFunction$2$$anonfun$1.apply(hiveUDFs.scala:69)
> at
> org.apache.spark.sql.hive.HiveFunctionRegistry$$anonfun$lookupFunction$2$$anonfun$1.apply(hiveUDFs.scala:69)
> at scala.Option.getOrElse(Option.scala:120)
How can I conveniently develop wrappers for common mutate operations so I can pass those to sparklyr pipeline?