Created
June 6, 2017 12:00
-
-
Save bigorn0/70fe35929a8a4d9bdefdcb779d3de400 to your computer and use it in GitHub Desktop.
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
You can use select with varargs including *: | |
import spark.implicits._ | |
df.select($"*" +: Seq("A", "B", "C").map(c => | |
sum(c).over(Window.partitionBy("ID").orderBy("time")).alias(s"cum$c") | |
): _*) | |
This: | |
Maps columns names to window expressions with Seq("A", ...).map(...) | |
Prepends all pre-existing columns with $"*" +: .... | |
Unpacks combined sequence with ... : _*. | |
and can be generalize as: | |
import org.apache.spark.sql.{Column, DataFrame} | |
/** | |
* @param cols a sequence of columns to transform | |
* @param df an input DataFrame | |
* @param f a function to be applied on each col in cols | |
*/ | |
def withColumns(cols: Seq[String], df: DataFrame, f: String => Column) = | |
df.select($"*" +: cols.map(c => f(c)): _*) | |
If you find withColumn syntax more readable you can use foldLeft: | |
Seq("A", "B", "C").foldLeft(df)((df, c) => | |
df.withColumn(s"cum$c", sum(c).over(Window.partitionBy("ID").orderBy("time"))) | |
) | |
which can be generalized for example to: | |
/** | |
* @param cols a sequence of columns to transform | |
* @param df an input DataFrame | |
* @param f a function to be applied on each col in cols | |
* @param name a function mapping from input to output name. | |
*/ | |
def withColumns(cols: Seq[String], df: DataFrame, | |
f: String => Column, name: String => String = identity) = | |
cols.foldLeft(df)((df, c) => df.withColumn(name(c), f(c))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment