-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* use fail-fast for full loads as default read mode
* allow programmatic addition of partitions in addition to recoverPartitions from sparkSession.catalog * add custom date formatters
- Loading branch information
Bernhard Müller
committed
Feb 5, 2020
1 parent
b84bd93
commit bef0b27
Showing
43 changed files
with
896 additions
and
150 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
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
28 changes: 28 additions & 0 deletions
28
src/main/scala/com/adidas/analytics/algo/core/PartitionHelpers.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,28 @@ | ||
package com.adidas.analytics.algo.core | ||
|
||
import org.apache.spark.sql.functions.col | ||
import org.apache.spark.sql.{Column, DataFrame, Dataset, Row} | ||
|
||
/** | ||
* This is a trait with generic logic to interact with dataframes on partition level | ||
*/ | ||
trait PartitionHelpers { | ||
|
||
protected def getDistinctPartitions(outputDataFrame: DataFrame, targetPartitions: Seq[String]): Dataset[Row] = { | ||
val targetPartitionsColumns: Seq[Column] = targetPartitions.map(partitionString => col(partitionString)) | ||
|
||
outputDataFrame.select(targetPartitionsColumns: _*).distinct | ||
} | ||
|
||
protected def getParameterValue(row: Row, partitionString: String): String = | ||
createParameterValue(row.get(row.fieldIndex(partitionString))) | ||
|
||
protected def createParameterValue(partitionRawValue: Any): String = | ||
partitionRawValue match { | ||
case value: java.lang.Short => value.toString | ||
case value: java.lang.Integer => value.toString | ||
case value: scala.Predef.String => "'" + value + "'" | ||
case null => throw new Exception("Partition Value is null. No support for null partitions!") | ||
case value => throw new Exception("Unsupported partition DataType: " + value.getClass) | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/scala/com/adidas/analytics/algo/core/TableStatistics.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,48 @@ | ||
package com.adidas.analytics.algo.core | ||
|
||
import org.apache.spark.sql._ | ||
import scala.collection.JavaConversions._ | ||
|
||
/** | ||
* This is a generic trait to use in the algorithms where we want | ||
* to compute statistics on table and partition level | ||
*/ | ||
trait TableStatistics extends PartitionHelpers { | ||
|
||
protected def spark: SparkSession | ||
|
||
/** | ||
* will add statistics on partition level using HiveQL statements | ||
*/ | ||
protected def computeStatisticsForTablePartitions(df: DataFrame, | ||
targetTable: String, | ||
targetPartitions: Seq[String]): Unit = { | ||
|
||
val distinctPartitions: DataFrame = getDistinctPartitions(df, targetPartitions) | ||
|
||
generateComputePartitionStatements(distinctPartitions, targetTable, targetPartitions) | ||
.collectAsList() | ||
.foreach((statement: String) => spark.sql(statement)) | ||
|
||
} | ||
|
||
/** | ||
* will add statistics on table level using HiveQL statements | ||
*/ | ||
protected def computeStatisticsForTable(tableName: Option[String]): Unit = tableName match { | ||
case Some(table) => spark.sql(s"ANALYZE TABLE ${table} COMPUTE STATISTICS") | ||
case None => Unit | ||
} | ||
|
||
private def generateComputePartitionStatements(df: DataFrame, | ||
targetTable: String, | ||
targetPartitions: Seq[String]): Dataset[String] = { | ||
df.map(partitionValue => { | ||
val partitionStatementValues: Seq[String] = targetPartitions | ||
.map(partitionColumn => s"${partitionColumn}=${getParameterValue(partitionValue, partitionColumn)}") | ||
|
||
s"ANALYZE TABLE ${targetTable} PARTITION(${partitionStatementValues.mkString(",")}) COMPUTE STATISTICS" | ||
})(Encoders.STRING) | ||
} | ||
|
||
} |
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
Oops, something went wrong.