Akka學(xué)習(xí)實(shí)現(xiàn)workcount

package com.dcx.scala.actor

import akka.actor.{Actor, ActorRef, ActorSystem, Props}

import scala.collection.mutable.HashMap
import scala.collection.mutable.ListBuffer
import scala.io.Source

/**
  * 思路:
  * 要有個(gè)Server
  * 要有個(gè)Client去通信,client統(tǒng)計(jì)文本后把(qy,3)輸出給Server;Server再把所有的qy聚合,放到ListBuffer中
  */

object AkkaWordCount {

//  可變長(zhǎng)List
  val list = new ListBuffer[HashMap[String,Int]]

  def main(args: Array[String]): Unit = {
//   輸入數(shù)據(jù)文本
    val files: Array[String] = Array("D:\\tmp\\input\\word3.txt", "D:\\tmp\\input\\word3.txt", "D:\\tmp\\input\\word3.txt")
    //存放接收到的每個(gè)actor處理的結(jié)果數(shù)據(jù)
    //存放有actor返回結(jié)果的Future數(shù)據(jù)

    //拿ActorSystem是一個(gè)靜態(tài)工廠
    val weChatApp = ActorSystem("WeChatApp")
    //拿到兩個(gè)Actor的通信地址
    val akkaServerRef: ActorRef = weChatApp.actorOf(Props[AkkaServer],"jianjian1")

    val clientRef: ActorRef = weChatApp.actorOf(Props(new Client(akkaServerRef)),"jianjian")

    for (file <- files) {
      clientRef ! file
    }

//  讓該線(xiàn)程先睡一下,過(guò)早進(jìn)入死循環(huán)會(huì)導(dǎo)致list沒(méi)有3個(gè),一直循環(huán)不出來(lái)
    Thread.sleep(1000)
//  如果list把三個(gè)文件都放滿(mǎn)了,就退出循環(huán)
    while(true){
      if(list.size == 3){
//       輸出list
        println(list(list.size -1))
        return
      }
    }
  }
}

//把每次聚合后的值都發(fā)送給AkkaServer

class Client(val serverRef:ActorRef) extends Actor {
  override def receive: Receive = {
    {
//     偏函數(shù) 常用作模式匹配
//      case filePath: String => {
////      map階段
//        val list: List[String] = Source.fromFile(filePath).getLines().toList
//        val words: List[String] = list.flatMap(_.split(" "))
//        val res: Map[String, Int] = words.map((_, 1)).groupBy(_._1).mapValues(_.size)
//        //異步發(fā)送結(jié)果數(shù)據(jù)    res發(fā)送到Server,去模式匹配
//        serverRef ! res
//      }

      case filePath:String => {
        val list: List[String] = Source.fromFile(filePath).getLines().toList
        val words: List[String] = list.flatMap(_.split(" "))
//      得出: (qy,3) 格式
        val res: Map[String, Int] = words.map((_, 1)).groupBy(_._1).mapValues(_.size)
        serverRef ! res
      }
    }
  }
}

import scala.collection.mutable.HashMap
class AkkaServer extends Actor {
  private var hashMap: HashMap[String, Int] = new HashMap[String, Int]

  override def receive: Receive = {
    case context: Map[String, Int] =>{
//      (qy,3) 
      context.map( (map:(String,Int)) => {
//      聚合
        val value: Any = hashMap.getOrElse(map._1,None)
        if(value != None){
          hashMap(map._1) = value.asInstanceOf[Int] + map._2
        }else{
          hashMap(map._1) = map._2
        }
      }
      )

      AkkaWordCount.list += hashMap
    }
  }
}

新聞名稱(chēng):Akka學(xué)習(xí)實(shí)現(xiàn)workcount
文章地址:http://muchs.cn/article18/gppedp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站云服務(wù)器、域名注冊(cè)虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司