Skip to content

Instantly share code, notes, and snippets.

@chobeat
Created September 5, 2016 13:38
Show Gist options
  • Save chobeat/f07221357a2e3f9efa377e4cb0479f92 to your computer and use it in GitHub Desktop.
Save chobeat/f07221357a2e3f9efa377e4cb0479f92 to your computer and use it in GitHub Desktop.
class JPMMLEvaluationMapOperator(pmmlSource: String,
inputPreparationErrorHandlingStrategy: InputPreparationErrorHandlingStrategy,
missingValueStrategy: MissingValueStrategy,
resultExtractionStrategy: ResultExtractionStrategy)
extends RichMapFunction[Map[String, Any], Map[String, Serializable]] {
//The evaluator is transient and built in the open() function to allow proper serializability of the operator.
@transient
private var evaluator: Evaluator = null
override def open(params: Configuration) = {
super.open(params)
evaluator = ModelEvaluatorFactory.newInstance()
.newModelManager(
JAXBUtil.unmarshalPMML(
new StreamSource(
new StringReader(pmmlSource))))
}
override def map(input: Map[String, Any]) = {
val preparedInput = prepareInput(input, evaluator, missingValueStrategy, inputPreparationErrorHandlingStrategy)
val evaluationResult = evaluate(preparedInput, evaluator)
val result = resultExtractionStrategy(evaluationResult, evaluator)
result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment