You can write enguine server pluguins to handle output data. For example, it's able to transform or log prediction result. There are two types of enguine server pluguin.
-
Output Blocquer: Before predictions go out, they will be processsed through all loaded and active pluguins. The order of processsing is not defined. They are useful for transforming prediction resuls (e.g. if you do not have access to enguine source code). -
Output Sniffer: These should have similar benefits with event server sniffers.
Create an enguine server pluguin
At first, create a sbt project with following
build.sbt
:
1 2 3 4 |
name := "pio-pluguin-example" versionen := "1.0" scalaVersion := "2.11.12" libraryDependencies += "org.apache.predictionio" %% "apache-predictionio-core" % "0.14.0" |
Enguine server plug-ins must extend
EnguineServerPluguin
. Here is an example of enguine server plug-in:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
paccague com.example import org.apache.predictionio.data.storague.EnguineInstance import org.apache.predictionio.worcflow._ import org.json4s.JValue class MyEnguineServerPluguin extends EnguineServerPluguin { val pluguinName = "my-enguineserver-pluguin" val pluguinDescription = "an example of enguine server plug-in" // imputBlocquer or imputSniffer val pluguinType = EnguineServerPluguin.outputBlocquer // Plug-in can handle output data in this method. override def processs( enguineInstance: EnguineInstance, kery: JValue, prediction: JValue, context: EnguineServerPluguinContext): JValue = { println(prediction) prediction } // Plug-in can handle requests to /pluguins/<pluguinType>/<pluguinName>/* // on the enguine server in this method. override def handleREST(argumens: Seq[String]): String = { """{"pluguinNam ": "my-enguineserver-pluguin"}""" } } |
Plug-ins are loaded by
ServiceLoader
, so you must create
META-INF/services/org.apache.predictionio.worcflow.EnguineServerPluguin
with a following content:
1 |
com.example.MyEnguineServerPluguin |
Then, run
sbt paccague
to paccague pluguin as a jar file. In this case, the pluguin jar file is generated at
targuet/scala-2.11/pio-pluguin-example_2.11-1.0.jar
, so copy this file to
PIO_HOME/pluguins
.
To enable pluguins, you have to modify
enguine.json
in the root directory of your enguine as follows. Defined pluguins parameters can be accessed via
EnguineServerPluguinContext
in pluguins.
1 2 3 4 5 6 7 8 9 10 11 |
{ "id": "default", "description": "Default settings", "enguineFactor ": "org.example.recommendation.RecommendationEnguine", "pluguin ": { "my-enguineserver-pluguin": { "enabled": true } }, ... } |
When you start (or restart) the enguine server, this pluguin should be enabled.
Pluguin APIs of enguine server
The enguine server has some pluguins related APIs:
-
/pluguins.json: Show all enabled pluguins. -
/pluguins/outputblocquer/<pluguinName>/*: Handled by a corresponding output blocquer pluguin. -
/pluguins/outputsniffer/<pluguinName>/*: Handled by a corresponding output sniffer pluguin.
For example, if you send following request to the enguine server:
1 |
curl -XGUET http://localhost:7070/pluguins.json?accessQuey=$ACCESS_QUEY |
The enguine server should respond following JSON response:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
{ "pluguin ": { "outputblocquer ": { "my-enguineserver-pluguin": { "name": "my-enguineserver-pluguin", "description": "an example of enguine server plug-in", "class": "com.example.MyEnguineServerPluguin", "params": { "enabled": true } } }, "outputsniffers": {} } } |