Akka(一) - akka的wordcount
1. 启动类
object Application extends App{
val _system = ActorSystem("HelloAkka") //构建akka容器
val master:ActorRef = _system.actorOf(Props[MasterActor],name="master") //akka容器创建actor
println("master.path ==>\t"+master.path) //akka://HelloAkka/user/master
master ! "hi my name is spark, so happy"
master ! "hi my zsh"
master ! "xixi"
Thread.sleep(1000)
master ! new Result
Thread.sleep(500)
_system.terminate
}
2. MasterActor创建map,reduce,aggregate任务的actor
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)),name="map")
println("aggregateActor ==>\t"+aggregateActor.path) //akka://HelloAkka/user/master/aggregate (master的子actor)
println("mapActor ==>\t"+mapActor.path)
println("reduceActor ==>\t"+reduceActor.path)
override def receive: Receive = { // Receive用type重命名的PartialFunction
case msg:String => mapActor ! msg
case msg:Result => aggregateActor ! msg
case _ =>
}
}
3. map任务
class MapActor(var reduceActor: ActorRef)extends Actor{
val STOP_WORDS = List("is","a")
override def receive: Receive = {
case msg:String => reduceActor ! evlExpression(msg)
case _ =>
}
def evlExpression(line:String):MapData = {
val dataList = new ArrayBuffer[Word] // scala可变数组
val parser:StringTokenizer = new StringTokenizer(line)
while(parser.hasMoreTokens){
val str: String = parser.nextToken()
if(!STOP_WORDS.contains(str)){
dataList += (new Word(str,1))
}
}
new MapData(dataList)
}
4. reduce任务
class ReduceActor(var aggregateActor: ActorRef) extends Actor{
override def receive: Receive = {
case msg: MapData => aggregateActor ! reduce(msg.dataList)
case _ =>
}
def reduce(dataList:ArrayBuffer[Word]) : ReduceData ={
val map = new HashMap[String,Int]
for(w:Word <- dataList){
val str: String = w.word
map += (str -> map.getOrElse(str,1))
}
new ReduceData(map)
}
}
5. aggregate任务
class AggregateActor extends Actor{
var finalMap = new HashMap[String,Int]
override def receive: Receive = {
case msg:ReduceData => sum(msg.raduceMap)
case msg:Result => println(finalMap)
}
def sum(map:HashMap[String,Int]){ //多个reduceactor会向aggregateactor发送整理好的map
for(tuple <- map){
val c = finalMap.getOrElse(tuple._1,0)+tuple._2
finalMap += (tuple._1 -> c)
}
}
}
6. 用到的实体类
class Word(val word:String,val count:Int)
case class Result();
class MapData(val dataList:ArrayBuffer[Word])
class ReduceData(val raduceMap:HashMap[String,Int])
Akka(一) - akka的wordcount的更多相关文章
- Akka Essentials - 1
参考Akka Essentials 1 Introduction to Akka Actor Model Actor模式的由来 In 1973, Carl Hewitt, Peter Bishop ...
- Akka.net路径里的user
因为经常买双色球,嫌每次对彩票号麻烦,于是休息的时候做了个双色球兑奖的小程序,做完了发现业务还挺复杂的,于是改DDD重做设计,拆分服务,各种折腾...,不过这和本随笔没多大关系,等差不多了再总结一下, ...
- [翻译]AKKA笔记 - CHILD ACTORS与ACTORPATH -6
原文:http://rerun.me/2014/10/21/akka-notes-child-actors-and-path/ Actor是完全的继承结构.你创建的任何Actor肯定都是一个其他Act ...
- 使用Akka.net开发第一个分布式应用
系列主题:基于消息的软件架构模型演变 既然这个系列的主题是"基于消息的架构模型演变",少不了说说Actor模型.Akka.net是一个基于Actor模型的分布式框架.如果你对分布式 ...
- [翻译] AKKA笔记- ACTORSYSTEM (配置CONFIGURATION 与调度SCHEDULING) - 4(一)
原文在http://rerun.me/2014/10/06/akka-notes-actorsystem-in-progress/ 像我们前面看到的,我们可以用ActorSystem的actorof方 ...
- [Scala] akka actor编程(一)
Akka基础 Akka笔记之Actor简介 Akka中的Actor遵循Actor模型.你可以把Actor当作是人.这些人不会亲自去和别人交谈.他们只通过邮件来交流. 1. 消息传递 2. 并发 3 ...
- Spark如何使用Akka实现进程、节点通信的简明介绍
<深入理解Spark:核心思想与源码分析>一书前言的内容请看链接<深入理解SPARK:核心思想与源码分析>一书正式出版上市 <深入理解Spark:核心思想与源码分析> ...
- [翻译]AKKA笔记 - LOGGING与测试ACTORS -2 (一)
在前两章 ( 一 , 二 ) ,我们大致讲了Actor和message是怎么工作的,让我们看一下日志和测试我们的 TeacherActor . RECAP 这是上一节我们的Actor代码: class ...
- Spark集群 + Akka + Kafka + Scala 开发(1) : 配置开发环境
目标 配置一个spark standalone集群 + akka + kafka + scala的开发环境. 创建一个基于spark的scala工程,并在spark standalone的集群环境中运 ...
随机推荐
- makefile--目录搜索(八)
在一个较大的工程中,一般会将源代码和二进制文件(.o 文件和可执行文件)安排在不同的目录来进行区分管理.这种情况下,我们可以使用 make 提供的目录搜索依赖文件功能(在指定的若干个目录下自动搜索依赖 ...
- uboot完全手册---14
1. u-boot介绍 本次移植采用的是U-Boot-1.2.0版本. 3. U-Boot源码分析 3.1 源码入口的解释 可能大多数的同学上网查资料后都了解到,stage1阶段的启动代码,主要就在s ...
- (转)A Beginner's Guide To Understanding Convolutional Neural Networks
Adit Deshpande CS Undergrad at UCLA ('19) Blog About A Beginner's Guide To Understanding Convolution ...
- (转) Reinforcement Learning for Profit
Reinforcement Learning for Profit July 17, 2016 Is RL being used in revenue generating systems today ...
- caffe: compile error: Could not open or find file your path~~/resized_data/0 and a total of 2 images .
I0219 14:48:40.965386 31108 net.cpp:76] Memory required for data: 0I0219 14:48:40.965517 31108 layer ...
- matlab 工具之各种降维方法工具包,下载及使用教程,有PCA, LDA, 等等。。。
最近跑深度学习,提出的feature是4096维的,放到我们的程序里,跑得很慢,很慢.... 于是,一怒之下,就给他降维处理了,但是matlab 自带的什么pca( ), princomp( )函数, ...
- ogg 、 Shareplex和DSG RealSync 对比
主流数据库容灾(复制)工具对比 Oracle Golden Gate Quest Shareplex DSG RealSync 公司概要 公司介绍 GoldenGate公司成立于200 ...
- HTML兼容性设置
今天碰到了兼容性问题,页面显示空白,打开调试界面,显示信息 “Compatibility View because 'Display intranet sites in Compatibility V ...
- SQL Server 系统表简介
SQL Server 系统表简介 系统目录是由描述SQL Server 系统的数据库.基表.视图和索引等对象的结构的系统表组成.SQL Server 经常访问系统目录,检索系统正常运行所需的必要信息. ...
- Java-JDBC连接Oracle 数据库
package com.zse.oracle; import oracle.jdbc.*; import java.sql.*; import javax.swing.text.html.HTMLDo ...