This file contains hidden or 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
import spray.json._ | |
val myJson: String = "{ | |
color: "red", | |
value: "#f00" | |
}" // JSON defined above | |
case class Settings(color: String, value: String) // Case class for the JSON | |
object SettingsProtocol extends DefaultJsonProtocol { |
This file contains hidden or 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
[ | |
{ | |
color: "red", | |
value: "#f00" | |
}, | |
{ | |
color: "green", | |
value: "#0f0" | |
}, | |
{ |
This file contains hidden or 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
val harmonic = new HarmonicMean | |
data.agg(harmonic(col("sales")).as("HarmonicMean")).show() | |
+-----------------+ | |
| GeometricMean| | |
+-----------------+ | |
|473.3701657458564| | |
+-----------------+ |
This file contains hidden or 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
for( var x <- Range ){ | |
statement(s); | |
} |
This file contains hidden or 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
// Taken From: https://github.com/apache/spark/blob/master/sql/core/src/main/scala/org/apache/spark/sql/expressions/udaf.scala#L128-L136 | |
@scala.annotation.varargs | |
def apply(exprs: Column*): Column = { | |
val aggregateExpression = | |
AggregateExpression( | |
ScalaUDAF(exprs.map(_.expr), this), | |
Complete, | |
isDistinct = false) | |
Column(aggregateExpression) |
This file contains hidden or 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
class ProductSum extends UserDefinedAggregateFunction { | |
override def inputSchema: org.apache.spark.sql.types.StructType = | |
StructType(Seq(StructField("value", DoubleType), StructField("value2", DoubleType))) | |
override def bufferSchema: StructType = StructType( | |
StructField("product", DoubleType) :: Nil | |
) | |
override def dataType: DataType = DoubleType |
This file contains hidden or 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
import org.apache.spark.sql.expressions.MutableAggregationBuffer | |
import org.apache.spark.sql.expressions.UserDefinedAggregateFunction | |
import org.apache.spark.sql.Row | |
import org.apache.spark.sql.types._ | |
class HarmonicMean extends UserDefinedAggregateFunction { | |
// Defind the schema of the input data | |
override def inputSchema: org.apache.spark.sql.types.StructType = | |
StructType(StructField("value", DoubleType) :: Nil) |
This file contains hidden or 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
class GeometricMean extends UserDefinedAggregateFunction ... |
This file contains hidden or 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
def harmonicMean(values: Seq[Double]): Double = { | |
values.length / values.map(v => 1.0 / v).sum | |
} | |
// Borrowed from: https://github.com/Leksyk/ml/blob/3d2c8ede6ff99d8c6ec3f830e6a4c49b85ee8e82/similar-words/src/main/scala/Utils.scala |
This file contains hidden or 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
def harmonicMean(values: Seq[Double]): Double = { | |
values.length / values.map(v => 1.0 / v).sum | |
} |