Akka之WordCount with SBT

build.sbt:

name := "HelloAkkaWithSBT"

version := "1.0"

scalaVersion := "2.9.2"

resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases"

libraryDependencies += "com.typesafe.akka" % "akka-actor" % "2.0.3"
MasterActor:
class MasterActor extends Actor{

  val aggregateActor : ActorRef = context.actorOf(Props[AggregateActor], name = "aggregate")

  val reduceActor : ActorRef = context.actorOf(Props(new ReduceActor(aggregateActor)), name = "reduce")

  val mapActor : ActorRef = context.actorOf(Props(new MapActor(reduceActor)), "map")

  override protected def receive: Receive = {
    case message : String =>
      mapActor ! message

    case message : Result =>
      aggregateActor ! message

    case _ =>
  }
}
MapActor:
class MapActor(reduceActor: ActorRef) extends Actor{

  val STOP_WORDS_LIST = List("a", "is")

  override protected def receive: Receive = {

    case message : String =>

      reduceActor ! map(message)

    case _ =>
  }

  def map(line : String): MapData = {

    val dataList = new util.ArrayList[Word]

    val parser : StringTokenizer = new StringTokenizer(line)

    val defaultCount : Integer = 1;

    while(parser.hasMoreTokens){

      val word : String = parser.nextToken().toLowerCase()

      if(!STOP_WORDS_LIST.contains(word)){

        dataList.add(new Word(word, defaultCount))

      }
    }

    return new MapData(dataList)
  }
}
ReduceActor:
class ReduceActor(aggregateActor : ActorRef) extends Actor{

  override protected def receive: Receive = {

    case message : MapData =>

      aggregateActor ! reduce(message.dataList)

    case _ =>
  }

  def reduce(dataList: util.ArrayList[Word]): ReduceData = {

    val reduceMap = new util.HashMap[String, Integer]

    if(!dataList.isEmpty()){

      for(wc : Word <- dataList){

        val word = wc.word

        if(reduceMap.containsKey(word)){

          reduceMap.put(word, reduceMap.get(word) + 1)

        }else{

          reduceMap.put(word, 1)
        }
      }
    }

    return new ReduceData(reduceMap)
  }
}
AggregateActor:
class AggregateActor extends Actor{

  val finalReduceMap = new util.HashMap[String, Integer]

  override protected def receive: Receive = {

    case message : ReduceData =>

      aggregate(message.reduceMap)

    case message : Result =>

      println(finalReduceMap.toString)

  }

  def aggregate(reduceMap: util.HashMap[String, Integer]): Unit = {

    var count : Integer = 0

    for(key : String <- reduceMap.keySet()){

      if(finalReduceMap.containsKey(key)){

        count = reduceMap.get(key)

        count += finalReduceMap.get(key)

        finalReduceMap.put(key, count)

      }else{

        finalReduceMap.put(key, reduceMap.get(key))
      }
    }
  }
}

入口:

object MapReduceApplication {

   def main (args: Array[String]){

    val _system = ActorSystem("HelloAkka")

     val master = _system.actorOf(Props[MasterActor], name = "master")

     master ! "Hi! My name is Rocky. I'm so so so happy to be here."

     master ! "Today, I'm going to read a news article for you."

     master ! "I hope I hope you'll like it."

     Thread.sleep(500)

     master ! new Result

     Thread.sleep(500);

     _system.shutdown()
  }
}

SBT运行结果:


Akka之WordCount with SBT_第1张图片
图片发自App

你可能感兴趣的:(Akka之WordCount with SBT)