Storm概念
- storm的编程其实就是设计topology的过程,该过程中最主要的就是Spout、bolt还有grouping
关键类
创建topology
- 这个部分可以直接参考官网,使用TopologyBuilder类
TopologyBuilder builder = new TopologyBuilder();
builder.setSpout("createNum",new NumSpout(),1);
builder.setBolt("countNum",new SumBolt(),1)
.shuffleGrouping("createNum");
StormTopology topology=builder.createTopology();
Spout和Bolt
spout
- BaseRichSpout(帮我们将一些不用关注的方法空实现了)
- BaseComponent
- IComponent
- IRichSpout(主要需要实现的方法都在这里面)
- ISpout
- IComponent
- 现在BaseRichSpout中还需要实现的就是:
public class ReadSpout extends BaseRichSpout {
private SpoutOutputCollector collector;
private final static String FILE_DIR="testFile.txt";
private final static String SEPARATOR=",";
public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
this.collector=collector;
}
public void nextTuple() {
File file=new File(FILE_DIR);
try (
BufferedReader br=new BufferedReader(new FileReader(file));
){
String line;
while ((line=br.readLine())!=null) {
//发出消息
collector.emit(new Values(line));
System.out.println("line:" + line);
}
Thread.sleep(10000);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void declareOutputFields(OutputFieldsDeclarer declarer) {
//定义发出消息的字段名称
declarer.declare(new Fields("line"));
}
}
bolt
- BaseRichBolt
- BaseComponent
- IComponent
- IRichBolt(主要需要实现的方法都在这里面)
- IBolt
- IComponent
public class SplitBolt extends BaseRichBolt {
private OutputCollector collector;
private final static String SEPARATOR=",";
@Override
public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
this.collector=collector;
}
@Override
public void execute(Tuple input) {
String line=input.getStringByField("line");
String[] words=line.split(SEPARATOR);
for (String word:words){
this.collector.emit(new Values(word));
}
System.out.println("word:"+ Arrays.toString(words));
}
@Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(new Fields("word"));
}
}