-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
222 additions
and
11 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
atk-pipeline/src/main/scala/io/gearpump/examples/atk_pipeline/ATKTask.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,34 @@ | ||
package io.gearpump.examples.atk_pipeline | ||
|
||
import io.gearpump.Message | ||
import io.gearpump.cluster.UserConfig | ||
import io.gearpump.streaming.dsl.plan.OpTranslator.HandlerTask | ||
import io.gearpump.streaming.task.{StartTime, TaskContext} | ||
|
||
class ATKTask(taskContext: TaskContext, userConf: UserConfig) extends HandlerTask[Scoring](taskContext, userConf) { | ||
val TAR = "trustedanalytics.scoring-engine.archive-tar" | ||
|
||
override def onStart(startTime : StartTime) : Unit = { | ||
LOG.info("onStart") | ||
val tar = userConf.getString(TAR).get | ||
handler.load(tar) | ||
} | ||
|
||
override def onNext(msg : Message) : Unit = { | ||
LOG.info("onNext") | ||
val seq = msg.msg.asInstanceOf[Seq[Array[String]]] | ||
val score = handler.score(seq) | ||
score.foreach(result => { | ||
result.onComplete(seq => { | ||
seq.map(seq => { | ||
taskContext.output(new Message(seq)) | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
} | ||
|
||
object ATKTask { | ||
implicit val atkTask = classOf[ATKTask] | ||
} |
13 changes: 13 additions & 0 deletions
13
atk-pipeline/src/main/scala/io/gearpump/examples/atk_pipeline/KMeans.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,13 @@ | ||
package io.gearpump.examples.atk_pipeline | ||
|
||
import scala.concurrent.Future | ||
|
||
class KMeans extends Scoring { | ||
|
||
override def score(vector: Seq[Array[String]]): Option[Future[Seq[Any]]] = { | ||
model.map(model => { | ||
model.score(vector) | ||
}) | ||
} | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
atk-pipeline/src/main/scala/io/gearpump/examples/atk_pipeline/PipeLine.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,64 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.gearpump.examples.atk_pipeline | ||
|
||
import akka.actor.ActorSystem | ||
import io.gearpump.cluster.UserConfig | ||
import io.gearpump.cluster.client.ClientContext | ||
import io.gearpump.cluster.main.{ArgumentsParser, CLIOption, ParseResult} | ||
import io.gearpump.streaming.dsl.CollectionDataSource | ||
import io.gearpump.streaming.dsl.plan.OpTranslator.{HandlerTask, SourceTask} | ||
import io.gearpump.streaming.source.DataSource | ||
import io.gearpump.streaming.{Processor, StreamApplication} | ||
import io.gearpump.util.Graph._ | ||
import io.gearpump.util.{AkkaApp, Graph, LogUtil} | ||
import org.slf4j.Logger | ||
|
||
|
||
object PipeLine extends AkkaApp with ArgumentsParser { | ||
private val LOG: Logger = LogUtil.getLogger(getClass) | ||
|
||
override val options: Array[(String, CLIOption[Any])] = Array( | ||
"models"-> CLIOption[String]("<models found in hdfs>", required = false, defaultValue = Some("/user/gearpump/atk/kmeans.tar")), | ||
"randomforest"-> CLIOption[String]("<tar file location in hdfs>", required = false, defaultValue = Some("/user/gearpump/atk/randomforest.tar")) | ||
) | ||
|
||
def application(config: ParseResult, system: ActorSystem): StreamApplication = { | ||
import ATKTask._ | ||
import SourceTask._ | ||
implicit val actorSystem = system | ||
val TAR = "trustedanalytics.scoring-engine.archive-tar" | ||
val appConfig = UserConfig.empty.withString(TAR, config.getString("tar")) | ||
val sourceProcessor = Processor[HandlerTask,DataSource](new CollectionDataSource[String](Seq("one","two","three")), 1, "Source", UserConfig.empty) | ||
val kmeansProcessor = Processor[HandlerTask,Scoring](new KMeans, 1, "ATK", appConfig) | ||
val app = StreamApplication("ATKPipeline", Graph( | ||
sourceProcessor ~> kmeansProcessor | ||
), UserConfig.empty) | ||
app | ||
} | ||
|
||
override def main(akkaConf: Config, args: Array[String]): Unit = { | ||
val config = parse(args) | ||
val context = ClientContext(akkaConf) | ||
val appId = context.submit(application(config, context.system)) | ||
context.close() | ||
} | ||
|
||
} | ||
|
29 changes: 29 additions & 0 deletions
29
atk-pipeline/src/main/scala/io/gearpump/examples/atk_pipeline/Scoring.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,29 @@ | ||
package io.gearpump.examples.atk_pipeline | ||
|
||
import java.io.File | ||
import java.net.URI | ||
|
||
import org.apache.hadoop.conf.Configuration | ||
import org.apache.hadoop.fs.Path | ||
import org.trustedanalytics.atk.model.publish.format.ModelPublishFormat | ||
import org.trustedanalytics.atk.scoring.interfaces.Model | ||
|
||
import scala.concurrent.Future | ||
|
||
trait Scoring extends java.io.Serializable { | ||
|
||
var model: Option[Model] = None | ||
|
||
def load(tar: String): Unit = { | ||
val pt = new Path(tar) | ||
val uri = new URI(tar) | ||
val hdfsFileSystem: org.apache.hadoop.fs.FileSystem = org.apache.hadoop.fs.FileSystem.get(uri, new Configuration()) | ||
val tempFilePath = "/tmp/kmeans.tar" | ||
val local = new Path(tempFilePath) | ||
hdfsFileSystem.copyToLocalFile(false, pt, local) | ||
model = Option(ModelPublishFormat.read(new File(tempFilePath), Thread.currentThread().getContextClassLoader)) | ||
} | ||
|
||
def score(vector: Seq[Array[String]]): Option[Future[Seq[Any]]] | ||
|
||
} |
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