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);
}
}
}
}
手动实现Storm的任务执行
最新推荐文章于 2020-04-29 11:13:12 发布