手动实现Storm的任务执行

package com.thp.bigdata;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 自己实现Strom框架的任务执行
 */
public class MyStorm {


    private Random random = new Random();

    private BlockingQueue sentenceQueue = new ArrayBlockingQueue(50000);
    private BlockingQueue wordQueue = new ArrayBlockingQueue(5000);

    // 用来保存最后计算的结果  key=单词, value=单词个数
    Map<String,Integer> counters = new HashMap<String, Integer>();

    // 用来发送句子
    public void nextTuple() {
        String[] sentences = new String[]{"the cow jumped over the moon",
                "an apple a day keeps the doctor away",
                "four score and seven years ago",
                "snow white and the seven dwarfs", "i am at two with nature"};

        String sentence = sentences[random.nextInt(sentences.length)];

        try {
            sentenceQueue.put(sentence);
            System.out.println("send sentence:" + sentence);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    // 用来切割句子
    public void split(String sentence) {
        System.out.println("resv sencentence" + sentence);
        String[] words = sentence.split(" ");
        for (String word : words) {
            word = word.trim();
            if(!word.isEmpty()) {
                word = word.toLowerCase();
                wordQueue.add(word);
                System.out.println("split word:" + word);
            }
        }
    }


    // 用来计算单词
    public void wordcounter(String word) {
        if(!counters.containsKey(word)) {
            counters.put(word,1);
        } else {
            int c = counters.get(word) + 1;
            counters.put(word,c);
        }
        System.out.println("print map:" + counters);
    }



    public BlockingQueue getSentenceQueue() {
        return this.sentenceQueue;
    }

    public void setSentenceQueue(BlockingQueue sentenceQueue) {
        this.sentenceQueue = sentenceQueue;
    }

    public BlockingQueue getWordQueue() {
        return wordQueue;
    }

    public void setWordQueue(BlockingQueue wordQueue) {
        this.wordQueue = wordQueue;
    }








    public static void main(String[] args) {
        // 线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        MyStorm myStorm = new MyStorm();
        // 发射句子到sentenceQueue
        executorService.submit(new MySpout(myStorm));
        // 接收一个句子,并且将句子进行分割
        executorService.submit(new MyBoltSplit(myStorm));
        // 接收一个单词,并进行极速昂
        executorService.submit(new MyBoltWordCount(myStorm));

    }




}

class MySpout extends Thread {
    private MyStorm myStorm;
    public MySpout(MyStorm myStorm) {
        this.myStorm = myStorm;
    }

    @Override
    public void run() {
        // storm框架在循环调用spout的nextTuple() 方法
        while(true) {
            myStorm.nextTuple();
            try {
                this.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class MyBoltWordCount extends Thread {
    private MyStorm myStorm;
    public MyBoltWordCount(MyStorm myStorm) {
        this.myStorm = myStorm;
    }

    @Override
    public void run() {
        while(true) {
            try {
                String word = (String) myStorm.getWordQueue().take();
                myStorm.wordcounter(word);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}


class MyBoltSplit extends Thread {
    private MyStorm myStorm;
    public MyBoltSplit(MyStorm myStorm) {
        this.myStorm = myStorm;
    }

    @Override
    public void run() {
        while (true) {
            try {
                String sentence = (String) myStorm.getSentenceQueue().take();
                myStorm.split(sentence);
            } catch (InterruptedException e) {
                e.printStackTrace();
                System.out.println(e);
            }
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值