-
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(obfuscate columns udf) :Obfuscate columns udf (#447)
- Loading branch information
1 parent
7fdd083
commit bc1e587
Showing
9 changed files
with
340 additions
and
16 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
5 changes: 5 additions & 0 deletions
5
src/main/scala/com/yotpo/metorikku/code/steps/InputMatcher.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,5 @@ | ||
package com.yotpo.metorikku.code.steps | ||
|
||
case class InputMatcher[K](ks: K*) { | ||
def unapplySeq[V](m: Map[K, V]): Option[Seq[V]] = if (ks.forall(m.contains)) Some(ks.map(m)) else None | ||
} |
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
5 changes: 5 additions & 0 deletions
5
...la/com/yotpo/metorikku/code/steps/obfuscate/ColumnsNotPartOfOriginalSchemaException.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,5 @@ | ||
package com.yotpo.metorikku.code.steps.obfuscate | ||
|
||
case class ColumnsNotPartOfOriginalSchemaException(columns: Array[String]) | ||
extends Exception(s"The following columns are not a part of the original schema and therefore cannot be obfuscated: " + | ||
s"${columns.mkString(", ")}") |
51 changes: 51 additions & 0 deletions
51
src/main/scala/com/yotpo/metorikku/code/steps/obfuscate/ObfuscateColumns.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,51 @@ | ||
package com.yotpo.metorikku.code.steps.obfuscate | ||
|
||
import org.apache.spark.sql.{Column, DataFrame} | ||
import org.apache.spark.sql.functions.{col, lit, udf, when} | ||
import com.roundeights.hasher.Implicits._ | ||
import com.yotpo.metorikku.code.steps.InputMatcher | ||
import com.yotpo.metorikku.exceptions.MetorikkuException | ||
|
||
object ObfuscateColumns { | ||
private val ObfuscateColumnsInputMatcher = InputMatcher("columns", "delimiter", "value", "table") | ||
|
||
def run(ss: org.apache.spark.sql.SparkSession, metricName: String, dataFrameName: String, params: Option[Map[String, String]]): Unit = { | ||
val (columns, value, table) = parse(params) | ||
val df = ss.table(table) | ||
val obfuscatedDf = obfuscateColumns(df, columns, value) | ||
|
||
obfuscatedDf.createOrReplaceTempView(dataFrameName) | ||
} | ||
|
||
private def parse(params: Option[Map[String, String]]): (Array[String], String, String) = { | ||
params.get match { | ||
case ObfuscateColumnsInputMatcher(columns, delimiter, value, table) => (columns.split(delimiter), value, table) | ||
case _ => throw MetorikkuException( | ||
"""Obfuscate Columns expects the following parameters: columns, | ||
delimiter, value, and table. one or more of those wasn't supplied""".stripMargin | ||
) | ||
} | ||
} | ||
|
||
def obfuscateColumns(df: DataFrame, columns: Array[String], value: String): DataFrame = { | ||
val columnsInOriginalSchema = df.schema.map(_.name) | ||
val columnsNotInOriginalSchema = columns.filter(col => !columnsInOriginalSchema.contains(col)) | ||
|
||
if (columnsNotInOriginalSchema.nonEmpty) throw ColumnsNotPartOfOriginalSchemaException(columnsNotInOriginalSchema) | ||
|
||
val transform: String => Column = value match { | ||
case "md5" => colName: String => md5Udf(col(colName)) | ||
case "sha256" => colName: String => sha256Udf(col(colName)) | ||
case _ => colName: String => lit(value) | ||
} | ||
// scalastyle:off null | ||
val transformPreservingNull = (colName: String) => when(col(colName).isNotNull, transform(colName)).otherwise(lit(null)) | ||
// scalastyle:on null | ||
|
||
columns.foldLeft(df)((dfAcc, column) => dfAcc.withColumn(column, transformPreservingNull(column))) | ||
} | ||
|
||
private def md5Udf = udf { s: String => s.md5.hex } | ||
|
||
private def sha256Udf = udf { s: String => s.sha256.hex } | ||
} |
Oops, something went wrong.