2016-04-15 88 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); 
     } 
    } 
} 
相關問題