8.storm中不同的流分組方式

package backtype.storm.topology;
import backtype.storm.generated.GlobalStreamId;
import backtype.storm.generated.Grouping;
import backtype.storm.grouping.CustomStreamGrouping;
import backtype.storm.tuple.Fields;

public interface InputDeclarer<T extends InputDeclarer> {
    //字段分組
    public T fieldsGrouping(String componentId, Fields fields);
    public T fieldsGrouping(String componentId, String streamId, Fields fields);

          //全局分組
    public T globalGrouping(String componentId);
   
public T globalGrouping(String componentId, String streamId);

    //隨機分組
    public T shuffleGrouping(String componentId);
    public T shuffleGrouping(String componentId, String streamId);

        //本地或隨機分組
    public T localOrShuffleGrouping(String componentId);
   
public T localOrShuffleGrouping(String componentId, String streamId);

        //無分組
    public T noneGrouping(String componentId);
   
public T noneGrouping(String componentId, String streamId);

        //廣播分組
    public T allGrouping(String componentId);
   
public T allGrouping(String componentId, String streamId);

        //直接分組
    public T directGrouping(String componentId);
   
public T directGrouping(String componentId, String streamId);

        //自定義分組
    public T customGrouping(String componentId, CustomStreamGrouping grouping);
   
public T customGrouping(String componentId, String streamId, CustomStreamGrouping grouping);
   
    public T grouping(GlobalStreamId id, Grouping grouping);
}

1. 隨機分組
隨機分組(Shuffle Grouping)是最常用的流分組方式,它隨機地分發元組到 Bolt 上的任 務,這樣能保證每個任務得到相同數量的元組。
隨機分組執行原子操作,這是非常有用的,例如數學運算。但是,如果操作不能被隨機分發的話,應該考慮使用其他的分組方式,例如,在單詞統計(WordCount)例子中,需要計算單詞,就不適合使用隨機分組。

 2. 字段分組
字段分組(Fields Grouping)根據指定字段對流進行分組。例如,如果流是按user-id 字段進行分組,具有相同 user-id 的元組總是被分發到相同的任務,具有不同 user-id 的元組可能被分發到不同的任務。
字段分組是實現流連接和關聯,以及大量其他的用例的基礎。在實現上,字段分組使用取模散列來實現。

3. 廣播分組
廣播分組(All Grouping)是指流被髮送到所有 Bolt 的任務中。使用這個分組方式時要小心。

4. 全局分組
全局分組(
Global Grouping)是指全部流都發送到 Bolt 的同一個任務中,再具體一點,
是發送給 ID 最小的任務。

5. 無分組
假定你不關心流是如何分組的,則可以使用這種分組方式。目前這種分組和隨機分組是一 樣的效果,有一點不同的是 Storm 會把這個 Bolt 放到 Bolt 的訂閱者的同一個線程中執行。

6. 直接分組
直接分組(Direct Grouping)是一種特殊的分組。這種方式的流分組意味着由元組的生產 者決定元組的消費者的接收元組的任務。直接分組只能在已經聲明爲直接流(Direct Stream) 的流中使用,並且元組必須使用 emitDirect 方法來發射。Bolt 通過 TopologyContext 對象或者 OutputCollector 類的 emit 方法的返回值,可以得到其消費者的任務 id 列表(List<Integer>)。

7. 本地或者隨機分組
如果目標
Bolt 在同一工作進程存在一個或多個任務,元組會隨機分配給這些任務。否則,
該分組方式與隨機分組方式是一樣的。

8. 自定義分組
可以自定義流分組的方式,通過實現 CustomStreamGrouping 接口來創建自定義的流分組。 CustomStreamGrouping 接口的定義如下:
public interface CustomStreamGrouping extends Serializable {
void prepare(WorkerTopologyContext context, GlobalStreamId stream, List

<Integer> targetTasks);
List<Integer> chooseTasks(int taskId, List<Object> values);
}
CustomStreamGrouping 接口主要有兩個方法:prepare chooseTasks
CustomStreamGrouping 接口的具體實現,可以參考如下的代碼類:
storm.trident.partition.GlobalGrouping
storm.trident.partition.IdentityGrouping
storm.trident.partition.IndexHashGrouping
backtype.storm.testing.NGrouping
builder.setBolt("exclaim2", new ExclamationBolt(), 2).shuffleGrouping("exclaim1");
builder.setBolt("exclaim2", new ExclamationBolt(), 2).customGrouping("exclaim1", new GlobalGrouping());//自定義時的區別!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章