2016-04-15 86 views
0

我正在尝试使用Apache Storm解决问题。我有以下疑问。如何在Apache Storm中添加用户定义的函数

  1. 有加比内置的功能,如​​,prepare()等其他螺栓用户自定义函数的任何方法?如果可能,如何从​​调用这样的函数?
  2. 也有可能在Bolt中添加'递归函数'类逻辑?

回答

0

当然,你可以添加任何方法给你,是的,它也可以是递归的。我不确定你的意思是“如何从​​调用这样的功能 - 只需从那里调用它 - 这是一个常规方法:

public class MyBolt extends IRichBolt { 
    void prepare(Map stormConf, TopologyContext context, OutputCollector collector) { /* put your code here */ } 
    void cleanup() { /* put your code here */ } 
    void declareOutputFields(OutputFieldsDeclarer declarer) { /* put your code here */ } 
    Map<String, Object> getComponentConfiguration() { /* put your code here */ } 

    void execute(Tuple input) { 
     // just call the new methods 
     int x = myFirstFunction(); 
     mySecondFunction(5); 
    } 

    // can also be public or protected etc (any return type or parameters are ok) 
    private int myFirstFunction() { 
     return 0; 
    } 
    // recursive 
    private void mySecondFunction(int a) { 
     while(--a > 0) { 
      mySecondFunction(a); 
     } 
    } 
} 
相关问题