Strom小实例,大小写转换

结构:


RandomWordSpout:

  1. package cn.itcast.stormdemo;
  2. import java.util.Map;
  3. import java.util.Random;
  4. import backtype.storm.spout.SpoutOutputCollector;
  5. import backtype.storm.task.TopologyContext;
  6. import backtype.storm.topology.OutputFieldsDeclarer;
  7. import backtype.storm.topology.base.BaseRichSpout;
  8. import backtype.storm.tuple.Fields;
  9. import backtype.storm.tuple.Values;
  10. import backtype.storm.utils.Utils;
  11. public class RandomWordSpout extends BaseRichSpout{
  12. private SpoutOutputCollector collector;
  13. //模拟一些数据
  14. String[] words = {"iphone","xiaomi","mate","sony","sumsung","moto","meizu"};
  15. //不断地往下一个组件发送tuple消息
  16. //这里面是该spout组件的核心逻辑
  17. @Override
  18. public void nextTuple() {
  19. //可以从kafka消息队列中拿到数据,简便起见,我们从words数组中随机挑选一个商品名发送出去
  20. Random random = new Random();
  21. int index = random.nextInt(words.length);
  22. //通过随机数拿到一个商品名
  23. String godName = words[index];
  24. //将商品名封装成tuple,发送消息给下一个组件
  25. collector.emit(new Values(godName));
  26. //每发送一个消息,休眠500ms
  27. Utils.sleep(500);
  28. }
  29. //初始化方法,在spout组件实例化时调用一次
  30. @Override
  31. public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
  32. this.collector = collector;
  33. }
  34. //声明本spout组件发送出去的tuple中的数据的字段名
  35. @Override
  36. public void declareOutputFields(OutputFieldsDeclarer declarer) {
  37. declarer.declare(new Fields("orignname"));
  38. }
  39. }
SuffixBolt :

  1. package cn.itcast.stormdemo;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import java.util.Map;
  5. import java.util.UUID;
  6. import backtype.storm.task.TopologyContext;
  7. import backtype.storm.topology.BasicOutputCollector;
  8. import backtype.storm.topology.OutputFieldsDeclarer;
  9. import backtype.storm.topology.base.BaseBasicBolt;
  10. import backtype.storm.tuple.Tuple;
  11. public class SuffixBolt extends BaseBasicBolt{
  12. FileWriter fileWriter = null;
  13. //在bolt组件运行过程中只会被调用一次
  14. @Override
  15. public void prepare(Map stormConf, TopologyContext context) {
  16. try {
  17. fileWriter = new FileWriter("/home/hadoop/stormoutput/"+UUID.randomUUID());
  18. } catch (IOException e) {
  19. throw new RuntimeException(e);
  20. }
  21. }
  22. //该bolt组件的核心处理逻辑
  23. //每收到一个tuple消息,就会被调用一次
  24. @Override
  25. public void execute(Tuple tuple, BasicOutputCollector collector) {
  26. //先拿到上一个组件发送过来的商品名称
  27. String upper_name = tuple.getString(0);
  28. String suffix_name = upper_name + "_itisok";
  29. //为上一个组件发送过来的商品名称添加后缀
  30. try {
  31. fileWriter.write(suffix_name);
  32. fileWriter.write("\n");
  33. fileWriter.flush();
  34. } catch (IOException e) {
  35. throw new RuntimeException(e);
  36. }
  37. }
  38. //本bolt已经不需要发送tuple消息到下一个组件,所以不需要再声明tuple的字段
  39. @Override
  40. public void declareOutputFields(OutputFieldsDeclarer arg0) {
  41. }
  42. }
TopoMain :

  1. package cn.itcast.stormdemo;
  2. import backtype.storm.Config;
  3. import backtype.storm.StormSubmitter;
  4. import backtype.storm.generated.AlreadyAliveException;
  5. import backtype.storm.generated.InvalidTopologyException;
  6. import backtype.storm.generated.StormTopology;
  7. import backtype.storm.topology.TopologyBuilder;
  8. /**
  9. * 组织各个处理组件形成一个完整的处理流程,就是所谓的topology(类似于mapreduce程序中的job)
  10. * 并且将该topology提交给storm集群去运行,topology提交到集群后就将永无休止地运行,除非人为或者异常退出
  11. * @author duanhaitao@itcast.cn
  12. *
  13. */
  14. public class TopoMain {
  15. public static void main(String[] args) throws Exception {
  16. TopologyBuilder builder = new TopologyBuilder();
  17. //将我们的spout组件设置到topology中去
  18. //parallelism_hint :4 表示用4个excutor来执行这个组件
  19. //setNumTasks(8) 设置的是该组件执行时的并发task数量,也就意味着1个excutor会运行2个task
  20. builder.setSpout("randomspout", new RandomWordSpout(), 4).setNumTasks(8);
  21. //将大写转换bolt组件设置到topology,并且指定它接收randomspout组件的消息
  22. //.shuffleGrouping("randomspout")包含两层含义:
  23. //1、upperbolt组件接收的tuple消息一定来自于randomspout组件
  24. //2、randomspout组件和upperbolt组件的大量并发task实例之间收发消息时采用的分组策略是随机分组shuffleGrouping
  25. builder.setBolt("upperbolt", new UpperBolt(), 4).shuffleGrouping("randomspout");
  26. //将添加后缀的bolt组件设置到topology,并且指定它接收upperbolt组件的消息
  27. builder.setBolt("suffixbolt", new SuffixBolt(), 4).shuffleGrouping("upperbolt");
  28. //用builder来创建一个topology
  29. StormTopology demotop = builder.createTopology();
  30. //配置一些topology在集群中运行时的参数
  31. Config conf = new Config();
  32. //这里设置的是整个demotop所占用的槽位数,也就是worker的数量
  33. conf.setNumWorkers(4);
  34. conf.setDebug(true);
  35. conf.setNumAckers(0);
  36. //将这个topology提交给storm集群运行
  37. StormSubmitter.submitTopology("demotopo", conf, demotop);
  38. }
  39. }
UpperBolt :
  1. package cn.itcast.stormdemo;
  2. import backtype.storm.topology.BasicOutputCollector;
  3. import backtype.storm.topology.OutputFieldsDeclarer;
  4. import backtype.storm.topology.base.BaseBasicBolt;
  5. import backtype.storm.tuple.Fields;
  6. import backtype.storm.tuple.Tuple;
  7. import backtype.storm.tuple.Values;
  8. public class UpperBolt extends BaseBasicBolt{
  9. //业务处理逻辑
  10. @Override
  11. public void execute(Tuple tuple, BasicOutputCollector collector) {
  12. //先获取到上一个组件传递过来的数据,数据在tuple里面
  13. String godName = tuple.getString(0);
  14. //将商品名转换成大写
  15. String godName_upper = godName.toUpperCase();
  16. //将转换完成的商品名发送出去
  17. collector.emit(new Values(godName_upper));
  18. }
  19. //声明该bolt组件要发出去的tuple的字段
  20. @Override
  21. public void declareOutputFields(OutputFieldsDeclarer declarer) {
  22. declarer.declare(new Fields("uppername"));
  23. }
  24. }


 




posted @ 2015-12-11 19:29  暗夜小精灵~~  阅读(531)  评论(0编辑  收藏  举报