Akka实现WordCount(Scala)
Akka实现WordCount(Scala):
架构图:

项目结构:

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.citi.sky</groupId>
<artifactId>AkkaPJ</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>AkkaPJ</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.11.6</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-compiler</artifactId>
<version>2.11.6</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-reflect</artifactId>
<version>2.11.6</version>
</dependency> <dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_2.11</artifactId>
<version>2.3.3</version> </dependency> <dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-testkit_2.11</artifactId>
<version>2.3.6</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_2.11</artifactId>
<version>3.0.4</version>
<scope>test</scope>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.2</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build> </project>
消息:
case class MapData (dataList: List[WordCount]) case class ReduceData (reduceDataList: Map[String, Int]) case class Result() case class WordCount (key: String, count: Int)
Actors:
MasterActor
import akka.actor.Actor
import akka.actor.Props
import com.citi.dw.messages.Result class MasterActor extends Actor { private val aggregateActor = context.actorOf(Props(classOf[AggregateActor]), "aggregateActor")
private val reduceActor = context.actorOf(Props(classOf[ReduceActor], aggregateActor), "reduceActor")
private val mapActor = context.actorOf(Props(classOf[MapActor], reduceActor), "mapActor") def receive: Actor.Receive = {
case msg: String => {
mapActor ! msg
}
case msg: Result => {
aggregateActor.forward(msg)
}
// case msg: Map[String, Int] =>
case _ => println("MasterActor receive wrong message.")
}
}
MapActor:
import akka.actor.Actor
import com.citi.dw.messages.MapData
import com.citi.dw.messages.WordCount
import scala.collection.mutable.ListBuffer
import akka.actor.ActorRef class MapActor(val reduceActor: ActorRef) extends Actor {
def receive: Actor.Receive = {
case msg: String => {
val mapData = evaluateExpression(msg)
reduceActor ! mapData
}
case _ => println("MapActor receive wrong message.")
} private[this] def evaluateExpression(line: String): MapData = {
val dataList = ListBuffer[WordCount]()
line.split(" ").map(word => dataList += WordCount(word, 1)) // val wordArr = line.split(" ")
// for(word <- wordArr) {
// dataList += WordCount(word, 1)
// }
// println(dataList)
MapData(dataList.toList)
} }
ReduceActor:
import akka.actor.Actor
import com.citi.dw.messages.MapData
import com.citi.dw.messages.ReduceData
import com.citi.dw.messages.WordCount
import scala.collection.mutable.HashMap
import akka.actor.ActorRef class ReduceActor(val aggregateActor: ActorRef) extends Actor { def receive: Actor.Receive = {
case msg: MapData => {
val reduceData = reduce(msg.dataList)
aggregateActor ! reduceData
}
case _ => println("ReduceActor receive wrong message.")
} private[this] def reduce(dataList: List[WordCount]): ReduceData = {
val reduceMap = HashMap[String, Int]() for (wc <- dataList) {
wc match {
case WordCount(key, count) if reduceMap.contains(key) => {
val localSumCount = reduceMap.get(key).get + count
reduceMap += ((key, localSumCount))
// println(reduceMap)
}
case WordCount(key, count) => {
reduceMap += ((key, 1))
// println(reduceMap)
}
} } ReduceData(reduceMap.toMap)
} }
AggregateActor:
import akka.actor.Actor
import com.citi.dw.messages.ReduceData
import scala.collection.mutable.HashMap
import com.citi.dw.messages.Result
import akka.actor.ActorRef class AggregateActor extends Actor { private[this] var finalReduceMap = HashMap[String, Int]() def receive: Actor.Receive = {
case msg: ReduceData => {
aggregateAndReduce(msg.reduceDataList)
}
case msg: Result => {
// println(f"Result: ${finalReduceMap}")
// sender().tell(finalReduceMap.toMap, ActorRef.noSender)
sender ! finalReduceMap.toMap
}
case _ => println("AggregateActor receive wrong message.")
} private[this] def aggregateAndReduce(reduceList: Map[String, Int]) = {
// println(s"final: ${finalReduceMap}")
for (key <- reduceList.keys) {
if (finalReduceMap.contains(key)) { val count = finalReduceMap.get(key).get + reduceList.get(key).get
finalReduceMap += ((key, count))
} else {
finalReduceMap += ((key, reduceList.get(key).get))
}
} } }
主程序:
import akka.actor.ActorSystem
import akka.actor.Props
import com.citi.dw.actors.MasterActor
import com.citi.dw.messages.Result
import akka.pattern.ask
import scala.concurrent.duration._
import akka.util.Timeout
import scala.util._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Await object AkkaWordCount extends App { implicit val timeout = Timeout(5 seconds)
val system = ActorSystem("WordCountAkka")
val master = system.actorOf(Props(classOf[MasterActor]), "master") master ! "Hi! Hi!"
master ! ("My name is Sky. I am so so so happy to be here ")
master ! ("Today, I am going to introduce word count for Akka ")
master ! ("I hope hope It is helpful to you ")
master ! ("Thank you ") Thread.sleep(1000) val future = master ? Result()
// future.onComplete({
// case Success(x: String) => println(x)
// case Failure(t) => println(t)
// case msg => println("unknown message! " + msg)
// }) val result = Await.result(future, timeout.duration).asInstanceOf[Map[String, Int]]
result.map(m => println(m._1, m._2)) system.shutdown() }
运行结果:
(for,1)
(name,1)
(count,1)
(is,2)
(am,2)
(My,1)
(going,1)
(so,3)
(introduce,1)
(Sky.,1)
(I,3)
(to,3)
(Hi!,2)
(you,2)
(here,1)
(happy,1)
(Thank,1)
(hope,2)
(Today,,1)
(helpful,1)
(Akka,1)
(It,1)
(be,1)
(word,1)
Akka实现WordCount(Scala)的更多相关文章
- Akka(一) - akka的wordcount
1. 启动类 object Application extends App{ val _system = ActorSystem("HelloAkka") //构建akka容器 v ...
- Spark:用Scala和Java实现WordCount
http://www.cnblogs.com/byrhuangqiang/p/4017725.html 为了在IDEA中编写scala,今天安装配置学习了IDEA集成开发环境.IDEA确实很优秀,学会 ...
- 编写Spark的WordCount程序并提交到集群运行[含scala和java两个版本]
编写Spark的WordCount程序并提交到集群运行[含scala和java两个版本] 1. 开发环境 Jdk 1.7.0_72 Maven 3.2.1 Scala 2.10.6 Spark 1.6 ...
- Scala IDE for Eclipse的下载、安装和WordCount的初步使用(本地模式和集群模式)
包括: Scala IDE for Eclipse的下载 Scala IDE for Eclipse的安装 本地模式或集群模式 我们知道,对于开发而言,IDE是有很多个选择的版本.如我们大部分人经常 ...
- IDEA15 下运行Scala遇到问题以及解决办法
为了让Scala运行起来还是很麻烦,为了大家方便,还是记录下来: 1.首先我下载的是IDEA的社区版本,版本号为15. 2.下载安装scala插件: 2.1 进入设置菜单. 2.2 点击安装JetBr ...
- 在IDEA中编写Spark的WordCount程序
1:spark shell仅在测试和验证我们的程序时使用的较多,在生产环境中,通常会在IDE中编制程序,然后打成jar包,然后提交到集群,最常用的是创建一个Maven项目,利用Maven来管理jar包 ...
- Win7上Spark WordCount运行过程及异常
WordCount.Scala代码如下: package com.husor.Spark /** * Created by huxiu on 2014/11/26. */ import org.apa ...
- Akka初步介绍
Akka可能很多人都没有用过,也不知道是什么,但如果说起Scala或Spark就有很多人都听说过或使用过 ,这里简单说下三者的关系Akka是使用Scala开发的,Spark中使用了Akka作为其消息的 ...
- IntelliJ IDEA的下载、安装和WordCount的初步使用(本地模式和集群模式)
包括: IntelliJ IDEA的下载 IntelliJ IDEA的安装 IntelliJ IDEA中的scala插件安装 用SBT方式来创建工程 或 选择Scala方式来创建工程 本地模式或集群 ...
随机推荐
- codeforces 496 E. Distributing Parts(贪心+set二分)
题目链接:http://codeforces.com/contest/496/problem/E 题意:有n场演出,每场演出都有限制的高音和低音.然后m个人给出每个人的极限高音和低音还有出场次数. 最 ...
- C、C++格式化字符串
引言 在C和C++开发中,我们经常会用到printf来进行字符串的格式化,例如printf("format string %d, %d", 1, 2);,这样的格式化只是用于打印调 ...
- lambda表达式与匿名内部类与双冒号(::)
lambda表达式在只有一条代码时还可以引用其他方法或构造器并自动调用,可以省略参数传递,代码更加简洁,引用方法的语法需要使用::符号.lambda表达式提供了四种引用方法和构造器的方式: 引用对象的 ...
- 分库分表之后,id 主键如何处理?
其实这是分库分表之后你必然要面对的一个问题,就是 id 咋生成?因为要是分成多个表之后,每个表都是从 1 开始累加,那肯定不对啊,需要一个全局唯一的 id 来支持.所以这都是你实际生产环境中必须考虑的 ...
- UGUI_创建旋转物体,使用Slider控制小球旋转速度
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : ...
- Eclipse中maven项目报错:org.springframework.web.filter.CharacterEncodingFilter
写了一个demo,发现在tomcat中部署完项目,启动时报错. 1,问题描述 2,解决办法 1)程序在部署完成后报错,说明是程序是编译通过的,即编译编译路径Java Build Path没问题.2)此 ...
- 重学js之JavaScript 面向对象的程序设计(创建对象)
注意: 本文章为 <重学js之JavaScript高级程序设计>系列第五章[JavaScript引用类型]. 关于<重学js之JavaScript高级程序设计>是重新回顾js基 ...
- C++类拷贝控制 深拷贝 浅拷贝
普通类型对象之间的复制很简单,而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员变量,这篇文章将帮你理清C++类对象的拷贝方式 拷贝构造函数,拷贝赋值运算符 首先我们简单了解下默认的拷贝 ...
- Java优化策略小积累
1.尽量避免大量使用静态变量 package com.cfang.jvm; public class Test2 { private static Test1 test1 = new Test1(); ...
- 自定义 Alamofire 的 response serializer
Alamofire 的 DataRequest 类针对二进制数据.字符串.json.属性列表提供了一系列方便解析的方法(内部实际上使用的是 Response Serializer),现在我们要针对服务 ...