数据采集

  1. package com.jdxia.ack;
  2. import backtype.storm.spout.SpoutOutputCollector;
  3. import backtype.storm.task.TopologyContext;
  4. import backtype.storm.topology.OutputFieldsDeclarer;
  5. import backtype.storm.topology.base.BaseRichSpout;
  6. import backtype.storm.tuple.Fields;
  7. import backtype.storm.tuple.Values;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.UUID;
  11. public class AckSpout extends BaseRichSpout {
  12. private SpoutOutputCollector collector;
  13. //初始化方法
  14. @Override
  15. public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
  16. this.collector = collector;
  17. }
  18. //循环调用,每调用一次就发送一条消息
  19. @Override
  20. public void nextTuple() {
  21. //随机生产一条数据
  22. String uuid = UUID.randomUUID().toString().replace("_", " ");
  23. collector.emit(new Values(uuid), new Values(uuid));
  24. try {
  25. Thread.sleep(10 * 1000);
  26. } catch (InterruptedException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. //定义发送的字段
  31. @Override
  32. public void declareOutputFields(OutputFieldsDeclarer declarer) {
  33. declarer.declare(new Fields("uuid"));
  34. }
  35. @Override
  36. public void ack(Object msgId) {
  37. System.out.println("消息处理成功" + msgId);
  38. }
  39. @Override
  40. public void fail(Object msgId) {
  41. System.out.println("消息处理失败" + msgId);
  42. //重新发送消息
  43. collector.emit((List) msgId, msgId);
  44. }
  45. }

数据处理


  1. package com.jdxia.ack;
  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 Bolt1 extends BaseBasicBolt {
  9. @Override
  10. public void execute(Tuple input, BasicOutputCollector collector) {
  11. //输出数据
  12. collector.emit(new Values(input.getString(0)));
  13. System.out.println("Bolt1输出消息");
  14. }
  15. @Override
  16. public void declareOutputFields(OutputFieldsDeclarer declarer) {
  17. declarer.declare(new Fields("uuid"));
  18. }
  19. }

  1. package com.jdxia.ack;
  2. import backtype.storm.topology.BasicOutputCollector;
  3. import backtype.storm.topology.FailedException;
  4. import backtype.storm.topology.OutputFieldsDeclarer;
  5. import backtype.storm.topology.base.BaseBasicBolt;
  6. import backtype.storm.tuple.Tuple;
  7. public class Bolt2 extends BaseBasicBolt {
  8. @Override
  9. public void execute(Tuple input, BasicOutputCollector collector) {
  10. //如果需要抛出异常,成功就不要抛出异常
  11. throw new FailedException("异常");
  12. }
  13. @Override
  14. public void declareOutputFields(OutputFieldsDeclarer declarer) {
  15. }
  16. }

任务编排

  1. package com.jdxia.ack;
  2. import backtype.storm.Config;
  3. import backtype.storm.LocalCluster;
  4. import backtype.storm.generated.StormTopology;
  5. import backtype.storm.topology.TopologyBuilder;
  6. public class AckTopologyDriver {
  7. public static void main(String[] args) {
  8. //1. 准备任务信息
  9. TopologyBuilder topologyBuilder = new TopologyBuilder();
  10. topologyBuilder.setSpout("mySpout", new AckSpout(), 1);
  11. topologyBuilder.setBolt("bolt1", new Bolt1(), 1).shuffleGrouping("mySpout");
  12. topologyBuilder.setBolt("bolt2", new Bolt2(), 1).shuffleGrouping("bolt1");
  13. //2. 任务提交
  14. Config config = new Config();
  15. StormTopology stormTopology = topologyBuilder.createTopology();
  16. //本地模式
  17. LocalCluster localCluster = new LocalCluster();
  18. localCluster.submitTopology("ack", config, stormTopology);
  19. }
  20. }