大数据系列修炼-Scala课程96(2)

大数据系列修炼-Scala课程96(2)

核心内容:
1、Akka第一个案例动手实战代码总结

1、Akka第一个案例动手实战代码总结

Akka案例之WordCount完整代码:


import akka.actor.*;

import java.util.*;


/** * Created by hp on 2016/9/29. * 基于Akka框架实现WordCount案例:ActorSystem、MasterActor、MapActor、ReduceActor、AggregateActor */
public class HelloAkka //本类实际上是启动类 {
    public static void main(String[] args) throws Exception
    {
            ActorSystem _system = ActorSystem.create("HelloAkka");
            ActorRef master = _system.actorOf(new Props(MasterActor.class), "master");

            //master向MapActor发送消息
            master.tell("I am Hadoop Spark");
            master.tell("this is java Hadoop java");
            master.tell("scala and this java");

            Thread.sleep(500);

            master.tell(new Result());  //masterActor获取最终的结果

            Thread.sleep(500);

            _system.shutdown();
    }
}
class MasterActor extends UntypedActor  //继承了一个抽象类,并实现其抽象方法
{
    private ActorRef aggregateActor = getContext().actorOf(
            new Props(AggregateActor.class),"aggregate");
    private ActorRef reduceActor = getContext().actorOf(
            new Props(new UntypedActorFactory() {
                public UntypedActor create(){
                    return new ReduceActor(aggregateActor);
                }
            }),"reduce");
    private ActorRef mapActor    = getContext().actorOf(
            new Props(new UntypedActorFactory() {
                public UntypedActor create(){
                    return new MapActor(reduceActor);
                }
            }),"map");
    public void onReceive(Object message) throws Exception
    {
        //判断消息的类型并进行模式匹配
        if (message instanceof String)
        {
            mapActor.tell(message);
        }else if(message instanceof Result)
        {
            aggregateActor.tell(message);
        }else
            unhandled(message);  //否则的话消息就不进行任何处理
    }
}
class MapActor extends UntypedActor  //MapActor对接收到的字符串信息进行处理
{
    public ActorRef reduceActor = null;
    public MapActor(ActorRef reduceActor)   //构造相应的构造函数
    {
        this.reduceActor = reduceActor;
    }
    @Override
    public void onReceive(Object message) throws Exception
    {
        if (message instanceof String)
        {
            String line = (String)message;
            MapData mapData = evaluateExpression(line);
            reduceActor.tell(mapData);
        }else
            unhandled(message);
    }
    //MapData中将单词进行具体的切分,并且每个单词计数为1
    public MapData evaluateExpression(String line)
    {
        List<WordCount> dataList = new ArrayList<WordCount>();  //通过泛型可以类型中嵌套着类型
        //String 拆分器对字符串进行处理
        StringTokenizer parser = new StringTokenizer(line);
        while(parser.hasMoreTokens())
        {
            String word = parser.nextToken();  //获取右边的word,并将拆分器右移动一个位置
            //获取单词完之后直接存入到WordCount类中
            dataList.add(new WordCount(word,Integer.valueOf(1)));
        }
        return new MapData(dataList);
    }
}
class ReduceActor extends UntypedActor   //ReduceActor---->reduceData---->AggregateActor
{
    public ActorRef aggregateActor = null;
    public ReduceActor(ActorRef aggregateActor)
    {
        this.aggregateActor = aggregateActor;
    }
    @Override
    public void onReceive(Object message) throws Exception
    {
        if(message instanceof  MapData)
        {
            MapData mapData = (MapData) message;
            ReduceData reduceData = reduce(mapData.getDataList());
            aggregateActor.tell(reduceData);
        }else
            unhandled(message);
    }
    //编写reduce函数,进行本地combiner操作
    public  ReduceData reduce(List<WordCount> listdata)
    {
        HashMap<String,Integer> hashMap = new HashMap<String,Integer>();
        //for迭代循环语法的灵活运用
        for (WordCount wordCount : listdata)
        {
            if (hashMap.containsKey(wordCount.getWord()))
            {
                Integer value = wordCount.getCount();
                value ++;
                hashMap.put(wordCount.getWord(),value);
            }else
                hashMap.put(wordCount.getWord(),Integer.valueOf(1));
        }
        return new ReduceData(hashMap);
    }
}
class  AggregateActor extends UntypedActor  //对于数据进行最终的归并操作
{
    public Map<String,Integer> finalHashMap = new HashMap<String,Integer>();
    @Override
    public void onReceive(Object message) throws Exception
    {
        if (message instanceof  ReduceData)
        {
            ReduceData msg = (ReduceData)message;
            aggregateInMemoryReduce(msg.getHashMap());  //对多个结果进行归并操作
        }else if(message instanceof Result)
        {
            System.out.println(finalHashMap);
        }else
            unhandled(message);
    }
    public void aggregateInMemoryReduce(Map<String,Integer> hashMap)
    {
        Integer value = null;
        for(String key:hashMap.keySet())
        {
            if (finalHashMap.containsKey(key))
            {
                value = finalHashMap.get(key) + hashMap.get(key);
                finalHashMap.put(key,value);
            }else
                finalHashMap.put(key,hashMap.get(key));
        }
    }
}
class WordCount
{
    private String word;
    private Integer count;
    public WordCount(String word,Integer count){this.word = word;this.count = count;}
    public String getWord(){return word;}
    public Integer getCount(){return count;}
}
class MapData   //MapData中存储的是List<WordCount>类型的数据
{
    public List<WordCount> dataList;  //终究类就是类型
    public MapData(List<WordCount> dataList)
    {
        this.dataList = dataList;
    }
    public List<WordCount> getDataList()
    {
        return dataList;
    }
}
class ReduceData  //ReduceData实际上也是用来进行数据存储的
{
    public HashMap<String,Integer> hashMap;  //泛型本质上也是数据类型
    public ReduceData(HashMap<String,Integer> hashMap)
    {
        this.hashMap = hashMap;
    }
    public HashMap<String,Integer> getHashMap()
    {
        return hashMap;
    }
}
class Result
{}

如有问题,欢迎留言指正!

你可能感兴趣的:(scala,大数据)