-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(udf): add align dataframe code step
- Loading branch information
lyogev
committed
Feb 12, 2019
1 parent
3724892
commit f80dff3
Showing
7 changed files
with
45 additions
and
7 deletions.
There are no files selected for viewing
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
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
34 changes: 34 additions & 0 deletions
34
src/main/scala/com/yotpo/metorikku/code/steps/AlignTables.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.yotpo.metorikku.code.steps | ||
|
||
import com.yotpo.metorikku.exceptions.MetorikkuException | ||
import org.apache.spark.sql.Column | ||
import org.apache.spark.sql.functions.{col, lit} | ||
|
||
object AlignTables { | ||
val message = "You need to send 2 parameters with the names of the dataframes to align: from, to" | ||
|
||
private def align(fromCols: Array[String], toCols: Array[String]): Array[Column] = { | ||
toCols.map( { | ||
case x if fromCols.contains(x) => col(x) | ||
// scalastyle:off null | ||
case y => lit(null).as(y) | ||
// scalastyle:on null | ||
}) | ||
} | ||
|
||
def run(ss: org.apache.spark.sql.SparkSession, metricName: String, dataFrameName: String, params: Option[Map[String, String]]): Unit = { | ||
params match { | ||
case Some(paramaters) => { | ||
val fromName = paramaters.get("from").get | ||
val toName = paramaters.get("to").get | ||
|
||
val from = ss.table(fromName) | ||
val to = ss.table(toName) | ||
|
||
val aligned = from.select(align(from.columns, to.columns): _*) | ||
aligned.createOrReplaceTempView(dataFrameName) | ||
} | ||
case None => throw MetorikkuException(message) | ||
} | ||
} | ||
} |
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
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
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
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